Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library script_inset_element; | 5 library script_inset_element; |
| 6 | 6 |
| 7 import 'dart:html'; | |
| 7 import 'observatory_element.dart'; | 8 import 'observatory_element.dart'; |
| 8 import 'package:observatory/service.dart'; | 9 import 'package:observatory/service.dart'; |
| 9 import 'package:polymer/polymer.dart'; | 10 import 'package:polymer/polymer.dart'; |
| 10 | 11 |
| 11 /// Box with script source code in it. | 12 /// Box with script source code in it. |
| 12 @CustomTag('script-inset') | 13 @CustomTag('script-inset') |
| 13 class ScriptInsetElement extends ObservatoryElement { | 14 class ScriptInsetElement extends ObservatoryElement { |
| 14 @published Script script; | 15 @published Script script; |
| 15 @published int pos; | 16 |
| 17 /// Set the height to make the script inset scroll. Otherwise it | |
| 18 /// will show from startPos to endPos. | |
| 19 @published String height = null; | |
| 20 | |
| 21 @published int currentPos; | |
| 22 @published int startPos; | |
| 16 @published int endPos; | 23 @published int endPos; |
| 17 final List<int> lineNumbers = new ObservableList<int>(); | 24 |
| 25 @observable int currentLine; | |
| 18 @observable int startLine; | 26 @observable int startLine; |
| 19 @observable int endLine; | 27 @observable int endLine; |
| 20 | 28 |
| 21 @observable List<ScriptLine> lines = toObservable([]); | 29 @observable List<ScriptLine> lines = toObservable([]); |
| 22 | 30 |
| 31 String makeLineId(int line) { | |
| 32 return 'line-$line'; | |
| 33 } | |
| 34 | |
| 35 void _onMutation(mutations, observer) { | |
| 36 var line = shadowRoot.querySelector('#line-$currentLine'); | |
| 37 if (line != null) { | |
| 38 line.scrollIntoView(); | |
| 39 } | |
| 40 } | |
| 41 | |
| 23 void attached() { | 42 void attached() { |
| 24 super.attached(); | 43 super.attached(); |
| 44 var table = shadowRoot.querySelector('.sourceTable'); | |
| 45 if (table != null) { | |
| 46 var observer = new MutationObserver(_onMutation); | |
|
Cutch
2014/07/09 15:49:22
Do we have to do anything with the mutation observ
turnidge
2014/07/09 17:36:32
Yes, there is a disconnect method I should call.
| |
| 47 observer.observe(table, childList:true); | |
| 48 } | |
| 25 } | 49 } |
| 26 | 50 |
| 27 void scriptChanged(oldValue) { | 51 void currentPosChanged(oldValue) { |
| 28 _updateLines(); | 52 _updateLines(); |
| 29 } | 53 } |
| 30 | 54 |
| 31 void posChanged(oldValue) { | 55 void startPosChanged(oldValue) { |
| 32 _updateLines(); | 56 _updateLines(); |
| 33 } | 57 } |
| 34 | 58 |
| 35 void endPosChanged(oldValue) { | 59 void endPosChanged(oldValue) { |
| 36 _updateLines(); | 60 _updateLines(); |
| 37 } | 61 } |
| 38 | 62 |
| 39 static const hitStyleNone = 'min-width:32px;'; | 63 void scriptChanged(oldValue) { |
| 40 static const hitStyleExecuted = 'min-width:32px; background-color:green'; | 64 _updateLines(); |
| 41 static const hitStyleNotExecuted = 'min-width:32px; background-color:red'; | |
| 42 | |
| 43 /// [hits] can be null which indicates that the line is not executable. | |
| 44 /// When [hits] is 0, the line is executable but hasn't been executed and | |
| 45 /// when [hits] is positive, the line is executable and has been executed. | |
| 46 String styleForHits(int hits) { | |
| 47 if (hits == null) { | |
| 48 return hitStyleNone; | |
| 49 } else if (hits == 0) { | |
| 50 return hitStyleNotExecuted; | |
| 51 } | |
| 52 assert(hits > 0); | |
| 53 return hitStyleExecuted; | |
| 54 } | 65 } |
| 55 | 66 |
| 56 var _updateFuture; | 67 var _updateFuture; |
| 57 | 68 |
| 58 void _updateLines() { | 69 void _updateLines() { |
| 59 if (_updateFuture != null) { | 70 if (_updateFuture != null) { |
| 60 // Already scheduled. | 71 // Already scheduled. |
| 61 return; | 72 return; |
| 62 } | 73 } |
| 63 if (!script.loaded) { | 74 if (!script.loaded) { |
| 64 _updateFuture = script.load().then((_) { | 75 _updateFuture = script.load().then((_) { |
| 65 if (script.loaded) { | 76 if (script.loaded) { |
| 66 _updateFuture = null; | 77 _updateFuture = null; |
| 67 _updateLines(); | 78 _updateLines(); |
| 68 } | 79 } |
| 69 }); | 80 }); |
| 70 return; | 81 return; |
| 71 } | 82 } |
| 72 startLine = | 83 startLine = (startPos != null |
| 73 (pos != null) ? script.tokenToLine(pos) - 1 : 0; | 84 ? script.tokenToLine(startPos) |
| 74 endLine = | 85 : 1); |
| 75 (endPos != null) ? script.tokenToLine(endPos) : startLine + 1; | 86 currentLine = (currentPos != null |
| 76 // Add line numbers. | 87 ? script.tokenToLine(currentPos) |
| 77 lineNumbers.clear(); | 88 : null); |
| 78 for (var i = startLine; i < endLine; i++) { | 89 endLine = (endPos != null |
| 79 lineNumbers.add(i); | 90 ? script.tokenToLine(endPos) |
| 91 : script.lines.length); | |
| 92 lines.clear(); | |
| 93 for (int i = (startLine - 1); i <= (endLine - 1); i++) { | |
| 94 lines.add(script.lines[i]); | |
| 80 } | 95 } |
| 81 } | 96 } |
| 82 | 97 |
| 83 ScriptInsetElement.created() : super.created(); | 98 ScriptInsetElement.created() : super.created(); |
| 84 } | 99 } |
| OLD | NEW |