Chromium Code Reviews| Index: chrome/browser/resources/settings/route.js |
| diff --git a/chrome/browser/resources/settings/route.js b/chrome/browser/resources/settings/route.js |
| index 0ef6c3fab622800f2120279fc2d99e7fcd9ec704..802ecc7f637a1c252a633207da4da49f4488d224 100644 |
| --- a/chrome/browser/resources/settings/route.js |
| +++ b/chrome/browser/resources/settings/route.js |
| @@ -263,15 +263,84 @@ cr.define('settings', function() { |
| r.DETAILED_BUILD_INFO.section = 'about'; |
| </if> |
| + var routeObservers_ = new Set(); |
| + |
| + /** @polymerBehavior */ |
| + var RouteObserverBehavior = { |
| + /** @override */ |
| + attached: function() { |
| + assert(!routeObservers_.has(this)); |
| + routeObservers_.add(this); |
| + }, |
| + |
| + /** @override */ |
| + detached: function() { |
| + assert(routeObservers_.has(this)); |
| + routeObservers_.delete(this); |
|
Dan Beam
2016/07/30 01:13:02
assert(routeObservers_.delete(this));
tommycli
2016/08/01 17:33:24
Done.
|
| + }, |
| + |
|
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
|
| + 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.
|
| + assertNotReached('Elements that mixin RouteObserverBehavior should ' + |
| + '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.
|
| + }, |
| + }; |
| + |
| + /** |
| + * Returns the matching canonical route, or the default route if none matches. |
| + * @param {string} path |
| + * @return {!settings.Route} |
| + * @private |
| + */ |
| + 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.
|
| + // TODO(tommycli): Use Object.values once Closure compilation supports it. |
| + var matchingKey = Object.keys(Route).find(function(key) { |
| + return Route[key].path == path; |
| + }); |
| + |
| + if (!matchingKey) |
| + return Route.BASIC; |
| + |
| + 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
|
| + }; |
| + |
| /** |
| - * Use this function (and only this function) to navigate within Settings. |
| - * This function is set by settings-router once it is created. |
| - * @type {?function(!settings.Route):void} |
| + * The current active route. This may only be updated via the global |
| + * function settings.navigateTo. |
| + * @private {!settings.Route} |
| */ |
| - var navigateTo = null; |
| + var currentRoute_ = getRouteFor(window.location.pathname); |
| + |
| + /** @return {!settings.Route} */ |
| + var getCurrentRoute = function() { return currentRoute_; }; |
| + |
| + /** |
| + * Navigates to a canonical route. |
| + * @param {!settings.Route} route |
| + * @private |
| + */ |
| + var navigateTo = function(route) { |
| + 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.
|
| + if (route == currentRoute_) |
| + return; |
| + |
| + window.history.pushState(undefined, document.title, route.path); |
| + currentRoute_ = route; |
| + for (var observer of routeObservers_) |
| + observer.currentRouteChanged_(); |
|
Dan Beam
2016/07/30 01:13:02
nit:
var setCurrentRoute = function(route) {
cu
tommycli
2016/08/01 17:33:23
Done.
|
| + }; |
| + |
| + window.addEventListener('popstate', function(event) { |
| + // On pop state, do not push the state onto the window.history again. |
| + currentRoute_ = getRouteFor(window.location.pathname); |
| + for (var observer of routeObservers_) |
| + observer.currentRouteChanged_(); |
| + }); |
| return { |
| Route: Route, |
| + RouteObserverBehavior: RouteObserverBehavior, |
| + getRouteFor: getRouteFor, |
| + getCurrentRoute: getCurrentRoute, |
| navigateTo: navigateTo, |
| }; |
| }); |