OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 swarmlib; | 5 part of swarmlib; |
6 | 6 |
7 /** | 7 /** |
8 * The base class for UI state that intends to support browser history. | 8 * The base class for UI state that intends to support browser history. |
9 */ | 9 */ |
10 abstract class UIState { | 10 abstract class UIState { |
(...skipping 17 matching lines...) Expand all Loading... |
28 } | 28 } |
29 | 29 |
30 if (firstEvent && state != '') { | 30 if (firstEvent && state != '') { |
31 // TODO(jmesserly): When loading a bookmark or refreshing, we replace | 31 // TODO(jmesserly): When loading a bookmark or refreshing, we replace |
32 // the app state with a clean app state so the back button works. It | 32 // the app state with a clean app state so the back button works. It |
33 // would be better to support jumping to the previous story. | 33 // would be better to support jumping to the previous story. |
34 // We'd need to do some history manipulation here and some fixes to | 34 // We'd need to do some history manipulation here and some fixes to |
35 // the views for this. | 35 // the views for this. |
36 window.history.replaceState(null, document.title, '#'); | 36 window.history.replaceState(null, document.title, '#'); |
37 } else if (state != '') { | 37 } else if (state != '') { |
38 loadFromHistory(JSON.parse(state)); | 38 loadFromHistory(json.parse(state)); |
39 } | 39 } |
40 firstEvent = false; | 40 firstEvent = false; |
41 }); | 41 }); |
42 | 42 |
43 window.on.popState.add(_historyTracking); | 43 window.on.popState.add(_historyTracking); |
44 } | 44 } |
45 | 45 |
46 void stopHistoryTracking() { | 46 void stopHistoryTracking() { |
47 if (_historyTracking != null) { | 47 if (_historyTracking != null) { |
48 window.on.popState.add(_historyTracking); // remove? | 48 window.on.popState.add(_historyTracking); // remove? |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 /** Pushes a state onto the browser history stack */ | 52 /** Pushes a state onto the browser history stack */ |
53 void pushToHistory() { | 53 void pushToHistory() { |
54 if (_historyTracking == null) { | 54 if (_historyTracking == null) { |
55 throw 'history tracking not started'; | 55 throw 'history tracking not started'; |
56 } | 56 } |
57 | 57 |
58 String state = JSON.stringify(toHistory()); | 58 String state = json.stringify(toHistory()); |
59 | 59 |
60 // TODO(jmesserly): [state] should be an Object, and we should pass it to | 60 // TODO(jmesserly): [state] should be an Object, and we should pass it to |
61 // the state parameter instead of as a #hash URL. Right now we're working | 61 // the state parameter instead of as a #hash URL. Right now we're working |
62 // around b/4582542. | 62 // around b/4582542. |
63 window.history.pushState(null, '${document.title}#$state'); | 63 window.history.pushState(null, '${document.title}#$state'); |
64 } | 64 } |
65 | 65 |
66 /** | 66 /** |
67 * Serialize the state to a form suitable for storing in browser history. | 67 * Serialize the state to a form suitable for storing in browser history. |
68 */ | 68 */ |
69 Map<String, String> toHistory(); | 69 Map<String, String> toHistory(); |
70 | 70 |
71 /** | 71 /** |
72 * Load the UI state from the given [values]. | 72 * Load the UI state from the given [values]. |
73 */ | 73 */ |
74 void loadFromHistory(Map<String, String> values); | 74 void loadFromHistory(Map<String, String> values); |
75 } | 75 } |
OLD | NEW |