| 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 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 var getRouteForPath = function(path) { | 256 var getRouteForPath = function(path) { |
| 257 // TODO(tommycli): Use Object.values once Closure compilation supports it. | 257 // TODO(tommycli): Use Object.values once Closure compilation supports it. |
| 258 var matchingKey = Object.keys(Route).find(function(key) { | 258 var matchingKey = Object.keys(Route).find(function(key) { |
| 259 return Route[key].path == path; | 259 return Route[key].path == path; |
| 260 }); | 260 }); |
| 261 | 261 |
| 262 return Route[matchingKey] || null; | 262 return Route[matchingKey] || null; |
| 263 }; | 263 }; |
| 264 | 264 |
| 265 /** | 265 /** |
| 266 * The current active route. This updated only by settings.navigateTo. | 266 * The current active route. This updated is only by settings.navigateTo or |
| 267 * settings.initializeRouteFromUrl. |
| 267 * @private {!settings.Route} | 268 * @private {!settings.Route} |
| 268 */ | 269 */ |
| 269 var currentRoute_ = Route.BASIC; | 270 var currentRoute_ = Route.BASIC; |
| 270 | 271 |
| 271 /** | 272 /** |
| 272 * The current query parameters. This updated only by settings.navigateTo. | 273 * The current query parameters. This is updated only by settings.navigateTo |
| 274 * or settings.initializeRouteFromUrl. |
| 273 * @private {!URLSearchParams} | 275 * @private {!URLSearchParams} |
| 274 */ | 276 */ |
| 275 var currentQueryParameters_ = new URLSearchParams(); | 277 var currentQueryParameters_ = new URLSearchParams(); |
| 276 | 278 |
| 277 // Initialize the route and query params from the URL. | 279 /** @private */ |
| 278 (function() { | 280 var initializeRouteFromUrlCalled_ = false; |
| 281 |
| 282 /** |
| 283 * Initialize the route and query params from the URL. |
| 284 */ |
| 285 var initializeRouteFromUrl = function() { |
| 286 assert(!initializeRouteFromUrlCalled_); |
| 287 initializeRouteFromUrlCalled_ = true; |
| 288 |
| 279 var route = getRouteForPath(window.location.pathname); | 289 var route = getRouteForPath(window.location.pathname); |
| 280 if (route) { | 290 if (route) { |
| 281 currentRoute_ = route; | 291 currentRoute_ = route; |
| 282 currentQueryParameters_ = new URLSearchParams(window.location.search); | 292 currentQueryParameters_ = new URLSearchParams(window.location.search); |
| 283 } else { | 293 } else { |
| 284 window.history.pushState(undefined, '', Route.BASIC.path); | 294 window.history.replaceState(undefined, '', Route.BASIC.path); |
| 285 } | 295 } |
| 286 })(); | 296 }; |
| 287 | 297 |
| 288 /** | 298 /** |
| 289 * Helper function to set the current route and notify all observers. | 299 * Helper function to set the current route and notify all observers. |
| 290 * @param {!settings.Route} route | 300 * @param {!settings.Route} route |
| 291 * @param {!URLSearchParams} queryParameters | 301 * @param {!URLSearchParams} queryParameters |
| 292 */ | 302 */ |
| 293 var setCurrentRoute = function(route, queryParameters) { | 303 var setCurrentRoute = function(route, queryParameters) { |
| 294 var oldRoute = currentRoute_; | 304 var oldRoute = currentRoute_; |
| 295 currentRoute_ = route; | 305 currentRoute_ = route; |
| 296 currentQueryParameters_ = queryParameters; | 306 currentQueryParameters_ = queryParameters; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 window.addEventListener('popstate', function(event) { | 346 window.addEventListener('popstate', function(event) { |
| 337 // On pop state, do not push the state onto the window.history again. | 347 // On pop state, do not push the state onto the window.history again. |
| 338 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC, | 348 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC, |
| 339 new URLSearchParams(window.location.search)); | 349 new URLSearchParams(window.location.search)); |
| 340 }); | 350 }); |
| 341 | 351 |
| 342 return { | 352 return { |
| 343 Route: Route, | 353 Route: Route, |
| 344 RouteObserverBehavior: RouteObserverBehavior, | 354 RouteObserverBehavior: RouteObserverBehavior, |
| 345 getRouteForPath: getRouteForPath, | 355 getRouteForPath: getRouteForPath, |
| 356 initializeRouteFromUrl: initializeRouteFromUrl, |
| 346 getCurrentRoute: getCurrentRoute, | 357 getCurrentRoute: getCurrentRoute, |
| 347 getQueryParameters: getQueryParameters, | 358 getQueryParameters: getQueryParameters, |
| 348 navigateTo: navigateTo, | 359 navigateTo: navigateTo, |
| 349 }; | 360 }; |
| 350 }); | 361 }); |
| OLD | NEW |