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 part of app; | 5 part of app; |
6 | 6 |
7 class LocationManager { | 7 class LocationManager { |
8 final ObservatoryApplication _app; | 8 final ObservatoryApplication _app; |
9 | 9 |
10 /// [internalArguments] are parameters specified after a '---' in the | 10 /// [internalArguments] are parameters specified after a '---' in the |
11 /// application URL. | 11 /// application URL. |
12 final Map<String, String> internalArguments = new Map<String, String>(); | 12 final Map<String, String> internalArguments = new Map<String, String>(); |
13 | 13 |
14 Uri _uri; | 14 Uri _uri; |
| 15 |
15 /// [uri] is the application uri. Application uris consist of a path and | 16 /// [uri] is the application uri. Application uris consist of a path and |
16 /// the queryParameters map. | 17 /// the queryParameters map. |
17 Uri get uri => _uri; | 18 Uri get uri => _uri; |
18 | 19 |
19 LocationManager(this._app) { | 20 LocationManager(this._app) { |
20 window.onPopState.listen(_onBrowserNavigation); | 21 window.onPopState.listen(_onBrowserNavigation); |
21 // Determine initial application path. | 22 // Determine initial application path. |
22 var applicationPath = '${window.location.hash}'; | 23 var applicationPath = '${window.location.hash}'; |
23 if ((window.location.hash == '') || (window.location.hash == '#')) { | 24 if ((window.location.hash == '') || (window.location.hash == '#')) { |
24 // Observatory has loaded but no application path has been specified, | 25 // Observatory has loaded but no application path has been specified, |
25 // use the default. | 26 // use the default. |
26 // By default we navigate to the VM page. | 27 // By default we navigate to the VM page. |
27 applicationPath = Uris.vm(); | 28 applicationPath = Uris.vm(); |
28 } | 29 } |
29 // Update current application path. | 30 // Update current application path. |
30 window.history.replaceState(applicationPath, | 31 window.history |
31 document.title, | 32 .replaceState(applicationPath, document.title, applicationPath); |
32 applicationPath); | |
33 _updateApplicationLocation(applicationPath); | 33 _updateApplicationLocation(applicationPath); |
34 } | 34 } |
35 | 35 |
36 bool getBoolParameter(String name, bool defaultValue) { | 36 bool getBoolParameter(String name, bool defaultValue) { |
37 var value = uri.queryParameters[name]; | 37 var value = uri.queryParameters[name]; |
38 if ("true" == value) return true; | 38 if ("true" == value) return true; |
39 if ("false" == value) return false; | 39 if ("false" == value) return false; |
40 return defaultValue; | 40 return defaultValue; |
41 } | 41 } |
42 | 42 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 _uri = Uri.parse(url); | 83 _uri = Uri.parse(url); |
84 } | 84 } |
85 | 85 |
86 /// Add [url] to the browser history. | 86 /// Add [url] to the browser history. |
87 _addToBrowserHistory(String url) { | 87 _addToBrowserHistory(String url) { |
88 window.history.pushState(url, document.title, url); | 88 window.history.pushState(url, document.title, url); |
89 } | 89 } |
90 | 90 |
91 /// Notify the current page that something has changed. | 91 /// Notify the current page that something has changed. |
92 _visit() { | 92 _visit() { |
93 runZoned(() => _app._visit(_uri, internalArguments), | 93 runZoned(() => _app._visit(_uri, internalArguments), onError: (e, st) { |
94 onError: (e, st) { | |
95 if (e is IsolateNotFound) { | 94 if (e is IsolateNotFound) { |
96 var newPath = ((_app.vm == null || _app.vm.isDisconnected) | 95 var newPath = ((_app.vm == null || _app.vm.isDisconnected) |
97 ? '/vm-connect' : '/isolate-reconnect'); | 96 ? '/vm-connect' |
| 97 : '/isolate-reconnect'); |
98 var parameters = {}; | 98 var parameters = {}; |
99 parameters.addAll(_uri.queryParameters); | 99 parameters.addAll(_uri.queryParameters); |
100 parameters['originalUri'] = _uri.toString(); | 100 parameters['originalUri'] = _uri.toString(); |
101 parameters['isolateId'] = parameters['isolateId']; | 101 parameters['isolateId'] = parameters['isolateId']; |
102 var generatedUri = new Uri(path: newPath, queryParameters: parameters); | 102 var generatedUri = new Uri(path: newPath, queryParameters: parameters); |
103 go(makeLink(generatedUri.toString()), true); | 103 go(makeLink(generatedUri.toString()), true); |
104 return; | 104 return; |
105 } | 105 } |
106 // Surface any uncaught exceptions. | 106 // Surface any uncaught exceptions. |
107 _app.handleException(e, st); | 107 _app.handleException(e, st); |
108 }); | 108 }); |
109 } | 109 } |
110 | 110 |
111 /// Navigate to [url]. | 111 /// Navigate to [url]. |
112 void go(String url, [bool addToBrowserHistory = true]) { | 112 void go(String url, [bool addToBrowserHistory = true]) { |
113 if (addToBrowserHistory) { | 113 if (addToBrowserHistory) { |
114 _addToBrowserHistory(url); | 114 _addToBrowserHistory(url); |
115 } | 115 } |
116 _updateApplicationLocation(url); | 116 _updateApplicationLocation(url); |
117 _visit(); | 117 _visit(); |
118 } | 118 } |
119 | 119 |
120 /// Starting with the current uri path and queryParameters, update | 120 /// Starting with the current uri path and queryParameters, update |
121 /// queryParameters present in [updateParameters], then generate a new uri | 121 /// queryParameters present in [updateParameters], then generate a new uri |
122 /// and navigate to that. | 122 /// and navigate to that. |
123 goReplacingParameters(Map updatedParameters, [bool addToBrowserHistory = true]
) { | 123 goReplacingParameters(Map updatedParameters, |
| 124 [bool addToBrowserHistory = true]) { |
124 go(makeLinkReplacingParameters(updatedParameters), addToBrowserHistory); | 125 go(makeLinkReplacingParameters(updatedParameters), addToBrowserHistory); |
125 } | 126 } |
126 | 127 |
127 makeLinkReplacingParameters(Map updatedParameters) { | 128 makeLinkReplacingParameters(Map updatedParameters) { |
128 var parameters = new Map.from(_uri.queryParameters); | 129 var parameters = new Map.from(_uri.queryParameters); |
129 updatedParameters.forEach((k, v) { | 130 updatedParameters.forEach((k, v) { |
130 parameters[k] = v; | 131 parameters[k] = v; |
131 }); | 132 }); |
132 // Ensure path starts with a slash. | 133 // Ensure path starts with a slash. |
133 var path = uri.path.startsWith('/') ? uri.path : '/${uri.path}'; | 134 var path = uri.path.startsWith('/') ? uri.path : '/${uri.path}'; |
134 var generatedUri = new Uri(path: path, queryParameters: parameters); | 135 var generatedUri = new Uri(path: path, queryParameters: parameters); |
135 return makeLink(generatedUri.toString()); | 136 return makeLink(generatedUri.toString()); |
136 } | 137 } |
137 | 138 |
138 goForwardingParameters(String newPath, [bool addToBrowserHistory = true]) { | 139 goForwardingParameters(String newPath, [bool addToBrowserHistory = true]) { |
139 go(makeLinkForwardingParameters(newPath), addToBrowserHistory); | 140 go(makeLinkForwardingParameters(newPath), addToBrowserHistory); |
140 } | 141 } |
141 | 142 |
142 makeLinkForwardingParameters(String newPath) { | 143 makeLinkForwardingParameters(String newPath) { |
143 var parameters = _uri.queryParameters; | 144 var parameters = _uri.queryParameters; |
144 var generatedUri = new Uri(path: newPath, queryParameters: parameters); | 145 var generatedUri = new Uri(path: newPath, queryParameters: parameters); |
145 return makeLink(generatedUri.toString()); | 146 return makeLink(generatedUri.toString()); |
146 } | 147 } |
147 | 148 |
148 /// Utility event handler when clicking on application url link. | 149 /// Utility event handler when clicking on application url link. |
149 void onGoto(MouseEvent event) { | 150 void onGoto(MouseEvent event) { |
150 if ((event.button > 0) || | 151 if ((event.button > 0) || |
151 event.metaKey || | 152 event.metaKey || |
152 event.ctrlKey || | 153 event.ctrlKey || |
153 event.shiftKey || | 154 event.shiftKey || |
154 event.altKey) { | 155 event.altKey) { |
155 // Mouse event is not a left-click OR | 156 // Mouse event is not a left-click OR |
156 // mouse event is a left-click with a modifier key: | 157 // mouse event is a left-click with a modifier key: |
157 // let browser handle. | 158 // let browser handle. |
158 return; | 159 return; |
159 } | 160 } |
160 event.preventDefault(); | 161 event.preventDefault(); |
161 // 'currentTarget' is the dom element that would process the event. | 162 // 'currentTarget' is the dom element that would process the event. |
162 // If we use 'target' we might get an <em> element or somesuch. | 163 // If we use 'target' we might get an <em> element or somesuch. |
163 var target = event.currentTarget; | 164 var target = event.currentTarget; |
164 go(target.attributes['href']); | 165 go(target.attributes['href']); |
165 } | 166 } |
166 } | 167 } |
OLD | NEW |