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 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 may only be updated via the global |
| 296 * function settings.navigateTo. | 296 * function settings.navigateTo. |
| 297 * @private {!settings.Route} | 297 * @private {!settings.Route} |
| 298 */ | 298 */ |
| 299 var currentRoute_ = getRouteForPath(window.location.pathname) || Route.BASIC; | 299 var currentRoute_ = (function() { |
| 300 var route = getRouteForPath(window.location.pathname); | |
| 301 if (route) | |
| 302 return route; | |
| 303 | |
| 304 // Reset the URL path to '/' if the user navigates to a nonexistent URL. | |
| 305 window.history.replaceState(undefined, '', Route.BASIC.path); | |
| 306 return Route.BASIC; | |
| 307 })(); | |
|
Dan Beam
2016/08/02 00:11:04
how would this leak global variables?
var current
tommycli
2016/08/02 00:14:29
Yeah, it complains :D
| |
| 300 | 308 |
| 301 /** | 309 /** |
| 302 * Helper function to set the current route and notify all observers. | 310 * Helper function to set the current route and notify all observers. |
| 303 * @param {!settings.Route} route | 311 * @param {!settings.Route} route |
| 304 */ | 312 */ |
| 305 var setCurrentRoute = function(route) { | 313 var setCurrentRoute = function(route) { |
| 306 currentRoute_ = route; | 314 currentRoute_ = route; |
| 307 for (var observer of routeObservers_) | 315 for (var observer of routeObservers_) |
| 308 observer.currentRouteChanged(); | 316 observer.currentRouteChanged(); |
| 309 }; | 317 }; |
| 310 | 318 |
| 311 /** @return {!settings.Route} */ | 319 /** @return {!settings.Route} */ |
| 312 var getCurrentRoute = function() { return currentRoute_; }; | 320 var getCurrentRoute = function() { return currentRoute_; }; |
| 313 | 321 |
| 314 /** | 322 /** |
| 315 * Navigates to a canonical route and pushes a new history entry. | 323 * Navigates to a canonical route and pushes a new history entry. |
| 316 * @param {!settings.Route} route | 324 * @param {!settings.Route} route |
| 317 * @private | 325 * @private |
| 318 */ | 326 */ |
| 319 var navigateTo = function(route) { | 327 var navigateTo = function(route) { |
| 320 if (assert(route) == currentRoute_) | 328 if (assert(route) == currentRoute_) |
| 321 return; | 329 return; |
| 322 | 330 |
| 323 window.history.pushState(undefined, document.title, route.path); | 331 window.history.pushState(undefined, '', route.path); |
| 324 setCurrentRoute(route); | 332 setCurrentRoute(route); |
| 325 }; | 333 }; |
| 326 | 334 |
| 327 window.addEventListener('popstate', function(event) { | 335 window.addEventListener('popstate', function(event) { |
| 328 // On pop state, do not push the state onto the window.history again. | 336 // On pop state, do not push the state onto the window.history again. |
| 329 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC); | 337 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC); |
| 330 }); | 338 }); |
| 331 | 339 |
| 332 return { | 340 return { |
| 333 Route: Route, | 341 Route: Route, |
| 334 RouteObserverBehavior: RouteObserverBehavior, | 342 RouteObserverBehavior: RouteObserverBehavior, |
| 335 getRouteForPath: getRouteForPath, | 343 getRouteForPath: getRouteForPath, |
| 336 getCurrentRoute: getCurrentRoute, | 344 getCurrentRoute: getCurrentRoute, |
| 337 navigateTo: navigateTo, | 345 navigateTo: navigateTo, |
| 338 }; | 346 }; |
| 339 }); | 347 }); |
| OLD | NEW |