OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('uber', function() { | 5 cr.define('uber', function() { |
6 /** | 6 /** |
7 * Options for how web history should be handled. | 7 * Options for how web history should be handled. |
8 */ | 8 */ |
9 var HISTORY_STATE_OPTION = { | 9 var HISTORY_STATE_OPTION = { |
10 PUSH: 1, // Push a new history state. | 10 PUSH: 1, // Push a new history state. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 if (!params.id) | 93 if (!params.id) |
94 params.id = getDefaultIframe().id; | 94 params.id = getDefaultIframe().id; |
95 | 95 |
96 return params; | 96 return params; |
97 } | 97 } |
98 | 98 |
99 /** | 99 /** |
100 * Handler for window.onpopstate. | 100 * Handler for window.onpopstate. |
101 * @param {Event} e The history event. | 101 * @param {Event} e The history event. |
102 */ | 102 */ |
103 function onPopHistoryState(e) { | 103 function onPopHistoryState(e) { |
Dan Beam
2014/05/29 20:42:10
what happens in the case of !e.state? should we p
Dan Beam
2014/05/29 20:45:52
nevermind, I see what's happening here (and in ube
| |
104 if (e.state && e.state.pageId) { | 104 // Use the URL to determine which page to route to. |
105 var params = resolvePageInfo(); | 105 var params = resolvePageInfo(); |
106 assert(params.id === e.state.pageId); | |
107 | 106 |
108 // If the page isn't the current page, load it fresh. Even if the page is | 107 // If the page isn't the current page, load it fresh. Even if the page is |
109 // already loaded, it may have state not reflected in the URL, such as the | 108 // already loaded, it may have state not reflected in the URL, such as the |
110 // history page's "Remove selected items" overlay. http://crbug.com/377386 | 109 // history page's "Remove selected items" overlay. http://crbug.com/377386 |
111 if (getRequiredElement(params.id) !== getSelectedIframe()) | 110 if (getRequiredElement(params.id) !== getSelectedIframe()) |
112 showPage(params.id, HISTORY_STATE_OPTION.NONE, params.path); | 111 showPage(params.id, HISTORY_STATE_OPTION.NONE, params.path); |
113 | 112 |
114 // Either way, send the state down to it. | 113 // Either way, send the state down to it. |
115 // | 114 // |
116 // Note: This assumes that the state and path parameters for every page | 115 // Note: This assumes that the state and path parameters for every page |
117 // under this origin are compatible. All of the downstream pages which | 116 // under this origin are compatible. All of the downstream pages which |
118 // navigate use pushState and replaceState. | 117 // navigate use pushState and replaceState. |
119 invokeMethodOnPage(e.state.pageId, 'popState', | 118 invokeMethodOnPage(params.id, 'popState', |
120 {state: e.state.pageState, path: params.path}); | 119 {state: e.state, path: params.path}); |
121 } | |
122 } | 120 } |
123 | 121 |
124 /** | 122 /** |
125 * @return {Object} The default iframe container. | 123 * @return {Object} The default iframe container. |
126 */ | 124 */ |
127 function getDefaultIframe() { | 125 function getDefaultIframe() { |
128 return $(loadTimeData.getString('helpHost')); | 126 return $(loadTimeData.getString('helpHost')); |
129 } | 127 } |
130 | 128 |
131 /** | 129 /** |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 | 230 |
233 var histFunc; | 231 var histFunc; |
234 if (historyOption == HISTORY_STATE_OPTION.PUSH) | 232 if (historyOption == HISTORY_STATE_OPTION.PUSH) |
235 histFunc = window.history.pushState; | 233 histFunc = window.history.pushState; |
236 else if (historyOption == HISTORY_STATE_OPTION.REPLACE) | 234 else if (historyOption == HISTORY_STATE_OPTION.REPLACE) |
237 histFunc = window.history.replaceState; | 235 histFunc = window.history.replaceState; |
238 | 236 |
239 assert(histFunc, 'invalid historyOption given ' + historyOption); | 237 assert(histFunc, 'invalid historyOption given ' + historyOption); |
240 | 238 |
241 var pageId = getSelectedIframe().id; | 239 var pageId = getSelectedIframe().id; |
242 var args = [{pageId: pageId, pageState: state}, | 240 var args = [state, '', '/' + pageId + '/' + (path || '')]; |
243 '', | |
244 '/' + pageId + '/' + (path || '')]; | |
245 histFunc.apply(window.history, args); | 241 histFunc.apply(window.history, args); |
246 } | 242 } |
247 | 243 |
248 /** | 244 /** |
249 * Adds or replaces the current history entry based on a navigation from the | 245 * Adds or replaces the current history entry based on a navigation from the |
250 * source iframe. | 246 * source iframe. |
251 * @param {string} origin The origin of the source iframe. | 247 * @param {string} origin The origin of the source iframe. |
252 * @param {Object} state The source iframe's state object. | 248 * @param {Object} state The source iframe's state object. |
253 * @param {string} path The new "path" (e.g. "/createProfile"). | 249 * @param {string} path The new "path" (e.g. "/createProfile"). |
254 * @param {boolean} replace Whether to replace the current history entry. | 250 * @param {boolean} replace Whether to replace the current history entry. |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
450 } | 446 } |
451 | 447 |
452 return { | 448 return { |
453 onLoad: onLoad, | 449 onLoad: onLoad, |
454 onPopHistoryState: onPopHistoryState | 450 onPopHistoryState: onPopHistoryState |
455 }; | 451 }; |
456 }); | 452 }); |
457 | 453 |
458 window.addEventListener('popstate', uber.onPopHistoryState); | 454 window.addEventListener('popstate', uber.onPopHistoryState); |
459 document.addEventListener('DOMContentLoaded', uber.onLoad); | 455 document.addEventListener('DOMContentLoaded', uber.onLoad); |
OLD | NEW |