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 /// The LocationManager class observes and parses the hash ('#') portion of the | 7 /// The LocationManager class observes and parses the hash ('#') portion of the |
8 /// URL in window.location. The text after the '#' is used as the request | 8 /// URL in window.location. The text after the '#' is used as the request |
9 /// string for the VM service. | 9 /// string for the VM service. |
10 class LocationManager extends Observable { | 10 class LocationManager extends Observable { |
11 static const String defaultHash = '#/vm'; | 11 static const String defaultHash = '#/vm'; |
12 | 12 |
13 ObservatoryApplication _app; | 13 ObservatoryApplication _app; |
14 @observable String currentHash = ''; | 14 @observable String currentHash = ''; |
15 | 15 |
16 void init() { | 16 void init() { |
17 window.onHashChange.listen((event) { | 17 window.onHashChange.listen((event) { |
18 if (setDefaultHash()) { | |
19 // We just triggered another onHashChange event. | |
20 return; | |
21 } | |
22 // Request the current anchor. | 18 // Request the current anchor. |
23 requestCurrentHash(); | 19 requestCurrentHash(); |
24 }); | 20 }); |
25 | 21 // Set the default hash. |
26 if (!setDefaultHash()) { | 22 window.location.hash = defaultHash; |
27 // An anchor was already present, trigger a request. | |
28 requestCurrentHash(); | |
29 } | |
30 } | 23 } |
31 | 24 |
32 /// If no anchor is set, set the default anchor and return true. | 25 /// Clear the current hash. |
33 /// Return false otherwise. | 26 void clearCurrentHash() { |
34 bool setDefaultHash() { | 27 window.location.hash = ''; |
35 currentHash = window.location.hash; | |
36 if (currentHash == '' || currentHash == '#') { | |
37 window.location.hash = defaultHash; | |
38 return true; | |
39 } | |
40 return false; | |
41 } | 28 } |
42 | 29 |
43 /// Refresh the service object reference in the location entry. | 30 /// Refresh the service object reference in the location entry. |
44 void requestCurrentHash() { | 31 void requestCurrentHash() { |
45 currentHash = window.location.hash; | 32 currentHash = window.location.hash; |
46 assert(currentHash.startsWith('#/')); | 33 if (!currentHash.startsWith('#/')) { |
47 | 34 return; |
| 35 } |
48 var parts = currentHash.substring(2).split('#'); | 36 var parts = currentHash.substring(2).split('#'); |
49 var location = parts[0]; | 37 var location = parts[0]; |
50 var args = (parts.length > 1 ? parts[1] : ''); | 38 var args = (parts.length > 1 ? parts[1] : ''); |
51 if (parts.length > 2) { | 39 if (parts.length > 2) { |
52 Logger.root.warning('Found more than 2 #-characters in $currentHash'); | 40 Logger.root.warning('Found more than 2 #-characters in $currentHash'); |
53 } | 41 } |
54 _app.vm.get(currentHash.substring(2)).then((obj) { | 42 _app.vm.get(currentHash.substring(2)).then((obj) { |
55 _app.response = obj; | 43 _app.response = obj; |
56 _app.args = args; | 44 _app.args = args; |
57 }); | 45 }); |
58 } | 46 } |
59 } | 47 } |
OLD | NEW |