OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library vm_connect_element; | |
6 | |
7 import 'dart:html'; | |
8 import 'package:polymer/polymer.dart'; | |
9 import 'observatory_element.dart'; | |
10 import 'package:observatory/app.dart'; | |
11 import 'package:observatory/service_html.dart'; | |
12 | |
13 void _connectToVM(ObservatoryApplication app, WebSocketVMTarget target) { | |
14 app.vm = new WebSocketVM(target); | |
15 } | |
16 | |
17 @CustomTag('vm-connect-target') | |
18 class VMConnectTargetElement extends ObservatoryElement { | |
19 @published WebSocketVMTarget target; | |
20 | |
21 VMConnectTargetElement.created() : super.created(); | |
22 | |
23 bool get isChromeTarget { | |
24 if (target == null) { | |
25 return false; | |
26 } | |
27 return target.chrome; | |
28 } | |
29 | |
30 bool get isCurrentTarget { | |
31 if (app.vm == null) { | |
32 return false; | |
33 } | |
34 return (app.vm as WebSocketVM).target == target; | |
35 } | |
36 | |
37 void connectToVm(MouseEvent event, var detail, Element node) { | |
38 if (event.button > 0 || event.metaKey || event.ctrlKey || | |
39 event.shiftKey || event.altKey) { | |
40 // Not a left-click or a left-click with a modifier key: | |
41 // Let browser handle. | |
42 return; | |
43 } | |
44 event.preventDefault(); | |
45 WebSocketVM currentVM = app.vm; | |
46 if ((currentVM == null) || (currentVM.target != target)) { | |
47 _connectToVM(app, target); | |
48 } | |
49 var href = node.attributes['href']; | |
50 app.locationManager.go(href); | |
51 } | |
52 | |
53 void deleteVm(MouseEvent event, var detail, Element node) { | |
54 app.targets.remove(target); | |
55 } | |
56 } | |
57 | |
58 @CustomTag('vm-connect') | |
59 class VMConnectElement extends ObservatoryElement { | |
60 @published String standaloneVmAddress = ''; | |
61 @published String chromiumAddress = 'localhost:9222'; | |
62 @observable ObservableList<WebSocketVMTarget> chromeTargets = | |
63 new ObservableList<WebSocketVMTarget>(); | |
64 | |
65 VMConnectElement.created() : super.created() { | |
66 pollPeriod = new Duration(seconds: 1); | |
67 } | |
68 | |
69 void _connect(WebSocketVMTarget target) { | |
70 _connectToVM(app, target); | |
71 app.locationManager.go('#/vm'); | |
72 } | |
73 | |
74 void onPoll() { | |
75 _refreshTabs(); | |
76 } | |
77 | |
78 String _normalizeStandaloneAddress(String networkAddress) { | |
79 if (networkAddress.startsWith('ws://')) { | |
80 return networkAddress; | |
81 } | |
82 return 'ws://${networkAddress}/ws'; | |
83 } | |
84 | |
85 void connectStandalone(Event e, var detail, Node target) { | |
86 // Prevent any form action. | |
87 e.preventDefault(); | |
88 var targetAddress = _normalizeStandaloneAddress(standaloneVmAddress); | |
89 var target = app.targets.findOrMake(targetAddress); | |
90 _connect(target); | |
91 } | |
92 | |
93 void getTabs(Event e, var detail, Node target) { | |
94 // Prevent any form action. | |
95 e.preventDefault(); | |
96 _refreshTabs(); | |
97 } | |
98 | |
99 void _refreshTabs() { | |
100 ChromiumTargetLister.fetch(chromiumAddress).then((targets) { | |
101 chromeTargets.clear(); | |
102 if (targets == null) { | |
103 return; | |
104 } | |
105 for (var i = 0; i < targets.length; i++) { | |
106 if (targets[i].networkAddress == null) { | |
107 // Don't add targets that don't have a network address. | |
108 // This happens when a tab has devtools open! | |
109 continue; | |
110 } | |
111 chromeTargets.add(targets[i]); | |
112 } | |
113 }).catchError((e) { | |
114 chromeTargets.clear(); | |
115 }); | |
116 } | |
117 } | |
OLD | NEW |