| 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:observatory/service.dart'; | |
| 11 import 'package:polymer/polymer.dart'; | |
| 12 | |
| 13 /// Base class for all Observatory custom elements. | |
| 14 class ObservatoryElement extends PolymerElement { | |
| 15 ObservatoryElement.created() : super.created(); | |
| 16 | |
| 17 ObservatoryApplication get app => ObservatoryApplication.app; | |
| 18 Page get page => app.currentPage; | |
| 19 | |
| 20 @override | |
| 21 void attached() { | |
| 22 super.attached(); | |
| 23 _startPoll(); | |
| 24 } | |
| 25 | |
| 26 @override | |
| 27 void attributeChanged(String name, var oldValue, var newValue) { | |
| 28 super.attributeChanged(name, oldValue, newValue); | |
| 29 } | |
| 30 | |
| 31 @override | |
| 32 void detached() { | |
| 33 super.detached(); | |
| 34 _stopPoll(); | |
| 35 } | |
| 36 | |
| 37 @override | |
| 38 void ready() { | |
| 39 super.ready(); | |
| 40 } | |
| 41 | |
| 42 /// Set to a non-null value to enable polling on this element. When the poll | |
| 43 /// timer fires, onPoll will be called. | |
| 44 @observable Duration pollPeriod; | |
| 45 Timer _pollTimer; | |
| 46 | |
| 47 /// Called every [pollPeriod] while the element is attached to the DOM. | |
| 48 void onPoll() { } | |
| 49 | |
| 50 void pollPeriodChanged(oldValue) { | |
| 51 if (pollPeriod != null) { | |
| 52 _startPoll(); | |
| 53 } else { | |
| 54 _stopPoll(); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 void _startPoll() { | |
| 59 if (pollPeriod == null) { | |
| 60 return; | |
| 61 } | |
| 62 if (_pollTimer != null) { | |
| 63 _pollTimer.cancel(); | |
| 64 } | |
| 65 _pollTimer = new Timer(pollPeriod, _onPoll); | |
| 66 } | |
| 67 | |
| 68 void _stopPoll() { | |
| 69 if (_pollTimer != null) { | |
| 70 _pollTimer.cancel(); | |
| 71 } | |
| 72 _pollTimer = null; | |
| 73 } | |
| 74 | |
| 75 void _onPoll() { | |
| 76 onPoll(); | |
| 77 if (pollPeriod == null) { | |
| 78 // Stop polling. | |
| 79 _stopPoll(); | |
| 80 return; | |
| 81 } | |
| 82 // Restart timer. | |
| 83 _pollTimer = new Timer(pollPeriod, _onPoll); | |
| 84 } | |
| 85 | |
| 86 /// Utility method for handling on-click of <a> tags. Navigates | |
| 87 /// within the application using the [LocationManager]. | |
| 88 void goto(MouseEvent event, var detail, Element target) { | |
| 89 app.locationManager.onGoto(event); | |
| 90 event.stopPropagation(); | |
| 91 } | |
| 92 | |
| 93 void onClickGoto(MouseEvent event) { | |
| 94 app.locationManager.onGoto(event); | |
| 95 event.stopPropagation(); | |
| 96 } | |
| 97 | |
| 98 String makeLink(String url, [ServiceObject obj]) { | |
| 99 if (obj != null) { | |
| 100 if (obj is Isolate) { | |
| 101 url = '${url}?isolateId=${Uri.encodeComponent(obj.id)}'; | |
| 102 } else { | |
| 103 if (obj.id == null) { | |
| 104 // No id | |
| 105 return url; | |
| 106 } | |
| 107 url = ('${url}?isolateId=${Uri.encodeComponent(obj.isolate.id)}' | |
| 108 '&objectId=${Uri.encodeComponent(obj.id)}'); | |
| 109 } | |
| 110 } | |
| 111 return url; | |
| 112 } | |
| 113 | |
| 114 /// Create a link that can be consumed by [goto]. | |
| 115 String gotoLink(String url, [ServiceObject obj]) { | |
| 116 return app.locationManager.makeLink(makeLink(url, obj)); | |
| 117 } | |
| 118 String gotoLinkForwardingParameters(String url, [ServiceObject obj]) { | |
| 119 return app.locationManager.makeLinkForwardingParameters(makeLink(url, obj)); | |
| 120 } | |
| 121 | |
| 122 String formatTimePrecise(double time) => Utils.formatTimePrecise(time); | |
| 123 String formatTimeMilliseconds(int millis) => | |
| 124 Utils.formatTimeMilliseconds(millis); | |
| 125 String formatTime(double time) => Utils.formatTime(time); | |
| 126 | |
| 127 String formatSeconds(double x) => Utils.formatSeconds(x); | |
| 128 | |
| 129 | |
| 130 String formatSize(int bytes) => Utils.formatSize(bytes); | |
| 131 | |
| 132 int parseInt(String value) => int.parse(value); | |
| 133 | |
| 134 String asStringLiteral(String value, [bool wasTruncated=false]) { | |
| 135 var result = new List(); | |
| 136 result.add("'".codeUnitAt(0)); | |
| 137 for (int codeUnit in value.codeUnits) { | |
| 138 if (codeUnit == '\n'.codeUnitAt(0)) result.addAll('\\n'.codeUnits); | |
| 139 else if (codeUnit == '\r'.codeUnitAt(0)) result.addAll('\\r'.codeUnits); | |
| 140 else if (codeUnit == '\f'.codeUnitAt(0)) result.addAll('\\f'.codeUnits); | |
| 141 else if (codeUnit == '\b'.codeUnitAt(0)) result.addAll('\\b'.codeUnits); | |
| 142 else if (codeUnit == '\t'.codeUnitAt(0)) result.addAll('\\t'.codeUnits); | |
| 143 else if (codeUnit == '\v'.codeUnitAt(0)) result.addAll('\\v'.codeUnits); | |
| 144 else if (codeUnit == '\$'.codeUnitAt(0)) result.addAll('\\\$'.codeUnits); | |
| 145 else if (codeUnit == '\\'.codeUnitAt(0)) result.addAll('\\\\'.codeUnits); | |
| 146 else if (codeUnit == "'".codeUnitAt(0)) result.addAll("'".codeUnits); | |
| 147 else if (codeUnit < 32) { | |
| 148 var escapeSequence = "\\u" + codeUnit.toRadixString(16).padLeft(4, "0")
; | |
| 149 result.addAll(escapeSequence.codeUnits); | |
| 150 } else result.add(codeUnit); | |
| 151 } | |
| 152 if (wasTruncated) { | |
| 153 result.addAll("...".codeUnits); | |
| 154 } else { | |
| 155 result.add("'".codeUnitAt(0)); | |
| 156 } | |
| 157 return new String.fromCharCodes(result); | |
| 158 } | |
| 159 | |
| 160 void clearShadowRoot() { | |
| 161 // Remove all non-style elements. | |
| 162 // Have to do the following because removeWhere doesn't work on DOM child | |
| 163 // node lists. i.e. removeWhere((e) => e is! StyleElement); | |
| 164 var styleElements = []; | |
| 165 for (var child in shadowRoot.children) { | |
| 166 if (child is StyleElement) { | |
| 167 styleElements.add(child); | |
| 168 } | |
| 169 } | |
| 170 shadowRoot.children.clear(); | |
| 171 for (var style in styleElements) { | |
| 172 shadowRoot.children.add(style); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void insertTextSpanIntoShadowRoot(String text) { | |
| 177 var spanElement = new SpanElement(); | |
| 178 spanElement.text = text; | |
| 179 shadowRoot.children.add(spanElement); | |
| 180 } | |
| 181 | |
| 182 void insertLinkIntoShadowRoot(String label, String href, [String title]) { | |
| 183 var anchorElement = new AnchorElement(); | |
| 184 anchorElement.href = href; | |
| 185 anchorElement.text = label; | |
| 186 if (title != null) { | |
| 187 anchorElement.title = title; | |
| 188 } | |
| 189 anchorElement.onClick.listen(onClickGoto); | |
| 190 shadowRoot.children.add(anchorElement); | |
| 191 } | |
| 192 } | |
| OLD | NEW |