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

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

Issue 1916323002: [MD settings] side nav routing to page sections (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge with master Created 4 years, 7 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 * @typedef {{
7 * url: string,
8 * page: string,
9 * section: string,
10 * subpage: !Array<string>,
11 * }}
12 */
13 var SettingsRoute;
14
15 /**
6 * @fileoverview 16 * @fileoverview
7 * 'settings-router' is a simple router for settings. Its responsibilities: 17 * 'settings-router' is a simple router for settings. Its responsibilities:
8 * - Update the URL when the routing state changes. 18 * - Update the URL when the routing state changes.
9 * - Initialize the routing state with the initial URL. 19 * - Initialize the routing state with the initial URL.
10 * - Process and validate all routing state changes. 20 * - Process and validate all routing state changes.
11 * 21 *
12 * Example: 22 * Example:
13 * 23 *
14 * <settings-router current-route="{{currentRoute}}"> 24 * <settings-router current-route="{{currentRoute}}">
15 * </settings-router> 25 * </settings-router>
16 */ 26 */
17 Polymer({ 27 Polymer({
18 is: 'settings-router', 28 is: 'settings-router',
19 29
20 properties: { 30 properties: {
21 /** 31 /**
22 * The current active route. This is reflected to the URL. Updates to this 32 * The current active route. This is reflected to the URL. Updates to this
23 * property should replace the whole object. 33 * property should replace the whole object.
24 * 34 *
25 * currentRoute.page refers to top-level pages such as Basic and Advanced. 35 * currentRoute.page refers to top-level pages such as Basic and Advanced.
26 * 36 *
27 * currentRoute.section is only non-empty when the user is on a subpage. If 37 * 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. 38 * the user is on Basic, for instance, this is an empty string.
29 * 39 *
30 * currentRoute.subpage is an Array. The last element is the actual subpage 40 * 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 41 * 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 42 * 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. 43 * both the Back button and the Breadcrumb to determine ancestor subpages.
44 * @type {SettingsRoute}
34 */ 45 */
35 currentRoute: { 46 currentRoute: {
36 notify: true, 47 notify: true,
37 observer: 'currentRouteChanged_', 48 observer: 'currentRouteChanged_',
38 type: Object, 49 type: Object,
39 value: function() { 50 value: function() {
40 // Take the current URL, find a matching pre-defined route, and 51 // Take the current URL, find a matching pre-defined route, and
41 // initialize the currentRoute to that pre-defined route. 52 // initialize the currentRoute to that pre-defined route.
42 for (var i = 0; i < this.routes_.length; ++i) { 53 for (var i = 0; i < this.routes_.length; ++i) {
43 var route = this.routes_[i]; 54 var route = this.routes_[i];
44 if (route.url == window.location.pathname) { 55 if (route.url == window.location.pathname) {
45 return { 56 return {
57 url: route.url,
46 page: route.page, 58 page: route.page,
47 section: route.section, 59 section: route.section,
48 subpage: route.subpage, 60 subpage: route.subpage,
49 }; 61 };
50 } 62 }
51 } 63 }
52 64
53 // As a fallback return the default route. 65 // As a fallback return the default route.
54 return this.routes_[0]; 66 return this.routes_[0];
55 }, 67 },
56 }, 68 },
57 69
58 /** 70 /**
59 * Page titles for the currently active route. Updated by the currentRoute 71 * Page titles for the currently active route. Updated by the currentRoute
60 * property observer. 72 * property observer.
61 * @type {{pageTitle: string}} 73 * @type {{pageTitle: string}}
62 */ 74 */
63 currentRouteTitles: { 75 currentRouteTitles: {
64 notify: true, 76 notify: true,
65 type: Object, 77 type: Object,
66 value: function() { 78 value: function() {
67 return { 79 return {
68 pageTitle: '', 80 pageTitle: '',
69 }; 81 };
70 }, 82 },
71 }, 83 },
72 }, 84 },
73 85
74 86
75 /** 87 /**
76 * @private 88 * @private {!Array<!SettingsRoute>}
77 * The 'url' property is not accessible to other elements. 89 * The 'url' property is not accessible to other elements.
78 */ 90 */
79 routes_: [ 91 routes_: [
80 { 92 {
81 url: '/', 93 url: '/',
82 page: 'basic', 94 page: 'basic',
83 section: '', 95 section: '',
84 subpage: [], 96 subpage: [],
85 }, 97 },
86 { 98 {
87 url: '/advanced', 99 url: '/advanced',
88 page: 'advanced', 100 page: 'advanced',
89 section: '', 101 section: '',
90 subpage: [], 102 subpage: [],
91 }, 103 },
92 <if expr="chromeos"> 104 <if expr="chromeos">
93 { 105 {
106 url: '/internet',
107 page: 'basic',
108 section: 'internet',
109 subpage: [],
110 },
111 {
94 url: '/networkDetail', 112 url: '/networkDetail',
95 page: 'basic', 113 page: 'basic',
96 section: 'internet', 114 section: 'internet',
97 subpage: ['network-detail'], 115 subpage: ['network-detail'],
98 }, 116 },
99 { 117 {
100 url: '/knownNetworks', 118 url: '/knownNetworks',
101 page: 'basic', 119 page: 'basic',
102 section: 'internet', 120 section: 'internet',
103 subpage: ['known-networks'], 121 subpage: ['known-networks'],
104 }, 122 },
105 </if> 123 </if>
106 { 124 {
125 url: '/appearance',
126 page: 'basic',
127 section: 'appearance',
128 subpage: [],
129 },
130 {
107 url: '/fonts', 131 url: '/fonts',
108 page: 'basic', 132 page: 'basic',
109 section: 'appearance', 133 section: 'appearance',
110 subpage: ['appearance-fonts'], 134 subpage: ['appearance-fonts'],
111 }, 135 },
112 { 136 {
137 url: '/defaultBrowser',
138 page: 'basic',
139 section: 'defaultBrowser',
140 subpage: [],
141 },
142 {
143 url: '/search',
144 page: 'basic',
145 section: 'search',
146 subpage: [],
147 },
148 {
113 url: '/searchEngines', 149 url: '/searchEngines',
114 page: 'basic', 150 page: 'basic',
115 section: 'search', 151 section: 'search',
116 subpage: ['search-engines'], 152 subpage: ['search-engines'],
117 }, 153 },
118 { 154 {
119 url: '/searchEngines/advanced', 155 url: '/searchEngines/advanced',
120 page: 'basic', 156 page: 'basic',
121 section: 'search', 157 section: 'search',
122 subpage: ['search-engines', 'search-engines-advanced'], 158 subpage: ['search-engines', 'search-engines-advanced'],
123 }, 159 },
160 {
161 url: '/onStartup',
162 page: 'basic',
163 section: 'onStartup',
164 subpage: [],
165 },
166 {
167 url: '/people',
168 page: 'basic',
169 section: 'people',
170 subpage: [],
171 },
124 <if expr="chromeos"> 172 <if expr="chromeos">
125 { 173 {
126 url: '/changePicture', 174 url: '/changePicture',
127 page: 'basic', 175 page: 'basic',
128 section: 'people', 176 section: 'people',
129 subpage: ['changePicture'], 177 subpage: ['changePicture'],
130 }, 178 },
131 </if> 179 </if>
132 <if expr="not chromeos"> 180 <if expr="not chromeos">
133 { 181 {
(...skipping 11 matching lines...) Expand all
145 }, 193 },
146 <if expr="chromeos"> 194 <if expr="chromeos">
147 { 195 {
148 url: '/accounts', 196 url: '/accounts',
149 page: 'basic', 197 page: 'basic',
150 section: 'people', 198 section: 'people',
151 subpage: ['users'], 199 subpage: ['users'],
152 }, 200 },
153 </if> 201 </if>
154 { 202 {
203 url: '/advanced',
204 page: 'advanced',
205 section: 'privacy',
206 subpage: [],
207 },
208 {
155 url: '/certificates', 209 url: '/certificates',
156 page: 'advanced', 210 page: 'advanced',
157 section: 'privacy', 211 section: 'privacy',
158 subpage: ['manage-certificates'], 212 subpage: ['manage-certificates'],
159 }, 213 },
160 { 214 {
161 url: '/siteSettings', 215 url: '/siteSettings',
162 page: 'advanced', 216 page: 'advanced',
163 section: 'privacy', 217 section: 'privacy',
164 subpage: ['site-settings'], 218 subpage: ['site-settings'],
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 'site-details'], 349 'site-details'],
296 }, 350 },
297 { 351 {
298 url: '/clearBrowserData', 352 url: '/clearBrowserData',
299 page: 'advanced', 353 page: 'advanced',
300 section: 'privacy', 354 section: 'privacy',
301 subpage: ['clear-browsing-data'], 355 subpage: ['clear-browsing-data'],
302 }, 356 },
303 <if expr="chromeos"> 357 <if expr="chromeos">
304 { 358 {
359 url: '/bluetooth',
360 page: 'advanced',
361 section: 'bluetooth',
362 subpage: [],
363 },
364 {
305 url: '/bluetoothAddDevice', 365 url: '/bluetoothAddDevice',
306 page: 'advanced', 366 page: 'advanced',
307 section: 'bluetooth', 367 section: 'bluetooth',
308 subpage: ['bluetooth-add-device'], 368 subpage: ['bluetooth-add-device'],
309 }, 369 },
310 { 370 {
311 url: '/bluetoothAddDevice/bluetoothPairDevice', 371 url: '/bluetoothAddDevice/bluetoothPairDevice',
312 page: 'advanced', 372 page: 'advanced',
313 section: 'bluetooth', 373 section: 'bluetooth',
314 subpage: ['bluetooth-add-device', 'bluetooth-pair-device'], 374 subpage: ['bluetooth-add-device', 'bluetooth-pair-device'],
315 }, 375 },
316 </if> 376 </if>
317 { 377 {
318 url: '/autofill', 378 url: '/autofill',
319 page: 'advanced', 379 page: 'advanced',
320 section: 'passwordsAndForms', 380 section: 'passwordsAndForms',
321 subpage: ['manage-autofill'], 381 subpage: ['manage-autofill'],
322 }, 382 },
323 { 383 {
384 url: '/pw', // TODO(dschuyler): find a better url.
385 page: 'advanced',
386 section: 'passwordsAndForms',
387 subpage: [],
388 },
389 {
324 url: '/passwords', 390 url: '/passwords',
325 page: 'advanced', 391 page: 'advanced',
326 section: 'passwordsAndForms', 392 section: 'passwordsAndForms',
327 subpage: ['manage-passwords'], 393 subpage: ['manage-passwords'],
328 }, 394 },
329 { 395 {
396 url: '/ln', // TODO(dschuyler): find a better url.
397 page: 'advanced',
398 section: 'languages',
399 subpage: [],
400 },
401 {
330 url: '/languages', 402 url: '/languages',
331 page: 'advanced', 403 page: 'advanced',
332 section: 'languages', 404 section: 'languages',
333 subpage: ['manage-languages'], 405 subpage: ['manage-languages'],
334 }, 406 },
335 { 407 {
336 url: '/languages/edit', 408 url: '/languages/edit',
337 page: 'advanced', 409 page: 'advanced',
338 section: 'languages', 410 section: 'languages',
339 subpage: ['language-detail'], 411 subpage: ['language-detail'],
340 }, 412 },
341 <if expr="chromeos"> 413 <if expr="chromeos">
342 { 414 {
343 url: '/inputMethods', 415 url: '/inputMethods',
344 page: 'advanced', 416 page: 'advanced',
345 section: 'languages', 417 section: 'languages',
346 subpage: ['manage-input-methods'], 418 subpage: ['manage-input-methods'],
347 }, 419 },
348 </if> 420 </if>
349 <if expr="not is_macosx"> 421 <if expr="not is_macosx">
350 { 422 {
351 url: '/editDictionary', 423 url: '/editDictionary',
352 page: 'advanced', 424 page: 'advanced',
353 section: 'languages', 425 section: 'languages',
354 subpage: ['edit-dictionary'], 426 subpage: ['edit-dictionary'],
355 }, 427 },
356 </if> 428 </if>
429 {
430 url: '/downloadsDirectory',
431 page: 'advanced',
432 section: 'downloads',
433 subpage: [],
434 },
435 {
436 url: '/accessibility',
437 page: 'advanced',
438 section: 'a11y',
439 subpage: [],
440 },
441 {
442 url: '/system',
443 page: 'advanced',
444 section: 'system',
445 subpage: [],
446 },
447 {
448 url: '/reset',
449 page: 'advanced',
450 section: 'reset',
451 subpage: [],
452 },
357 <if expr="chromeos"> 453 <if expr="chromeos">
358 { 454 {
455 url: '/device',
456 page: 'basic',
457 section: 'device',
458 subpage: [],
459 },
460 {
359 url: '/pointer-overlay', 461 url: '/pointer-overlay',
360 page: 'basic', 462 page: 'basic',
361 section: 'device', 463 section: 'device',
362 subpage: ['touchpad'], 464 subpage: ['touchpad'],
363 }, 465 },
364 { 466 {
365 url: '/keyboard-overlay', 467 url: '/keyboard-overlay',
366 page: 'basic', 468 page: 'basic',
367 section: 'device', 469 section: 'device',
368 subpage: ['keyboard'], 470 subpage: ['keyboard'],
(...skipping 11 matching lines...) Expand all
380 * Sets up a history popstate observer. 482 * Sets up a history popstate observer.
381 */ 483 */
382 created: function() { 484 created: function() {
383 window.addEventListener('popstate', function(event) { 485 window.addEventListener('popstate', function(event) {
384 if (event.state && event.state.page) 486 if (event.state && event.state.page)
385 this.currentRoute = event.state; 487 this.currentRoute = event.state;
386 }.bind(this)); 488 }.bind(this));
387 }, 489 },
388 490
389 /** 491 /**
390 * @private
391 * Is called when another element modifies the route. This observer validates 492 * Is called when another element modifies the route. This observer validates
392 * the route change against the pre-defined list of routes, and updates the 493 * the route change against the pre-defined list of routes, and updates the
393 * URL appropriately. 494 * URL appropriately.
495 * @param {!SettingsRoute} newRoute Where we're headed.
496 * @param {!SettingsRoute|undefined} oldRoute Where we've been.
497 * @private
394 */ 498 */
395 currentRouteChanged_: function(newRoute, oldRoute) { 499 currentRouteChanged_: function(newRoute, oldRoute) {
396 for (var i = 0; i < this.routes_.length; ++i) { 500 for (var i = 0; i < this.routes_.length; ++i) {
397 var route = this.routes_[i]; 501 var route = this.routes_[i];
398 if (route.page == newRoute.page && route.section == newRoute.section && 502 if (route.page == newRoute.page && route.section == newRoute.section &&
399 route.subpage.length == newRoute.subpage.length && 503 route.subpage.length == newRoute.subpage.length &&
400 newRoute.subpage.every(function(value, index) { 504 newRoute.subpage.every(function(value, index) {
401 return value == route.subpage[index]; 505 return value == route.subpage[index];
402 })) { 506 })) {
403 507
(...skipping 25 matching lines...) Expand all
429 window.history.replaceState(historicState, document.title); 533 window.history.replaceState(historicState, document.title);
430 } 534 }
431 535
432 return; 536 return;
433 } 537 }
434 } 538 }
435 539
436 assertNotReached('Route not found: ' + JSON.stringify(newRoute)); 540 assertNotReached('Route not found: ' + JSON.stringify(newRoute));
437 }, 541 },
438 }); 542 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698