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

Side by Side Diff: chrome/browser/resources/settings/settings_page/settings_router.js

Issue 2193333002: Setting Router Refactor: Implement RouteObserverBehavior. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 /** 5 /**
6 * @fileoverview 6 * @fileoverview
7 * 'settings-router' is a simple router for settings. Its responsibilities: 7 * 'settings-router' is only used to propagate route changes to bound elements.
8 * - Update the URL when the routing state changes. 8 * All the real routing is defined within route.js.
9 * - Initialize the routing state with the initial URL. 9 * TODO(tommycli): Remove once all elements migrated to RouteObserverBehavior.
10 * - Process and validate all routing state changes.
11 * 10 *
12 * Example: 11 * Example:
13 *
14 * <settings-router current-route="{{currentRoute}}"> 12 * <settings-router current-route="{{currentRoute}}">
15 * </settings-router> 13 * </settings-router>
16 */ 14 */
17 Polymer({ 15 Polymer({
18 is: 'settings-router', 16 is: 'settings-router',
19 17
18 behaviors: [settings.RouteObserverBehavior],
19
20 properties: { 20 properties: {
21 /** 21 /**
22 * The current active route. This may only be updated via the global 22 * Only used to propagate settings.currentRoute to all the elements bound to
23 * function settings.navigateTo. 23 * settings-router.
24 *
25 * currentRoute.page refers to top-level pages such as Basic and Advanced.
26 *
27 * currentRoute.section is only non-empty when the user is on a subpage. If
28 * the user is on Basic, for instance, this is an empty string.
29 *
30 * currentRoute.subpage is an Array. The last element is the actual subpage
31 * the user is on. The previous elements are the ancestor subpages. This
32 * enables support for multiple paths to the same subpage. This is used by
33 * both the Back button and the Breadcrumb to determine ancestor subpages.
34 * @type {!settings.Route} 24 * @type {!settings.Route}
35 */ 25 */
36 currentRoute: { 26 currentRoute: {
37 notify: true, 27 notify: true,
38 type: Object, 28 type: Object,
39 value: function() { 29 value: function() { return settings.getCurrentRoute(); },
40 return this.getRouteFor_(window.location.pathname);
41 },
42 }, 30 },
43 }, 31 },
44 32
45 /** 33 /** @private */
46 * Sets up a history popstate observer. 34 currentRouteChanged_: function() {
47 * @override 35 this.currentRoute = settings.getCurrentRoute();
48 */
49 created: function() {
50 window.addEventListener('popstate', function(event) {
51 // On pop state, do not push the state onto the window.history again.
52 this.currentRoute = this.getRouteFor_(window.location.pathname);
53 }.bind(this));
54
55 settings.navigateTo = this.navigateTo_.bind(this);
56 },
57
58 /**
59 * Returns the matching canonical route, or the default route if none matches.
60 * @param {string} path
61 * @return {!settings.Route}
62 * @private
63 */
64 getRouteFor_: function(path) {
65 // TODO(tommycli): Use Object.values once Closure compilation supports it.
66 var matchingKey = Object.keys(settings.Route).find(function(key) {
67 return settings.Route[key].path == path;
68 });
69
70 if (!matchingKey)
71 return settings.Route.BASIC;
72
73 return settings.Route[matchingKey];
74 },
75
76 /**
77 * Navigates to a canonical route.
78 * @param {!settings.Route} route
79 * @private
80 */
81 navigateTo_: function(route) {
82 assert(!!route);
83
84 if (route == this.currentRoute)
85 return;
86
87 window.history.pushState(undefined, document.title, route.path);
88 this.currentRoute = route;
89 }, 36 },
90 }); 37 });
OLDNEW
« chrome/browser/resources/settings/route.js ('K') | « chrome/browser/resources/settings/route.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698