Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1319)

Unified Diff: chrome/browser/resources/settings/route.js

Issue 2193333002: Setting Router Refactor: Implement RouteObserverBehavior. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix naming issue in route.js Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/settings/settings_page/settings_router.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/settings/route.js
diff --git a/chrome/browser/resources/settings/route.js b/chrome/browser/resources/settings/route.js
index 453e693115ffac4d0d4a494ebfe6705818ec6e62..dc4f177aafd754a0af8b714d77e5cc4f30e603de 100644
--- a/chrome/browser/resources/settings/route.js
+++ b/chrome/browser/resources/settings/route.js
@@ -257,12 +257,24 @@ cr.define('settings', function() {
r.DETAILED_BUILD_INFO.section = 'about';
</if>
- /**
- * 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}
- */
- var navigateTo = null;
+ var routeObservers_ = new Set();
+
+ /** @polymerBehavior */
+ var RouteObserverBehavior = {
+ /** @override */
+ attached: function() {
+ assert(!routeObservers_.has(this));
+ routeObservers_.add(this);
+ },
+
+ /** @override */
+ detached: function() {
+ assert(routeObservers_.delete(this));
+ },
+
+ /** @abstract */
+ currentRouteChanged: assertNotReached,
+ };
/**
* Returns the matching canonical route, or null if none matches.
@@ -276,15 +288,52 @@ cr.define('settings', function() {
return Route[key].path == path;
});
- if (!matchingKey)
- return null;
+ return Route[matchingKey] || null;
+ };
+
+ /**
+ * The current active route. This may only be updated via the global
+ * function settings.navigateTo.
+ * @private {!settings.Route}
+ */
+ var currentRoute_ = getRouteForPath(window.location.pathname) || Route.BASIC;
- return Route[matchingKey];
+ /**
+ * Helper function to set the current route and notify all observers.
+ * @param {!settings.Route} route
+ */
+ var setCurrentRoute = function(route) {
+ currentRoute_ = route;
+ for (var observer of routeObservers_)
+ observer.currentRouteChanged();
};
+ /** @return {!settings.Route} */
+ var getCurrentRoute = function() { return currentRoute_; };
+
+ /**
+ * Navigates to a canonical route and pushes a new history entry.
+ * @param {!settings.Route} route
+ * @private
+ */
+ var navigateTo = function(route) {
+ if (assert(route) == currentRoute_)
+ return;
+
+ window.history.pushState(undefined, document.title, route.path);
+ setCurrentRoute(route);
+ };
+
+ window.addEventListener('popstate', function(event) {
+ // On pop state, do not push the state onto the window.history again.
+ setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC);
+ });
+
return {
Route: Route,
- navigateTo: navigateTo,
+ RouteObserverBehavior: RouteObserverBehavior,
getRouteForPath: getRouteForPath,
+ getCurrentRoute: getCurrentRoute,
+ navigateTo: navigateTo,
};
});
« no previous file with comments | « no previous file | chrome/browser/resources/settings/settings_page/settings_router.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698