| 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 abstract class LocationManager extends Observable { | 7 abstract class LocationManager extends Observable { |
| 8 final _initialPath = '/vm'; | 8 final _initialPath = '/vm'; |
| 9 ObservatoryApplication _app; | 9 ObservatoryApplication _app; |
| 10 | 10 |
| 11 String _lastUrl; | 11 String _lastUrl; |
| 12 | 12 |
| 13 void _init(ObservatoryApplication app) { | 13 void _init(ObservatoryApplication app) { |
| 14 // Called once. | 14 // Called once. |
| 15 assert(_app == null); | 15 assert(_app == null); |
| 16 _app = app; | 16 _app = app; |
| 17 // Register for history events. | 17 // Register for history events. |
| 18 window.onPopState.listen(_onLocationChange); | 18 window.onPopState.listen(_onLocationChange); |
| 19 _onStartup(); | 19 _onStartup(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 void _onStartup(); | 22 void _onStartup(); |
| 23 void _onLocationChange(PopStateEvent event); | 23 void _onLocationChange(PopStateEvent event); |
| 24 | 24 |
| 25 /// Go to a specific url. | 25 void _pushUrl(String url) { |
| 26 void go(String url) { | |
| 27 if (_lastUrl != url) { | 26 if (_lastUrl != url) { |
| 28 Logger.root.info('Navigated to ${url}'); | 27 Logger.root.info('Navigated to ${url}'); |
| 29 window.history.pushState(url, document.title, url); | 28 window.history.pushState(url, document.title, url); |
| 30 _lastUrl = url; | 29 _lastUrl = url; |
| 31 } | 30 } |
| 31 } |
| 32 |
| 33 /// Go to a specific url. |
| 34 void go(String url) { |
| 35 if (_app.vm == null) { |
| 36 url = makeLink('/vm-connect/'); |
| 37 } |
| 38 _pushUrl(url); |
| 32 _go(url); | 39 _go(url); |
| 33 } | 40 } |
| 34 | 41 |
| 35 void _go(String url) { | 42 void _go(String url) { |
| 36 // Chop off leading '#'. | 43 // Chop off leading '#'. |
| 37 if (url.startsWith('#')) { | 44 if (url.startsWith('#')) { |
| 38 url = url.substring(1); | 45 url = url.substring(1); |
| 39 } | 46 } |
| 40 // Fall through handles '#/' | 47 // Fall through handles '#/' |
| 41 // Chop off leading '/'. | 48 // Chop off leading '/'. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 60 } | 67 } |
| 61 | 68 |
| 62 /// Go forward. | 69 /// Go forward. |
| 63 void forward() { | 70 void forward() { |
| 64 window.history.go(1); | 71 window.history.go(1); |
| 65 } | 72 } |
| 66 | 73 |
| 67 /// Handle clicking on an application url link. | 74 /// Handle clicking on an application url link. |
| 68 void onGoto(MouseEvent event, var detail, Element target) { | 75 void onGoto(MouseEvent event, var detail, Element target) { |
| 69 var href = target.attributes['href']; | 76 var href = target.attributes['href']; |
| 70 if (event.button > 1 || event.metaKey || event.ctrlKey || | 77 if (event.button > 0 || event.metaKey || event.ctrlKey || |
| 71 event.shiftKey || event.altKey) { | 78 event.shiftKey || event.altKey) { |
| 72 // Not a left-click or a left-click with a modifier key: | 79 // Not a left-click or a left-click with a modifier key: |
| 73 // Let browser handle. | 80 // Let browser handle. |
| 74 return; | 81 return; |
| 75 } | 82 } |
| 76 location.go(href); | 83 go(href); |
| 77 event.preventDefault(); | 84 event.preventDefault(); |
| 78 } | 85 } |
| 79 | 86 |
| 80 /// Given an application url, generate a link. | 87 /// Given an application url, generate a link. |
| 81 String makeLink(String url); | 88 String makeLink(String url); |
| 82 } | 89 } |
| 83 | 90 |
| 84 /// Uses location.hash to encode application urls. | 91 /// Uses location.hash to encode application urls. |
| 85 class HashLocationManager extends LocationManager { | 92 class HashLocationManager extends LocationManager { |
| 86 void _onStartup() { | 93 void _onStartup() { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 117 _go(window.location.pathname); | 124 _go(window.location.pathname); |
| 118 } | 125 } |
| 119 | 126 |
| 120 void _onLocationChange(PopStateEvent _) { | 127 void _onLocationChange(PopStateEvent _) { |
| 121 _go(window.location.pathname); | 128 _go(window.location.pathname); |
| 122 } | 129 } |
| 123 | 130 |
| 124 /// Given an application url, generate a link for an anchor tag. | 131 /// Given an application url, generate a link for an anchor tag. |
| 125 String makeLink(String url) => url; | 132 String makeLink(String url) => url; |
| 126 } | 133 } |
| OLD | NEW |