OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 vm_connect_element; | 5 library vm_connect_element; |
6 | 6 |
| 7 import 'dart:html'; |
| 8 import 'dart:async'; |
7 import 'dart:convert'; | 9 import 'dart:convert'; |
8 import 'dart:html'; | 10 import 'package:observatory/models.dart' as M; |
| 11 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 12 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
| 13 import 'package:observatory/src/elements/nav/bar.dart'; |
| 14 import 'package:observatory/src/elements/nav/notify.dart'; |
| 15 import 'package:observatory/src/elements/nav/top_menu.dart'; |
| 16 import 'package:observatory/src/elements/view_footer.dart'; |
| 17 import 'package:observatory/src/elements/vm_connect_target.dart'; |
9 | 18 |
10 import 'observatory_element.dart'; | 19 class VMConnectElement extends HtmlElement implements Renderable{ |
11 import 'package:observatory/elements.dart'; | 20 static const tag = const Tag<VMConnectElement>('vm-connect', |
12 import 'package:observatory/service_html.dart'; | 21 dependencies: const [NavBarElement.tag, |
13 import 'package:polymer/polymer.dart'; | 22 NavTopMenuElement.tag, |
| 23 NavNotifyElement.tag, |
| 24 ViewFooterElement.tag, |
| 25 VMConnectTargetElement.tag]); |
14 | 26 |
15 @CustomTag('vm-connect') | 27 RenderingScheduler _r; |
16 class VMConnectElement extends ObservatoryElement { | |
17 @published String standaloneVmAddress = ''; | |
18 | 28 |
19 VMConnectElement.created() : super.created() { | 29 Stream<RenderedEvent<VMConnectElement>> get onRendered => _r.onRendered; |
| 30 |
| 31 M.CrashDumpRepository _dump; |
| 32 M.NotificationRepository _notifications; |
| 33 M.TargetRepository _targets; |
| 34 StreamSubscription _targetsSubscription; |
| 35 |
| 36 String _address; |
| 37 |
| 38 factory VMConnectElement(M.TargetRepository targets, |
| 39 M.CrashDumpRepository dump, |
| 40 M.NotificationRepository notifications, |
| 41 {String address: '', RenderingQueue queue}) { |
| 42 assert(address != null); |
| 43 assert(dump != null); |
| 44 assert(notifications != null); |
| 45 assert(targets != null); |
| 46 VMConnectElement e = document.createElement(tag.name); |
| 47 e._r = new RenderingScheduler(e, queue: queue); |
| 48 e._address = address; |
| 49 e._dump = dump; |
| 50 e._notifications = notifications; |
| 51 e._targets = targets; |
| 52 return e; |
20 } | 53 } |
21 | 54 |
22 void _connect(WebSocketVMTarget target) { | 55 VMConnectElement.created() : super.created(); |
23 app.vm = new WebSocketVM(target); | 56 |
24 app.locationManager.goForwardingParameters('/vm'); | 57 @override |
| 58 void attached() { |
| 59 super.attached(); _r.enable(); |
| 60 _targetsSubscription = _targets.onChange.listen((_) => _r.dirty()); |
25 } | 61 } |
26 | 62 |
27 @override | 63 @override |
28 void attached() { | 64 void detached() { |
29 super.attached(); | 65 super.detached(); children = []; _r.disable(notify: true); |
30 var fileInput = shadowRoot.querySelector('#crashDumpFile'); | 66 _targetsSubscription.cancel(); _targetsSubscription = null; |
31 fileInput.onChange.listen(_onCrashDumpFileChange); | |
32 } | 67 } |
33 | 68 |
34 String _normalizeStandaloneAddress(String networkAddress) { | 69 void render() { |
| 70 children = [ |
| 71 new NavBarElement(queue: _r.queue) |
| 72 ..children = [ |
| 73 new NavTopMenuElement(last: true, queue: _r.queue), |
| 74 new NavNotifyElement(_notifications, queue: _r.queue) |
| 75 ], |
| 76 new DivElement() |
| 77 ..classes = ['content-centered'] |
| 78 ..children = [ |
| 79 new HeadingElement.h1()..text = 'Connect to a Dart VM', |
| 80 new BRElement(), new HRElement(), |
| 81 new DivElement() |
| 82 ..classes = ['flex-row'] |
| 83 ..children = [ |
| 84 new DivElement() |
| 85 ..classes = ['flex-item-40-percent'] |
| 86 ..children = [ |
| 87 new HeadingElement.h2()..text = 'WebSocket', |
| 88 new BRElement(), |
| 89 new UListElement() |
| 90 ..children = _targets.list().map((target) { |
| 91 return new LIElement() |
| 92 ..children = [new VMConnectTargetElement(target, |
| 93 current: target == _targets.current, queue: _r.queue) |
| 94 ..onConnect.listen(_connect) |
| 95 ..onDelete.listen(_delete) |
| 96 ]; |
| 97 }).toList(), |
| 98 new HRElement(), |
| 99 new FormElement() |
| 100 ..autocomplete = 'on' |
| 101 ..children = [ |
| 102 _createAddressBox(), |
| 103 new SpanElement()..text = ' ', |
| 104 new ButtonElement() |
| 105 ..classes = ['vm_connect'] |
| 106 ..text = 'Connect' |
| 107 ..onClick.listen((e) { |
| 108 e.preventDefault(); _create(); }), |
| 109 ], |
| 110 new BRElement(), |
| 111 new PreElement() |
| 112 ..classes = ['well'] |
| 113 ..text = 'Run Standalone with: \'--observe\'', |
| 114 new HRElement() |
| 115 ], |
| 116 new DivElement() |
| 117 ..classes = ['flex-item-20-percent'], |
| 118 new DivElement() |
| 119 ..classes = ['flex-item-40-percent'] |
| 120 ..children = [ |
| 121 new HeadingElement.h2()..text = 'Crash dump', |
| 122 new BRElement(), |
| 123 _createCrushDumpLoader(), |
| 124 new BRElement(), new BRElement(), |
| 125 new PreElement() |
| 126 ..classes = ['well'] |
| 127 ..text = 'Request a crash dump with:\n' |
| 128 '\'curl localhost:8181/_getCrashDump > dump.json\'', |
| 129 new HRElement() |
| 130 ] |
| 131 ], |
| 132 ], |
| 133 new ViewFooterElement(queue: _r.queue) |
| 134 ]; |
| 135 } |
| 136 |
| 137 TextInputElement _createAddressBox() { |
| 138 var textbox = new TextInputElement() |
| 139 ..classes = ['textbox'] |
| 140 ..placeholder = 'localhost:8181' |
| 141 ..value = _address |
| 142 ..onKeyUp |
| 143 .where((e) => e.key == '\n') |
| 144 .listen((e) { e.preventDefault(); _create(); }); |
| 145 textbox.onInput.listen((e) { |
| 146 _address = textbox.value; |
| 147 }); |
| 148 return textbox; |
| 149 } |
| 150 |
| 151 FileUploadInputElement _createCrushDumpLoader() { |
| 152 FileUploadInputElement e = new FileUploadInputElement() |
| 153 ..id = 'crashDumpFile'; |
| 154 e.onChange.listen((_) { |
| 155 var reader = new FileReader(); |
| 156 reader.readAsText(e.files[0]); |
| 157 reader.onLoad.listen((_) { |
| 158 var crashDump = JSON.decode(reader.result); |
| 159 _dump.load(crashDump); |
| 160 }); |
| 161 }); |
| 162 return e; |
| 163 } |
| 164 void _create() { |
| 165 if (_address == null || _address.isEmpty) return; |
| 166 _targets.add(_normalizeStandaloneAddress(_address)); |
| 167 } |
| 168 void _connect(TargetEvent e) => _targets.setCurrent(e.target); |
| 169 void _delete(TargetEvent e) => _targets.delete(e.target); |
| 170 |
| 171 static String _normalizeStandaloneAddress(String networkAddress) { |
35 if (networkAddress.startsWith('ws://')) { | 172 if (networkAddress.startsWith('ws://')) { |
36 return networkAddress; | 173 return networkAddress; |
37 } | 174 } |
38 return 'ws://${networkAddress}/ws'; | 175 return 'ws://${networkAddress}/ws'; |
39 } | 176 } |
40 | |
41 void connectStandalone(Event e, var detail, Node target) { | |
42 // Prevent any form action. | |
43 e.preventDefault(); | |
44 if (standaloneVmAddress == null) { | |
45 return; | |
46 } | |
47 if (standaloneVmAddress.isEmpty) { | |
48 return; | |
49 } | |
50 var targetAddress = _normalizeStandaloneAddress(standaloneVmAddress); | |
51 var target = app.targets.findOrMake(targetAddress); | |
52 _connect(target); | |
53 } | |
54 | |
55 _onCrashDumpFileChange(e) { | |
56 var fileInput = shadowRoot.querySelector('#crashDumpFile'); | |
57 var reader = new FileReader(); | |
58 reader.readAsText(fileInput.files[0]); | |
59 reader.onLoad.listen((_) { | |
60 var crashDump = JSON.decode(reader.result); | |
61 app.loadCrashDump(crashDump); | |
62 }); | |
63 } | |
64 } | 177 } |
OLD | NEW |