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 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 * @param {!settings.Route|undefined} opt_newRoute | 231 * @param {!settings.Route|undefined} opt_newRoute |
| 232 * @param {!settings.Route|undefined} opt_oldRoute | 232 * @param {!settings.Route|undefined} opt_oldRoute |
| 233 * @abstract | 233 * @abstract |
| 234 */ | 234 */ |
| 235 currentRouteChanged: function(opt_newRoute, opt_oldRoute) { | 235 currentRouteChanged: function(opt_newRoute, opt_oldRoute) { |
| 236 assertNotReached(); | 236 assertNotReached(); |
| 237 }, | 237 }, |
| 238 }; | 238 }; |
| 239 | 239 |
| 240 /** | 240 /** |
| 241 * Returns the matching canonical route, or null if none matches. | |
| 242 * @param {string} path | 241 * @param {string} path |
| 243 * @return {?settings.Route} | 242 * @return {?settings.Route} The matching canonical route, or null if none |
| 243 * matches. | |
| 244 */ | 244 */ |
| 245 var getRouteForPath = function(path) { | 245 var getRouteForPath = function(path) { |
| 246 // TODO(tommycli): Use Object.values once Closure compilation supports it. | 246 // TODO(tommycli): Use Object.values once Closure compilation supports it. |
| 247 var matchingKey = Object.keys(Route).find(function(key) { | 247 var matchingKey = Object.keys(Route).find(function(key) { |
| 248 return Route[key].path == path; | 248 // Allow trailing slash in paths. |
| 249 return Route[key].path == (path == '/' ? path : path.replace(/\/$/, '')); | |
|
dpapad
2016/11/23 22:57:20
As I am adding tests, I realized that getRouteForP
| |
| 249 }); | 250 }); |
| 250 | 251 |
| 251 return Route[matchingKey] || null; | 252 return !!matchingKey ? Route[matchingKey] : null; |
|
tommycli
2016/11/23 22:33:46
I assume doing Route[undefined] is bad? (when ther
dpapad
2016/11/23 22:57:20
Yes. The effective behavior was the same, a fals-y
| |
| 252 }; | 253 }; |
| 253 | 254 |
| 254 /** | 255 /** |
| 255 * The current active route. This updated is only by settings.navigateTo or | 256 * The current active route. This updated is only by settings.navigateTo or |
| 256 * settings.initializeRouteFromUrl. | 257 * settings.initializeRouteFromUrl. |
| 257 * @private {!settings.Route} | 258 * @private {!settings.Route} |
| 258 */ | 259 */ |
| 259 var currentRoute_ = Route.BASIC; | 260 var currentRoute_ = Route.BASIC; |
| 260 | 261 |
| 261 /** | 262 /** |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 275 * Initialize the route and query params from the URL. | 276 * Initialize the route and query params from the URL. |
| 276 */ | 277 */ |
| 277 var initializeRouteFromUrl = function() { | 278 var initializeRouteFromUrl = function() { |
| 278 assert(!initializeRouteFromUrlCalled_); | 279 assert(!initializeRouteFromUrlCalled_); |
| 279 initializeRouteFromUrlCalled_ = true; | 280 initializeRouteFromUrlCalled_ = true; |
| 280 | 281 |
| 281 var route = getRouteForPath(window.location.pathname); | 282 var route = getRouteForPath(window.location.pathname); |
| 282 // Never allow direct navigation to ADVANCED. | 283 // Never allow direct navigation to ADVANCED. |
| 283 if (route && route != Route.ADVANCED) { | 284 if (route && route != Route.ADVANCED) { |
| 284 currentRoute_ = route; | 285 currentRoute_ = route; |
| 285 currentQueryParameters_ = new URLSearchParams(window.location.search); | 286 currentQueryParameters_ = new URLSearchParams(window.location.search); |
|
tommycli
2016/11/23 22:33:46
Should we use replaceState here to canonicalize th
dpapad
2016/11/23 23:55:04
Leaving this for a separate CL, so that I can ensu
| |
| 286 } else { | 287 } else { |
| 287 window.history.replaceState(undefined, '', Route.BASIC.path); | 288 window.history.replaceState(undefined, '', Route.BASIC.path); |
| 288 } | 289 } |
| 289 }; | 290 }; |
| 290 | 291 |
| 291 /** | 292 /** |
| 292 * Helper function to set the current route and notify all observers. | 293 * Helper function to set the current route and notify all observers. |
| 293 * @param {!settings.Route} route | 294 * @param {!settings.Route} route |
| 294 * @param {!URLSearchParams} queryParameters | 295 * @param {!URLSearchParams} queryParameters |
| 295 * @param {boolean} isPopstate | 296 * @param {boolean} isPopstate |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 RouteObserverBehavior: RouteObserverBehavior, | 372 RouteObserverBehavior: RouteObserverBehavior, |
| 372 getRouteForPath: getRouteForPath, | 373 getRouteForPath: getRouteForPath, |
| 373 initializeRouteFromUrl: initializeRouteFromUrl, | 374 initializeRouteFromUrl: initializeRouteFromUrl, |
| 374 getCurrentRoute: getCurrentRoute, | 375 getCurrentRoute: getCurrentRoute, |
| 375 getQueryParameters: getQueryParameters, | 376 getQueryParameters: getQueryParameters, |
| 376 lastRouteChangeWasPopstate: lastRouteChangeWasPopstate, | 377 lastRouteChangeWasPopstate: lastRouteChangeWasPopstate, |
| 377 navigateTo: navigateTo, | 378 navigateTo: navigateTo, |
| 378 navigateToPreviousRoute: navigateToPreviousRoute, | 379 navigateToPreviousRoute: navigateToPreviousRoute, |
| 379 }; | 380 }; |
| 380 }); | 381 }); |
| OLD | NEW |