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

Side by Side 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, 4 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 r.RESET = r.ADVANCED.createSection('/reset', 'reset'); 250 r.RESET = r.ADVANCED.createSection('/reset', 'reset');
251 251
252 <if expr="chromeos"> 252 <if expr="chromeos">
253 r.INPUT_METHODS = 253 r.INPUT_METHODS =
254 r.LANGUAGES.createChild('/inputMethods', 'manage-input-methods'); 254 r.LANGUAGES.createChild('/inputMethods', 'manage-input-methods');
255 r.DETAILED_BUILD_INFO = 255 r.DETAILED_BUILD_INFO =
256 r.ABOUT.createChild('/help/details', 'detailed-build-info'); 256 r.ABOUT.createChild('/help/details', 'detailed-build-info');
257 r.DETAILED_BUILD_INFO.section = 'about'; 257 r.DETAILED_BUILD_INFO.section = 'about';
258 </if> 258 </if>
259 259
260 /** 260 var routeObservers_ = new Set();
261 * Use this function (and only this function) to navigate within Settings. 261
262 * This function is set by settings-router once it is created. 262 /** @polymerBehavior */
263 * @type {?function(!settings.Route):void} 263 var RouteObserverBehavior = {
264 */ 264 /** @override */
265 var navigateTo = null; 265 attached: function() {
266 assert(!routeObservers_.has(this));
267 routeObservers_.add(this);
268 },
269
270 /** @override */
271 detached: function() {
272 assert(routeObservers_.delete(this));
273 },
274
275 /** @abstract */
276 currentRouteChanged: assertNotReached,
277 };
266 278
267 /** 279 /**
268 * Returns the matching canonical route, or null if none matches. 280 * Returns the matching canonical route, or null if none matches.
269 * @param {string} path 281 * @param {string} path
270 * @return {?settings.Route} 282 * @return {?settings.Route}
271 * @private 283 * @private
272 */ 284 */
273 var getRouteForPath = function(path) { 285 var getRouteForPath = function(path) {
274 // TODO(tommycli): Use Object.values once Closure compilation supports it. 286 // TODO(tommycli): Use Object.values once Closure compilation supports it.
275 var matchingKey = Object.keys(Route).find(function(key) { 287 var matchingKey = Object.keys(Route).find(function(key) {
276 return Route[key].path == path; 288 return Route[key].path == path;
277 }); 289 });
278 290
279 if (!matchingKey) 291 return Route[matchingKey] || null;
280 return null; 292 };
281 293
282 return Route[matchingKey]; 294 /**
295 * The current active route. This may only be updated via the global
296 * function settings.navigateTo.
297 * @private {!settings.Route}
298 */
299 var currentRoute_ = getRouteForPath(window.location.pathname) || Route.BASIC;
300
301 /**
302 * Helper function to set the current route and notify all observers.
303 * @param {!settings.Route} route
304 */
305 var setCurrentRoute = function(route) {
306 currentRoute_ = route;
307 for (var observer of routeObservers_)
308 observer.currentRouteChanged();
283 }; 309 };
284 310
311 /** @return {!settings.Route} */
312 var getCurrentRoute = function() { return currentRoute_; };
313
314 /**
315 * Navigates to a canonical route and pushes a new history entry.
316 * @param {!settings.Route} route
317 * @private
318 */
319 var navigateTo = function(route) {
320 if (assert(route) == currentRoute_)
321 return;
322
323 window.history.pushState(undefined, document.title, route.path);
324 setCurrentRoute(route);
325 };
326
327 window.addEventListener('popstate', function(event) {
328 // On pop state, do not push the state onto the window.history again.
329 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC);
330 });
331
285 return { 332 return {
286 Route: Route, 333 Route: Route,
334 RouteObserverBehavior: RouteObserverBehavior,
335 getRouteForPath: getRouteForPath,
336 getCurrentRoute: getCurrentRoute,
287 navigateTo: navigateTo, 337 navigateTo: navigateTo,
288 getRouteForPath: getRouteForPath,
289 }; 338 };
290 }); 339 });
OLDNEW
« 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