| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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('settings', function() { | 5 cr.define('settings', function() { |
| 6 /** | 6 /** |
| 7 * Class for navigable routes. May only be instantiated within this file. | 7 * Class for navigable routes. May only be instantiated within this file. |
| 8 * @constructor | 8 * @constructor |
| 9 * @param {string} path | 9 * @param {string} path |
| 10 * @private | 10 * @private |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 var currentRoute = Route.BASIC; | 301 var currentRoute = Route.BASIC; |
| 302 | 302 |
| 303 /** | 303 /** |
| 304 * The current query parameters. This is updated only by settings.navigateTo | 304 * The current query parameters. This is updated only by settings.navigateTo |
| 305 * or settings.initializeRouteFromUrl. | 305 * or settings.initializeRouteFromUrl. |
| 306 * @private {!URLSearchParams} | 306 * @private {!URLSearchParams} |
| 307 */ | 307 */ |
| 308 var currentQueryParameters = new URLSearchParams(); | 308 var currentQueryParameters = new URLSearchParams(); |
| 309 | 309 |
| 310 /** @private {boolean} */ | 310 /** @private {boolean} */ |
| 311 var lastRouteChangeWasPopstate_ = false; | 311 var wasLastRouteChangePopstate = false; |
| 312 | 312 |
| 313 /** @private */ | 313 /** @private */ |
| 314 var initializeRouteFromUrlCalled = false; | 314 var initializeRouteFromUrlCalled = false; |
| 315 | 315 |
| 316 /** | 316 /** |
| 317 * Initialize the route and query params from the URL. | 317 * Initialize the route and query params from the URL. |
| 318 */ | 318 */ |
| 319 var initializeRouteFromUrl = function() { | 319 var initializeRouteFromUrl = function() { |
| 320 assert(!initializeRouteFromUrlCalled); | 320 assert(!initializeRouteFromUrlCalled); |
| 321 initializeRouteFromUrlCalled = true; | 321 initializeRouteFromUrlCalled = true; |
| 322 | 322 |
| 323 var route = getRouteForPath(window.location.pathname); | 323 var route = getRouteForPath(window.location.pathname); |
| 324 // Never allow direct navigation to ADVANCED. | 324 // Never allow direct navigation to ADVANCED. |
| 325 if (route && route != Route.ADVANCED) { | 325 if (route && route != Route.ADVANCED) { |
| 326 currentRoute = route; | 326 currentRoute = route; |
| 327 currentQueryParameters = new URLSearchParams(window.location.search); | 327 currentQueryParameters = new URLSearchParams(window.location.search); |
| 328 } else { | 328 } else { |
| 329 window.history.replaceState(undefined, '', Route.BASIC.path); | 329 window.history.replaceState(undefined, '', Route.BASIC.path); |
| 330 } | 330 } |
| 331 }; | 331 }; |
| 332 | 332 |
| 333 function resetRouteForTesting() { | 333 function resetRouteForTesting() { |
| 334 initializeRouteFromUrlCalled = false; | 334 initializeRouteFromUrlCalled = false; |
| 335 lastRouteChangeWasPopstate_ = false; | 335 wasLastRouteChangePopstate = false; |
| 336 currentRoute = Route.BASIC; | 336 currentRoute = Route.BASIC; |
| 337 currentQueryParameters = new URLSearchParams(); | 337 currentQueryParameters = new URLSearchParams(); |
| 338 } | 338 } |
| 339 | 339 |
| 340 /** | 340 /** |
| 341 * Helper function to set the current route and notify all observers. | 341 * Helper function to set the current route and notify all observers. |
| 342 * @param {!settings.Route} route | 342 * @param {!settings.Route} route |
| 343 * @param {!URLSearchParams} queryParameters | 343 * @param {!URLSearchParams} queryParameters |
| 344 * @param {boolean} isPopstate | 344 * @param {boolean} isPopstate |
| 345 */ | 345 */ |
| 346 var setCurrentRoute = function(route, queryParameters, isPopstate) { | 346 var setCurrentRoute = function(route, queryParameters, isPopstate) { |
| 347 var oldRoute = currentRoute; | 347 var oldRoute = currentRoute; |
| 348 currentRoute = route; | 348 currentRoute = route; |
| 349 currentQueryParameters = queryParameters; | 349 currentQueryParameters = queryParameters; |
| 350 lastRouteChangeWasPopstate_ = isPopstate; | 350 wasLastRouteChangePopstate = isPopstate; |
| 351 routeObservers.forEach(function(observer) { | 351 routeObservers.forEach(function(observer) { |
| 352 observer.currentRouteChanged(currentRoute, oldRoute); | 352 observer.currentRouteChanged(currentRoute, oldRoute); |
| 353 }); | 353 }); |
| 354 }; | 354 }; |
| 355 | 355 |
| 356 /** @return {!settings.Route} */ | 356 /** @return {!settings.Route} */ |
| 357 var getCurrentRoute = function() { return currentRoute; }; | 357 var getCurrentRoute = function() { return currentRoute; }; |
| 358 | 358 |
| 359 /** @return {!URLSearchParams} */ | 359 /** @return {!URLSearchParams} */ |
| 360 var getQueryParameters = function() { | 360 var getQueryParameters = function() { |
| 361 return new URLSearchParams(currentQueryParameters); // Defensive copy. | 361 return new URLSearchParams(currentQueryParameters); // Defensive copy. |
| 362 }; | 362 }; |
| 363 | 363 |
| 364 /** @return {boolean} */ | 364 /** @return {boolean} */ |
| 365 var lastRouteChangeWasPopstate = function() { | 365 var lastRouteChangeWasPopstate = function() { |
| 366 return lastRouteChangeWasPopstate_; | 366 return wasLastRouteChangePopstate; |
| 367 }; | 367 }; |
| 368 | 368 |
| 369 /** | 369 /** |
| 370 * Navigates to a canonical route and pushes a new history entry. | 370 * Navigates to a canonical route and pushes a new history entry. |
| 371 * @param {!settings.Route} route | 371 * @param {!settings.Route} route |
| 372 * @param {URLSearchParams=} opt_dynamicParameters Navigations to the same | 372 * @param {URLSearchParams=} opt_dynamicParameters Navigations to the same |
| 373 * URL parameters in a different order will still push to history. | 373 * URL parameters in a different order will still push to history. |
| 374 * @param {boolean=} opt_removeSearch Whether to strip the 'search' URL | 374 * @param {boolean=} opt_removeSearch Whether to strip the 'search' URL |
| 375 * parameter during navigation. Defaults to false. | 375 * parameter during navigation. Defaults to false. |
| 376 */ | 376 */ |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 getRouteForPath: getRouteForPath, | 427 getRouteForPath: getRouteForPath, |
| 428 initializeRouteFromUrl: initializeRouteFromUrl, | 428 initializeRouteFromUrl: initializeRouteFromUrl, |
| 429 resetRouteForTesting: resetRouteForTesting, | 429 resetRouteForTesting: resetRouteForTesting, |
| 430 getCurrentRoute: getCurrentRoute, | 430 getCurrentRoute: getCurrentRoute, |
| 431 getQueryParameters: getQueryParameters, | 431 getQueryParameters: getQueryParameters, |
| 432 lastRouteChangeWasPopstate: lastRouteChangeWasPopstate, | 432 lastRouteChangeWasPopstate: lastRouteChangeWasPopstate, |
| 433 navigateTo: navigateTo, | 433 navigateTo: navigateTo, |
| 434 navigateToPreviousRoute: navigateToPreviousRoute, | 434 navigateToPreviousRoute: navigateToPreviousRoute, |
| 435 }; | 435 }; |
| 436 }); | 436 }); |
| OLD | NEW |