Chromium Code Reviews| 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 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 var getRouteForPath = function(path) { | 294 var getRouteForPath = function(path) { |
| 295 // TODO(tommycli): Use Object.values once Closure compilation supports it. | 295 // TODO(tommycli): Use Object.values once Closure compilation supports it. |
| 296 var matchingKey = Object.keys(Route).find(function(key) { | 296 var matchingKey = Object.keys(Route).find(function(key) { |
| 297 return Route[key].path == path; | 297 return Route[key].path == path; |
| 298 }); | 298 }); |
| 299 | 299 |
| 300 return Route[matchingKey] || null; | 300 return Route[matchingKey] || null; |
| 301 }; | 301 }; |
| 302 | 302 |
| 303 /** | 303 /** |
| 304 * The current active route. This updated only by settings.navigateTo. | 304 * The current active route. This updated only by settings.navigateTo. |
|
michaelpg
2016/08/12 22:18:02
"is updated"
"or initializeRouteFromURL"?
tommycli
2016/08/15 17:36:00
Done.
| |
| 305 * @private {!settings.Route} | 305 * @private {!settings.Route} |
| 306 */ | 306 */ |
| 307 var currentRoute_ = Route.BASIC; | 307 var currentRoute_ = Route.BASIC; |
| 308 | 308 |
| 309 /** | 309 /** |
| 310 * The current query parameters. This updated only by settings.navigateTo. | 310 * The current query parameters. This updated only by settings.navigateTo. |
|
michaelpg
2016/08/12 22:18:02
same
tommycli
2016/08/15 17:36:00
Done.
| |
| 311 * @private {!URLSearchParams} | 311 * @private {!URLSearchParams} |
| 312 */ | 312 */ |
| 313 var currentQueryParameters_ = new URLSearchParams(); | 313 var currentQueryParameters_ = new URLSearchParams(); |
| 314 | 314 |
| 315 // Initialize the route and query params from the URL. | 315 /** |
| 316 (function() { | 316 * Initialize the route and query params from the URL. |
| 317 */ | |
| 318 var initializeRouteFromUrl = function() { | |
| 317 var route = getRouteForPath(window.location.pathname); | 319 var route = getRouteForPath(window.location.pathname); |
|
michaelpg
2016/08/12 22:18:02
wonder if it's worth adding a check that we only c
tommycli
2016/08/15 17:36:00
Done.
| |
| 318 if (route) { | 320 if (route) { |
| 319 currentRoute_ = route; | 321 currentRoute_ = route; |
| 320 currentQueryParameters_ = new URLSearchParams(window.location.search); | 322 currentQueryParameters_ = new URLSearchParams(window.location.search); |
| 321 } else { | 323 } else { |
| 322 window.history.pushState(undefined, '', Route.BASIC.path); | 324 window.history.replaceState(undefined, '', Route.BASIC.path); |
| 323 } | 325 } |
| 324 })(); | 326 }; |
| 325 | 327 |
| 326 /** | 328 /** |
| 327 * Helper function to set the current route and notify all observers. | 329 * Helper function to set the current route and notify all observers. |
| 328 * @param {!settings.Route} route | 330 * @param {!settings.Route} route |
| 329 * @param {!URLSearchParams} queryParameters | 331 * @param {!URLSearchParams} queryParameters |
| 330 */ | 332 */ |
| 331 var setCurrentRoute = function(route, queryParameters) { | 333 var setCurrentRoute = function(route, queryParameters) { |
| 332 var oldRoute = currentRoute_; | 334 var oldRoute = currentRoute_; |
| 333 currentRoute_ = route; | 335 currentRoute_ = route; |
| 334 currentQueryParameters_ = queryParameters; | 336 currentQueryParameters_ = queryParameters; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 window.addEventListener('popstate', function(event) { | 374 window.addEventListener('popstate', function(event) { |
| 373 // On pop state, do not push the state onto the window.history again. | 375 // On pop state, do not push the state onto the window.history again. |
| 374 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC, | 376 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC, |
| 375 new URLSearchParams(window.location.search)); | 377 new URLSearchParams(window.location.search)); |
| 376 }); | 378 }); |
| 377 | 379 |
| 378 return { | 380 return { |
| 379 Route: Route, | 381 Route: Route, |
| 380 RouteObserverBehavior: RouteObserverBehavior, | 382 RouteObserverBehavior: RouteObserverBehavior, |
| 381 getRouteForPath: getRouteForPath, | 383 getRouteForPath: getRouteForPath, |
| 384 initializeRouteFromUrl: initializeRouteFromUrl, | |
| 382 getCurrentRoute: getCurrentRoute, | 385 getCurrentRoute: getCurrentRoute, |
| 383 getQueryParameters: getQueryParameters, | 386 getQueryParameters: getQueryParameters, |
| 384 navigateTo: navigateTo, | 387 navigateTo: navigateTo, |
| 385 }; | 388 }; |
| 386 }); | 389 }); |
| OLD | NEW |