Chromium Code Reviews| 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 abstract class LocationManager extends Observable { |
| 8 /// URL in window.location. The text after the '#' is used as the request | 8 final _initialPath = '/vm'; |
| 9 /// string for the VM service. | 9 ObservatoryApplication _app; |
| 10 class LocationManager extends Observable { | |
| 11 static const String defaultHash = '#/vm'; | |
| 12 | 10 |
| 13 ObservatoryApplication _app; | 11 String _lastUrl; |
| 14 @observable String currentHash = ''; | |
| 15 | 12 |
| 16 void init() { | 13 void _init(ObservatoryApplication app) { |
| 17 window.onHashChange.listen((event) { | 14 // Called once. |
| 18 // Request the current anchor. | 15 assert(_app == null); |
| 19 requestCurrentHash(); | 16 _app = app; |
| 20 }); | 17 // Register for history events. |
| 21 | 18 window.onPopState.listen(_onLocationChange); |
| 22 if (window.location.hash == '') { | 19 _onStartup(); |
| 23 // Fresh start, load the vm page. | |
| 24 window.location.hash = defaultHash; | |
| 25 } else { | |
| 26 // The page is being reloaded. | |
| 27 requestCurrentHash(); | |
| 28 } | |
| 29 } | 20 } |
| 30 | 21 |
| 31 /// Clear the current hash. | 22 void _onStartup(); |
| 32 void clearCurrentHash() { | 23 void _onLocationChange(_); |
|
turnidge
2014/06/04 18:02:24
Do you know what the _ parameter is supposed to be
Cutch
2014/06/04 20:28:57
Done.
| |
| 33 window.location.hash = ''; | 24 |
| 25 /// Go to a specific url. | |
| 26 void go(String url) { | |
| 27 if (_lastUrl != url) { | |
| 28 Logger.root.info('Navigated to ${url}'); | |
| 29 window.history.pushState(url, document.title, url); | |
| 30 } | |
| 31 _lastUrl = url; | |
|
turnidge
2014/06/04 18:02:24
Could move this line inside above "if".
Cutch
2014/06/04 20:28:57
Done.
| |
| 32 _go(url); | |
| 34 } | 33 } |
| 35 | 34 |
| 36 /// Refresh the service object reference in the location entry. | 35 void _go(String url) { |
| 37 void requestCurrentHash() { | 36 // Chop off leading '#'. |
| 38 currentHash = window.location.hash; | 37 if (url.startsWith('#')) { |
| 39 if (!currentHash.startsWith('#/')) { | 38 url = url.substring(1); |
| 39 } | |
| 40 // Fall through handles '#/' | |
| 41 // Chop off leading '/'. | |
| 42 if (url.startsWith('/')) { | |
| 43 url = url.substring(1); | |
| 44 } | |
| 45 _app._visit(url); | |
| 46 } | |
| 47 | |
| 48 /// Go back. | |
| 49 void back() { | |
| 50 window.history.go(-1); | |
| 51 } | |
| 52 | |
| 53 /// Go forward. | |
| 54 void forward() { | |
| 55 window.history.go(1); | |
| 56 } | |
| 57 | |
| 58 /// Handle clicking on an application url link. | |
| 59 void onGoto(MouseEvent event, var detail, Element target) { | |
| 60 var href = target.attributes['href']; | |
| 61 if (event.button > 1 || event.metaKey || event.ctrlKey || | |
| 62 event.shiftKey || event.altKey) { | |
| 63 // Not a left-click or a left-click with a modifier key: | |
| 64 // Let browser handle. | |
| 40 return; | 65 return; |
| 41 } | 66 } |
| 42 var parts = currentHash.substring(2).split('#'); | 67 location.go(href); |
| 43 var location = parts[0]; | 68 event.preventDefault(); |
| 44 var args = (parts.length > 1 ? parts[1] : ''); | 69 } |
| 45 if (parts.length > 2) { | 70 |
| 46 Logger.root.warning('Found more than 2 #-characters in $currentHash'); | 71 /// Given an application url, generate a link. |
| 72 String makeLink(String url); | |
| 73 } | |
| 74 | |
| 75 /// Uses location.hash to encode application urls. | |
| 76 class HashLocationManager extends LocationManager { | |
| 77 void _onStartup() { | |
| 78 String initialPath = '/${window.location.hash}'; | |
| 79 if ((window.location.hash == '') || (window.location.hash == '#')) { | |
| 80 initialPath = '/#${_initialPath}'; | |
| 47 } | 81 } |
| 48 _app.vm.get(currentHash.substring(2)).then((obj) { | 82 window.history.replaceState(initialPath, document.title, initialPath); |
| 49 _app.response = obj; | 83 _go(window.location.hash); |
| 50 _app.args = args; | 84 } |
| 51 }); | 85 |
| 86 void _onLocationChange(_) { | |
| 87 _go(window.location.hash); | |
| 88 } | |
| 89 | |
| 90 /// Given an application url, generate a link for an anchor tag. | |
| 91 String makeLink(String url) { | |
| 92 return '#$url'; | |
| 52 } | 93 } |
| 53 } | 94 } |
| 95 | |
| 96 /// Uses location.pathname to encode application urls. Requires server side | |
| 97 /// rewriting to support copy and paste linking. pub serve makes this hard. | |
| 98 /// STATUS: Work in progress. | |
| 99 class LinkLocationManager extends LocationManager { | |
| 100 void _onStartup() { | |
| 101 Logger.root.warning('Using untested LinkLocationManager'); | |
| 102 String initialPath = window.location.pathname; | |
| 103 if ((window.location.pathname == '/index.html') || | |
| 104 (window.location.pathname == '/')) { | |
| 105 initialPath = '/vm'; | |
| 106 } | |
| 107 window.history.replaceState(initialPath, document.title, initialPath); | |
| 108 _go(window.location.pathname); | |
| 109 } | |
| 110 | |
| 111 void _onLocationChange(_) { | |
| 112 _go(window.location.pathname); | |
| 113 } | |
| 114 | |
| 115 /// Given an application url, generate a link for an anchor tag. | |
| 116 String makeLink(String url) => url; | |
| 117 } | |
| OLD | NEW |