| 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 (_app.vm == null) { | |
| 36 url = makeLink('/vm-connect/'); | |
| 37 } | |
| 38 _pushUrl(url); | |
| 39 _go(url); | |
| 40 } | |
| 41 | |
| 42 void _go(String url) { | |
| 43 // Chop off leading '#'. | |
| 44 if (url.startsWith('#')) { | |
| 45 url = url.substring(1); | |
| 46 } | |
| 47 // Fall through handles '#/' | |
| 48 // Chop off leading '/'. | |
| 49 if (url.startsWith('/')) { | |
| 50 url = url.substring(1); | |
| 51 } | |
| 52 var args; | |
| 53 // Parse out arguments. | |
| 54 if (url.contains('#')) { | |
| 55 var chunks = url.split('#'); | |
| 56 url = chunks[0]; | |
| 57 if ((chunks.length > 1) && (chunks[1] != '')) { | |
| 58 args = chunks[1]; | |
| 59 } | |
| 60 } | |
| 61 _app._visit(url, args); | |
| 62 } | |
| 63 | |
| 64 /// Go back. | |
| 65 void back() { | |
| 66 window.history.go(-1); | |
| 67 } | |
| 68 | |
| 69 /// Go forward. | |
| 70 void forward() { | |
| 71 window.history.go(1); | |
| 72 } | |
| 73 | |
| 74 /// Handle clicking on an application url link. | |
| 75 void onGoto(MouseEvent event, var detail, Element target) { | |
| 76 var href = target.attributes['href']; | |
| 77 if (event.button > 0 || event.metaKey || event.ctrlKey || | |
| 78 event.shiftKey || event.altKey) { | |
| 79 // Not a left-click or a left-click with a modifier key: | |
| 80 // Let browser handle. | |
| 81 return; | |
| 82 } | |
| 83 go(href); | |
| 84 event.preventDefault(); | |
| 85 } | |
| 86 | |
| 87 /// Given an application url, generate a link. | |
| 88 String makeLink(String url); | |
| 89 } | |
| 90 | |
| 91 /// Uses location.hash to encode application urls. | |
| 92 class HashLocationManager extends LocationManager { | |
| 93 void _onStartup() { | |
| 94 String initialPath = '${window.location.hash}'; | |
| 95 if ((window.location.hash == '') || (window.location.hash == '#')) { | |
| 96 initialPath = '#${_initialPath}'; | |
| 97 } | |
| 98 window.history.pushState(initialPath, document.title, initialPath); | |
| 99 _go(window.location.hash); | |
| 100 } | |
| 101 | |
| 102 void _onLocationChange(PopStateEvent _) { | |
| 103 _go(window.location.hash); | |
| 104 } | |
| 105 | |
| 106 /// Given an application url, generate a link for an anchor tag. | |
| 107 String makeLink(String url) { | |
| 108 return '#$url'; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 /// Uses location.pathname to encode application urls. Requires server side | |
| 113 /// rewriting to support copy and paste linking. pub serve makes this hard. | |
| 114 /// STATUS: Work in progress. | |
| 115 class LinkLocationManager extends LocationManager { | |
| 116 void _onStartup() { | |
| 117 Logger.root.warning('Using untested LinkLocationManager'); | |
| 118 String initialPath = window.location.pathname; | |
| 119 if ((window.location.pathname == '/index.html') || | |
| 120 (window.location.pathname == '/')) { | |
| 121 initialPath = '/vm'; | |
| 122 } | |
| 123 window.history.replaceState(initialPath, document.title, initialPath); | |
| 124 _go(window.location.pathname); | |
| 125 } | |
| 126 | |
| 127 void _onLocationChange(PopStateEvent _) { | |
| 128 _go(window.location.pathname); | |
| 129 } | |
| 130 | |
| 131 /// Given an application url, generate a link for an anchor tag. | |
| 132 String makeLink(String url) => url; | |
| 133 } | |
| OLD | NEW |