OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library observatory_element; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:html'; | |
9 import 'package:observatory/app.dart'; | |
10 import 'package:polymer/polymer.dart'; | |
11 | |
12 /// Base class for all Observatory custom elements. | |
13 @CustomTag('observatory-element') | |
14 class ObservatoryElement extends PolymerElement { | |
15 ObservatoryElement.created() : super.created(); | |
16 | |
17 ObservatoryApplication get app => ObservatoryApplication.app; | |
18 Page get page => app.currentPage; | |
19 ObservableMap get args => page.args; | |
20 | |
21 @override | |
22 void attached() { | |
23 super.attached(); | |
24 _startPoll(); | |
25 } | |
26 | |
27 @override | |
28 void attributeChanged(String name, var oldValue, var newValue) { | |
29 super.attributeChanged(name, oldValue, newValue); | |
30 } | |
31 | |
32 @override | |
33 void detached() { | |
34 super.detached(); | |
35 _stopPoll(); | |
36 } | |
37 | |
38 @override | |
39 void ready() { | |
40 super.ready(); | |
41 } | |
42 | |
43 /// Set to a non-null value to enable polling on this element. When the poll | |
44 /// timer fires, onPoll will be called. | |
45 @observable Duration pollPeriod; | |
46 Timer _pollTimer; | |
47 | |
48 /// Called every [pollPeriod] while the element is attached to the DOM. | |
49 void onPoll() { } | |
50 | |
51 void pollPeriodChanged(oldValue) { | |
52 if (pollPeriod != null) { | |
53 _startPoll(); | |
54 } else { | |
55 _stopPoll(); | |
56 } | |
57 } | |
58 | |
59 void _startPoll() { | |
60 if (pollPeriod == null) { | |
61 return; | |
62 } | |
63 if (_pollTimer != null) { | |
64 _pollTimer.cancel(); | |
65 } | |
66 _pollTimer = new Timer(pollPeriod, _onPoll); | |
67 } | |
68 | |
69 void _stopPoll() { | |
70 if (_pollTimer != null) { | |
71 _pollTimer.cancel(); | |
72 } | |
73 _pollTimer = null; | |
74 } | |
75 | |
76 void _onPoll() { | |
77 onPoll(); | |
78 if (pollPeriod == null) { | |
79 // Stop polling. | |
80 _stopPoll(); | |
81 return; | |
82 } | |
83 // Restart timer. | |
84 _pollTimer = new Timer(pollPeriod, _onPoll); | |
85 } | |
86 | |
87 /// Utility method for handling on-click of <a> tags. Navigates | |
88 /// within the application using the [LocationManager]. | |
89 void goto(MouseEvent event, var detail, Element target) { | |
90 app.locationManager.onGoto(event, detail, target); | |
91 } | |
92 | |
93 /// Create a link that can be consumed by [goto]. | |
94 String gotoLink(String url) { | |
95 return app.locationManager.makeLink(url); | |
96 } | |
97 | |
98 String formatTimePrecise(double time) => Utils.formatTimePrecise(time); | |
99 | |
100 String formatTime(double time) => Utils.formatTime(time); | |
101 | |
102 String formatSeconds(double x) => Utils.formatSeconds(x); | |
103 | |
104 | |
105 String formatSize(int bytes) => Utils.formatSize(bytes); | |
106 | |
107 String fileAndLine(Map frame) { | |
108 var file = frame['script'].name; | |
109 var shortFile = file.substring(file.lastIndexOf('/') + 1); | |
110 return "${shortFile}:${frame['line']}"; | |
111 } | |
112 | |
113 int parseInt(String value) => int.parse(value); | |
114 | |
115 String asStringLiteral(String value, [bool wasTruncated=false]) { | |
116 var result = new List(); | |
117 result.add("'".codeUnitAt(0)); | |
118 for (int codeUnit in value.codeUnits) { | |
119 if (codeUnit == '\n'.codeUnitAt(0)) result.addAll('\\n'.codeUnits); | |
120 else if (codeUnit == '\r'.codeUnitAt(0)) result.addAll('\\r'.codeUnits); | |
121 else if (codeUnit == '\f'.codeUnitAt(0)) result.addAll('\\f'.codeUnits); | |
122 else if (codeUnit == '\b'.codeUnitAt(0)) result.addAll('\\b'.codeUnits); | |
123 else if (codeUnit == '\t'.codeUnitAt(0)) result.addAll('\\t'.codeUnits); | |
124 else if (codeUnit == '\v'.codeUnitAt(0)) result.addAll('\\v'.codeUnits); | |
125 else if (codeUnit == '\$'.codeUnitAt(0)) result.addAll('\\\$'.codeUnits); | |
126 else if (codeUnit == '\\'.codeUnitAt(0)) result.addAll('\\\\'.codeUnits); | |
127 else if (codeUnit == "'".codeUnitAt(0)) result.addAll("'".codeUnits); | |
128 else if (codeUnit < 32) { | |
129 var escapeSequence = "\\u" + codeUnit.toRadixString(16).padLeft(4, "0")
; | |
130 result.addAll(escapeSequence.codeUnits); | |
131 } else result.add(codeUnit); | |
132 } | |
133 if (wasTruncated) { | |
134 result.addAll("...".codeUnits); | |
135 } else { | |
136 result.add("'".codeUnitAt(0)); | |
137 } | |
138 return new String.fromCharCodes(result); | |
139 } | |
140 } | |
OLD | NEW |