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 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 r.RESET = r.ADVANCED.createSection('/reset', 'reset'); | 256 r.RESET = r.ADVANCED.createSection('/reset', 'reset'); |
| 257 | 257 |
| 258 <if expr="chromeos"> | 258 <if expr="chromeos"> |
| 259 r.INPUT_METHODS = | 259 r.INPUT_METHODS = |
| 260 r.LANGUAGES.createChild('/inputMethods', 'manage-input-methods'); | 260 r.LANGUAGES.createChild('/inputMethods', 'manage-input-methods'); |
| 261 r.DETAILED_BUILD_INFO = | 261 r.DETAILED_BUILD_INFO = |
| 262 r.ABOUT.createChild('/help/details', 'detailed-build-info'); | 262 r.ABOUT.createChild('/help/details', 'detailed-build-info'); |
| 263 r.DETAILED_BUILD_INFO.section = 'about'; | 263 r.DETAILED_BUILD_INFO.section = 'about'; |
| 264 </if> | 264 </if> |
| 265 | 265 |
| 266 var routeObservers_ = new Set(); | |
| 267 | |
| 268 /** @polymerBehavior */ | |
| 269 var RouteObserverBehavior = { | |
| 270 /** @override */ | |
| 271 attached: function() { | |
| 272 assert(!routeObservers_.has(this)); | |
| 273 routeObservers_.add(this); | |
| 274 }, | |
| 275 | |
| 276 /** @override */ | |
| 277 detached: function() { | |
| 278 assert(routeObservers_.has(this)); | |
| 279 routeObservers_.delete(this); | |
|
Dan Beam
2016/07/30 01:13:02
assert(routeObservers_.delete(this));
tommycli
2016/08/01 17:33:24
Done.
| |
| 280 }, | |
| 281 | |
|
Dan Beam
2016/07/30 01:13:02
nit: /** @abstract */
tommycli
2016/08/01 17:33:24
Done. Ideally I could use @override in settings-ro
| |
| 282 currentRouteChanged_: function() { | |
|
Dan Beam
2016/07/30 01:13:02
this should not be named with _, as that is for @p
tommycli
2016/08/01 17:33:24
Done.
| |
| 283 assertNotReached('Elements that mixin RouteObserverBehavior should ' + | |
| 284 'override currentRouteChanged_.'); | |
|
Dan Beam
2016/07/30 01:13:02
i don't know that this message adds much, but I gu
tommycli
2016/08/01 17:33:24
Done.
| |
| 285 }, | |
| 286 }; | |
| 287 | |
| 266 /** | 288 /** |
| 267 * Use this function (and only this function) to navigate within Settings. | 289 * Returns the matching canonical route, or the default route if none matches. |
| 268 * This function is set by settings-router once it is created. | 290 * @param {string} path |
| 269 * @type {?function(!settings.Route):void} | 291 * @return {!settings.Route} |
| 292 * @private | |
| 270 */ | 293 */ |
| 271 var navigateTo = null; | 294 var getRouteFor = function(path) { |
|
Dan Beam
2016/07/30 01:13:02
in my utopia:
return Object.values(Routes).find(r
tommycli
2016/08/01 17:33:24
Acknowledged.
| |
| 295 // TODO(tommycli): Use Object.values once Closure compilation supports it. | |
| 296 var matchingKey = Object.keys(Route).find(function(key) { | |
| 297 return Route[key].path == path; | |
| 298 }); | |
| 299 | |
| 300 if (!matchingKey) | |
| 301 return Route.BASIC; | |
| 302 | |
| 303 return Route[matchingKey]; | |
|
Dan Beam
2016/07/30 01:13:02
return Route[matchingKey] || Route.BASIC;
tommycli
2016/08/01 17:33:24
Done.
tommycli
2016/08/01 20:06:35
FYI, in a different patch we changed the behavior
| |
| 304 }; | |
| 305 | |
| 306 /** | |
| 307 * The current active route. This may only be updated via the global | |
| 308 * function settings.navigateTo. | |
| 309 * @private {!settings.Route} | |
| 310 */ | |
| 311 var currentRoute_ = getRouteFor(window.location.pathname); | |
| 312 | |
| 313 /** @return {!settings.Route} */ | |
| 314 var getCurrentRoute = function() { return currentRoute_; }; | |
| 315 | |
| 316 /** | |
| 317 * Navigates to a canonical route. | |
| 318 * @param {!settings.Route} route | |
| 319 * @private | |
| 320 */ | |
| 321 var navigateTo = function(route) { | |
| 322 assert(!!route); | |
|
Dan Beam
2016/07/30 01:13:02
assert(!!thing) is the same as assert(thing)
the
Dan Beam
2016/07/30 01:13:02
nit: if (assert(route) == currentRoute_)
tommycli
2016/08/01 17:33:23
Done.
tommycli
2016/08/01 17:33:23
Done.
| |
| 323 if (route == currentRoute_) | |
| 324 return; | |
| 325 | |
| 326 window.history.pushState(undefined, document.title, route.path); | |
| 327 currentRoute_ = route; | |
| 328 for (var observer of routeObservers_) | |
| 329 observer.currentRouteChanged_(); | |
|
Dan Beam
2016/07/30 01:13:02
nit:
var setCurrentRoute = function(route) {
cu
tommycli
2016/08/01 17:33:23
Done.
| |
| 330 }; | |
| 331 | |
| 332 window.addEventListener('popstate', function(event) { | |
| 333 // On pop state, do not push the state onto the window.history again. | |
| 334 currentRoute_ = getRouteFor(window.location.pathname); | |
| 335 for (var observer of routeObservers_) | |
| 336 observer.currentRouteChanged_(); | |
| 337 }); | |
| 272 | 338 |
| 273 return { | 339 return { |
| 274 Route: Route, | 340 Route: Route, |
| 341 RouteObserverBehavior: RouteObserverBehavior, | |
| 342 getRouteFor: getRouteFor, | |
| 343 getCurrentRoute: getCurrentRoute, | |
| 275 navigateTo: navigateTo, | 344 navigateTo: navigateTo, |
| 276 }; | 345 }; |
| 277 }); | 346 }); |
| OLD | NEW |