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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 * @return {!HTMLElement} The frame associated to |origin| or null. | 217 * @return {!HTMLElement} The frame associated to |origin| or null. |
218 */ | 218 */ |
219 function getIframeFromOrigin(origin) { | 219 function getIframeFromOrigin(origin) { |
220 assert(origin.substr(-1) != '/', 'invalid origin given'); | 220 assert(origin.substr(-1) != '/', 'invalid origin given'); |
221 var query = '.iframe-container > iframe[src^="' + origin + '/"]'; | 221 var query = '.iframe-container > iframe[src^="' + origin + '/"]'; |
222 return document.querySelector(query); | 222 return document.querySelector(query); |
223 } | 223 } |
224 | 224 |
225 /** | 225 /** |
226 * Changes the path past the page title (i.e. chrome://chrome/settings/(.*)). | 226 * Changes the path past the page title (i.e. chrome://chrome/settings/(.*)). |
| 227 * @param {string} pageId Should match an id of one of the iframe containers. |
227 * @param {Object} state The page's state object for the navigation. | 228 * @param {Object} state The page's state object for the navigation. |
228 * @param {string} path The new /path/ to be set after the page name. | 229 * @param {string} path The new /path/ to be set after the page name. |
229 * @param {number} historyOption The type of history modification to make. | 230 * @param {number} historyOption The type of history modification to make. |
230 */ | 231 */ |
231 function changePathTo(state, path, historyOption) { | 232 function changePathTo(pageId, state, path, historyOption) { |
232 assert(!path || path.substr(-1) != '/', 'invalid path given'); | 233 assert(!path || path.substr(-1) != '/', 'invalid path given'); |
233 | 234 |
234 var histFunc; | 235 var histFunc; |
235 if (historyOption == HISTORY_STATE_OPTION.PUSH) | 236 if (historyOption == HISTORY_STATE_OPTION.PUSH) |
236 histFunc = window.history.pushState; | 237 histFunc = window.history.pushState; |
237 else if (historyOption == HISTORY_STATE_OPTION.REPLACE) | 238 else if (historyOption == HISTORY_STATE_OPTION.REPLACE) |
238 histFunc = window.history.replaceState; | 239 histFunc = window.history.replaceState; |
239 | 240 |
240 assert(histFunc, 'invalid historyOption given ' + historyOption); | 241 assert(histFunc, 'invalid historyOption given ' + historyOption); |
241 | 242 |
242 var pageId = getSelectedIframe().id; | |
243 var args = [{pageId: pageId, pageState: state}, | 243 var args = [{pageId: pageId, pageState: state}, |
244 '', | 244 '', |
245 '/' + pageId + '/' + (path || '')]; | 245 '/' + pageId + '/' + (path || '')]; |
246 histFunc.apply(window.history, args); | 246 histFunc.apply(window.history, args); |
247 } | 247 } |
248 | 248 |
249 /** | 249 /** |
250 * Adds or replaces the current history entry based on a navigation from the | 250 * Adds or replaces the current history entry based on a navigation from the |
251 * source iframe. | 251 * source iframe. |
252 * @param {string} origin The origin of the source iframe. | 252 * @param {string} origin The origin of the source iframe. |
253 * @param {Object} state The source iframe's state object. | 253 * @param {Object} state The source iframe's state object. |
254 * @param {string} path The new "path" (e.g. "/createProfile"). | 254 * @param {string} path The new "path" (e.g. "/createProfile"). |
255 * @param {boolean} replace Whether to replace the current history entry. | 255 * @param {boolean} replace Whether to replace the current history entry. |
256 */ | 256 */ |
257 function updateHistory(origin, state, path, replace) { | 257 function updateHistory(origin, state, path, replace) { |
258 assert(!path || path[0] != '/', 'invalid path sent from ' + origin); | 258 assert(!path || path[0] != '/', 'invalid path sent from ' + origin); |
259 var historyOption = | 259 var historyOption = |
260 replace ? HISTORY_STATE_OPTION.REPLACE : HISTORY_STATE_OPTION.PUSH; | 260 replace ? HISTORY_STATE_OPTION.REPLACE : HISTORY_STATE_OPTION.PUSH; |
261 // Only update the currently displayed path if this is the visible frame. | 261 // Only update the currently displayed path if this is the visible frame. |
262 if (getIframeFromOrigin(origin).parentNode == getSelectedIframe()) | 262 var container = getIframeFromOrigin(origin).parentNode; |
263 changePathTo(state, path, historyOption); | 263 if (container == getSelectedIframe()) |
| 264 changePathTo(container.id, state, path, historyOption); |
264 } | 265 } |
265 | 266 |
266 /** | 267 /** |
267 * Sets the title of the page. | 268 * Sets the title of the page. |
268 * @param {string} origin The origin of the source iframe. | 269 * @param {string} origin The origin of the source iframe. |
269 * @param {string} title The title of the page. | 270 * @param {string} title The title of the page. |
270 */ | 271 */ |
271 function setTitle(origin, title) { | 272 function setTitle(origin, title) { |
272 // Cache the title for the client iframe, i.e., the iframe setting the | 273 // Cache the title for the client iframe, i.e., the iframe setting the |
273 // title. querySelector returns the actual iframe element, so use parentNode | 274 // title. querySelector returns the actual iframe element, so use parentNode |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 container.appendChild(frame); | 334 container.appendChild(frame); |
334 frame.src = sourceUrl; | 335 frame.src = sourceUrl; |
335 } else { | 336 } else { |
336 // There's no particularly good way to know what the current URL of the | 337 // There's no particularly good way to know what the current URL of the |
337 // content frame is as we don't have access to its contentWindow's | 338 // content frame is as we don't have access to its contentWindow's |
338 // location, so just replace every time until necessary to do otherwise. | 339 // location, so just replace every time until necessary to do otherwise. |
339 frame.contentWindow.location.replace(sourceUrl); | 340 frame.contentWindow.location.replace(sourceUrl); |
340 frame.dataset.ready = false; | 341 frame.dataset.ready = false; |
341 } | 342 } |
342 | 343 |
| 344 // This must be called before selectPage so that the title change applies to |
| 345 // the new history entry. |
| 346 if (historyOption != HISTORY_STATE_OPTION.NONE) |
| 347 changePathTo(pageId, {}, path, historyOption); |
| 348 |
343 selectPage(pageId); | 349 selectPage(pageId); |
344 | |
345 if (historyOption != HISTORY_STATE_OPTION.NONE) | |
346 changePathTo({}, path, historyOption); | |
347 } | 350 } |
348 | 351 |
349 /** | 352 /** |
350 * Switches to a subpage. The subpage must already exist. | 353 * Switches to a subpage. The subpage must already exist. |
351 * @param {string} pageId Should match an id of one of the iframe containers. | 354 * @param {string} pageId Should match an id of one of the iframe containers. |
352 */ | 355 */ |
353 function selectPage(pageId) { | 356 function selectPage(pageId) { |
354 var container = getRequiredElement(pageId); | 357 var container = getRequiredElement(pageId); |
355 var lastSelected = document.querySelector('.iframe-container.selected'); | 358 var lastSelected = document.querySelector('.iframe-container.selected'); |
356 | 359 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 } | 463 } |
461 | 464 |
462 return { | 465 return { |
463 onLoad: onLoad, | 466 onLoad: onLoad, |
464 onPopHistoryState: onPopHistoryState | 467 onPopHistoryState: onPopHistoryState |
465 }; | 468 }; |
466 }); | 469 }); |
467 | 470 |
468 window.addEventListener('popstate', uber.onPopHistoryState); | 471 window.addEventListener('popstate', uber.onPopHistoryState); |
469 document.addEventListener('DOMContentLoaded', uber.onLoad); | 472 document.addEventListener('DOMContentLoaded', uber.onLoad); |
OLD | NEW |