| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of app; | |
| 6 | |
| 7 abstract class LocationManager extends Observable { | |
| 8 final _initialPath = '/vm'; | |
| 9 ObservatoryApplication _app; | |
| 10 | |
| 11 String _lastUrl; | |
| 12 | |
| 13 void _init(ObservatoryApplication app) { | |
| 14 // Called once. | |
| 15 assert(_app == null); | |
| 16 _app = app; | |
| 17 // Register for history events. | |
| 18 window.onPopState.listen(_onLocationChange); | |
| 19 _onStartup(); | |
| 20 } | |
| 21 | |
| 22 void _onStartup(); | |
| 23 void _onLocationChange(PopStateEvent event); | |
| 24 | |
| 25 void _pushUrl(String url) { | |
| 26 if (_lastUrl != url) { | |
| 27 Logger.root.info('Navigated to ${url}'); | |
| 28 window.history.pushState(url, document.title, url); | |
| 29 _lastUrl = url; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 /// Go to a specific url. | |
| 34 void go(String url) { | |
| 35 if ((url != makeLink('/vm-connect/')) && _app.vm == null) { | |
| 36 if (!window.confirm('Connection with VM has been lost. ' | |
| 37 'Proceeding will lose current page.')) { | |
| 38 return; | |
| 39 } | |
| 40 url = makeLink('/vm-connect/'); | |
| 41 } | |
| 42 _pushUrl(url); | |
| 43 _go(url); | |
| 44 } | |
| 45 | |
| 46 void _go(String url) { | |
| 47 // Chop off leading '#'. | |
| 48 if (url.startsWith('#')) { | |
| 49 url = url.substring(1); | |
| 50 } | |
| 51 // Fall through handles '#/' | |
| 52 // Chop off leading '/'. | |
| 53 if (url.startsWith('/')) { | |
| 54 url = url.substring(1); | |
| 55 } | |
| 56 var args; | |
| 57 // Parse out arguments. | |
| 58 if (url.contains('---')) { | |
| 59 var chunks = url.split('---'); | |
| 60 url = chunks[0]; | |
| 61 if ((chunks.length > 1) && (chunks[1] != '')) { | |
| 62 args = chunks[1]; | |
| 63 } | |
| 64 } | |
| 65 _app._visit(url, args); | |
| 66 } | |
| 67 | |
| 68 /// Go back. | |
| 69 void back() { | |
| 70 window.history.go(-1); | |
| 71 } | |
| 72 | |
| 73 /// Go forward. | |
| 74 void forward() { | |
| 75 window.history.go(1); | |
| 76 } | |
| 77 | |
| 78 /// Handle clicking on an application url link. | |
| 79 void onGoto(MouseEvent event, var detail, Element target) { | |
| 80 var href = target.attributes['href']; | |
| 81 if (event.button > 0 || event.metaKey || event.ctrlKey || | |
| 82 event.shiftKey || event.altKey) { | |
| 83 // Not a left-click or a left-click with a modifier key: | |
| 84 // Let browser handle. | |
| 85 return; | |
| 86 } | |
| 87 go(href); | |
| 88 event.preventDefault(); | |
| 89 } | |
| 90 | |
| 91 /// Given an application url, generate a link. | |
| 92 String makeLink(String url); | |
| 93 } | |
| 94 | |
| 95 /// Uses location.hash to encode application urls. | |
| 96 class HashLocationManager extends LocationManager { | |
| 97 void _onStartup() { | |
| 98 String initialPath = '${window.location.hash}'; | |
| 99 if ((window.location.hash == '') || (window.location.hash == '#')) { | |
| 100 initialPath = '#${_initialPath}'; | |
| 101 } | |
| 102 window.history.pushState(initialPath, document.title, initialPath); | |
| 103 _go(window.location.hash); | |
| 104 } | |
| 105 | |
| 106 void _onLocationChange(PopStateEvent _) { | |
| 107 _go(window.location.hash); | |
| 108 } | |
| 109 | |
| 110 /// Given an application url, generate a link for an anchor tag. | |
| 111 String makeLink(String url) { | |
| 112 return '#$url'; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 /// Uses location.pathname to encode application urls. Requires server side | |
| 117 /// rewriting to support copy and paste linking. pub serve makes this hard. | |
| 118 /// STATUS: Work in progress. | |
| 119 class LinkLocationManager extends LocationManager { | |
| 120 void _onStartup() { | |
| 121 Logger.root.warning('Using untested LinkLocationManager'); | |
| 122 String initialPath = window.location.pathname; | |
| 123 if ((window.location.pathname == '/index.html') || | |
| 124 (window.location.pathname == '/')) { | |
| 125 initialPath = '/vm'; | |
| 126 } | |
| 127 window.history.replaceState(initialPath, document.title, initialPath); | |
| 128 _go(window.location.pathname); | |
| 129 } | |
| 130 | |
| 131 void _onLocationChange(PopStateEvent _) { | |
| 132 _go(window.location.pathname); | |
| 133 } | |
| 134 | |
| 135 /// Given an application url, generate a link for an anchor tag. | |
| 136 String makeLink(String url) => url; | |
| 137 } | |
| OLD | NEW |