| 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 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 var getRouteForPath = function(path) { | 285 var getRouteForPath = function(path) { |
| 286 // TODO(tommycli): Use Object.values once Closure compilation supports it. | 286 // TODO(tommycli): Use Object.values once Closure compilation supports it. |
| 287 var matchingKey = Object.keys(Route).find(function(key) { | 287 var matchingKey = Object.keys(Route).find(function(key) { |
| 288 return Route[key].path == path; | 288 return Route[key].path == path; |
| 289 }); | 289 }); |
| 290 | 290 |
| 291 return Route[matchingKey] || null; | 291 return Route[matchingKey] || null; |
| 292 }; | 292 }; |
| 293 | 293 |
| 294 /** | 294 /** |
| 295 * The current active route. This may only be updated via the global | 295 * The current active route. This updated only by settings.navigateTo. |
| 296 * function settings.navigateTo. | |
| 297 * @private {!settings.Route} | 296 * @private {!settings.Route} |
| 298 */ | 297 */ |
| 299 var currentRoute_ = (function() { | 298 var currentRoute_ = Route.BASIC; |
| 299 |
| 300 /** |
| 301 * The current query parameters. This updated only by settings.navigateTo. |
| 302 * @private {!URLSearchParams} |
| 303 */ |
| 304 var currentQueryParameters_ = new URLSearchParams(); |
| 305 |
| 306 // Initialize the route and query params from the URL. |
| 307 (function() { |
| 300 var route = getRouteForPath(window.location.pathname); | 308 var route = getRouteForPath(window.location.pathname); |
| 301 if (route) | 309 if (route) { |
| 302 return route; | 310 currentRoute_ = route; |
| 303 | 311 currentQueryParameters_ = new URLSearchParams(window.location.search); |
| 304 // Reset the URL path to '/' if the user navigates to a nonexistent URL. | 312 } else { |
| 305 window.history.replaceState(undefined, '', Route.BASIC.path); | 313 window.history.pushState(undefined, '', Route.BASIC.path); |
| 306 return Route.BASIC; | 314 } |
| 307 })(); | 315 })(); |
| 308 | 316 |
| 309 /** | 317 /** |
| 310 * Helper function to set the current route and notify all observers. | 318 * Helper function to set the current route and notify all observers. |
| 311 * @param {!settings.Route} route | 319 * @param {!settings.Route} route |
| 320 * @param {!URLSearchParams} queryParameters |
| 312 */ | 321 */ |
| 313 var setCurrentRoute = function(route) { | 322 var setCurrentRoute = function(route, queryParameters) { |
| 314 currentRoute_ = route; | 323 currentRoute_ = route; |
| 324 currentQueryParameters_ = queryParameters; |
| 315 for (var observer of routeObservers_) | 325 for (var observer of routeObservers_) |
| 316 observer.currentRouteChanged(); | 326 observer.currentRouteChanged(); |
| 317 }; | 327 }; |
| 318 | 328 |
| 319 /** @return {!settings.Route} */ | 329 /** @return {!settings.Route} */ |
| 320 var getCurrentRoute = function() { return currentRoute_; }; | 330 var getCurrentRoute = function() { return currentRoute_; }; |
| 321 | 331 |
| 332 /** @return {!URLSearchParams} */ |
| 333 var getQueryParameters = function() { |
| 334 return new URLSearchParams(currentQueryParameters_); // Defensive copy. |
| 335 }; |
| 336 |
| 322 /** | 337 /** |
| 323 * Navigates to a canonical route and pushes a new history entry. | 338 * Navigates to a canonical route and pushes a new history entry. |
| 324 * @param {!settings.Route} route | 339 * @param {!settings.Route} route |
| 340 * @param {URLSearchParams=} opt_dynamicParameters Navigations to the same |
| 341 * search parameters in a different order will still push to history. |
| 325 * @private | 342 * @private |
| 326 */ | 343 */ |
| 327 var navigateTo = function(route) { | 344 var navigateTo = function(route, opt_dynamicParameters) { |
| 328 if (assert(route) == currentRoute_) | 345 var params = opt_dynamicParameters || new URLSearchParams(); |
| 346 if (assert(route) == currentRoute_ && |
| 347 params.toString() == currentQueryParameters_.toString()) { |
| 329 return; | 348 return; |
| 349 } |
| 330 | 350 |
| 331 window.history.pushState(undefined, '', route.path); | 351 var url = route.path; |
| 332 setCurrentRoute(route); | 352 if (opt_dynamicParameters) { |
| 353 var queryString = opt_dynamicParameters.toString(); |
| 354 if (queryString) |
| 355 url += '?' + queryString; |
| 356 } |
| 357 |
| 358 setCurrentRoute(route, params); |
| 359 window.history.pushState(undefined, '', url); |
| 333 }; | 360 }; |
| 334 | 361 |
| 335 window.addEventListener('popstate', function(event) { | 362 window.addEventListener('popstate', function(event) { |
| 336 // On pop state, do not push the state onto the window.history again. | 363 // On pop state, do not push the state onto the window.history again. |
| 337 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC); | 364 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC, |
| 365 new URLSearchParams(window.location.search)); |
| 338 }); | 366 }); |
| 339 | 367 |
| 340 return { | 368 return { |
| 341 Route: Route, | 369 Route: Route, |
| 342 RouteObserverBehavior: RouteObserverBehavior, | 370 RouteObserverBehavior: RouteObserverBehavior, |
| 343 getRouteForPath: getRouteForPath, | 371 getRouteForPath: getRouteForPath, |
| 344 getCurrentRoute: getCurrentRoute, | 372 getCurrentRoute: getCurrentRoute, |
| 373 getQueryParameters: getQueryParameters, |
| 345 navigateTo: navigateTo, | 374 navigateTo: navigateTo, |
| 346 }; | 375 }; |
| 347 }); | 376 }); |
| OLD | NEW |