OLD | NEW |
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 |
11 */ | 11 */ |
12 var Route = function(path) { | 12 var Route = function(path) { |
13 this.path = path; | 13 this.path = path; |
14 | 14 |
15 /** @type {?settings.Route} */ | 15 /** @type {?settings.Route} */ |
16 this.parent = null; | 16 this.parent = null; |
17 | 17 |
18 // Below are all legacy properties to provide compatibility with the old | 18 // Below are all legacy properties to provide compatibility with the old |
19 // routing system. TODO(tommycli): Remove once routing refactor complete. | 19 // routing system. TODO(tommycli): Remove once routing refactor complete. |
20 this.page = ''; | |
21 this.section = ''; | 20 this.section = ''; |
22 /** @type {!Array<string>} */ this.subpage = []; | 21 /** @type {!Array<string>} */ this.subpage = []; |
23 }; | 22 }; |
24 | 23 |
25 Route.prototype = { | 24 Route.prototype = { |
26 /** | 25 /** |
27 * Returns a new Route instance that's a child of this route. | 26 * Returns a new Route instance that's a child of this route. |
28 * @param {string} path Extends this route's path if it doesn't contain a | 27 * @param {string} path Extends this route's path if it doesn't contain a |
29 * leading slash. | 28 * leading slash. |
30 * @param {string=} opt_subpageName | 29 * @param {string=} opt_subpageName |
31 * @return {!settings.Route} | 30 * @return {!settings.Route} |
32 * @private | 31 * @private |
33 */ | 32 */ |
34 createChild: function(path, opt_subpageName) { | 33 createChild: function(path, opt_subpageName) { |
35 assert(path); | 34 assert(path); |
36 | 35 |
37 // |path| extends this route's path if it doesn't have a leading slash. | 36 // |path| extends this route's path if it doesn't have a leading slash. |
38 // If it does have a leading slash, it's just set as the new route's URL. | 37 // If it does have a leading slash, it's just set as the new route's URL. |
39 var newUrl = path[0] == '/' ? path : this.path + '/' + path; | 38 var newUrl = path[0] == '/' ? path : this.path + '/' + path; |
40 | 39 |
41 var route = new Route(newUrl); | 40 var route = new Route(newUrl); |
42 route.parent = this; | 41 route.parent = this; |
43 route.page = this.page; | |
44 route.section = this.section; | 42 route.section = this.section; |
45 route.subpage = this.subpage.slice(); // Shallow copy. | 43 route.subpage = this.subpage.slice(); // Shallow copy. |
46 | 44 |
47 if (opt_subpageName) | 45 if (opt_subpageName) |
48 route.subpage.push(opt_subpageName); | 46 route.subpage.push(opt_subpageName); |
49 | 47 |
50 return route; | 48 return route; |
51 }, | 49 }, |
52 | 50 |
53 /** | 51 /** |
(...skipping 17 matching lines...) Expand all Loading... |
71 * @return {!settings.Route} | 69 * @return {!settings.Route} |
72 * @private | 70 * @private |
73 */ | 71 */ |
74 createSection: function(path, section) { | 72 createSection: function(path, section) { |
75 var route = this.createChild(path); | 73 var route = this.createChild(path); |
76 route.section = section; | 74 route.section = section; |
77 return route; | 75 return route; |
78 }, | 76 }, |
79 | 77 |
80 /** | 78 /** |
81 * Returns true if this route is a descendant of the parameter. | 79 * Returns true if this route matches or is an ancestor of the parameter. |
82 * @param {!settings.Route} route | 80 * @param {!settings.Route} route |
83 * @return {boolean} | 81 * @return {boolean} |
84 */ | 82 */ |
85 isDescendantOf: function(route) { | 83 contains: function(route) { |
86 for (var parent = this.parent; parent != null; parent = parent.parent) { | 84 for (var r = route; r != null; r = r.parent) { |
87 if (route == parent) | 85 if (this == r) |
88 return true; | 86 return true; |
89 } | 87 } |
90 | |
91 return false; | 88 return false; |
92 }, | 89 }, |
93 }; | 90 }; |
94 | 91 |
95 // Abbreviated variable for easier definitions. | 92 // Abbreviated variable for easier definitions. |
96 var r = Route; | 93 var r = Route; |
97 | 94 |
98 // Root pages. | 95 // Root pages. |
99 r.BASIC = new Route('/'); | 96 r.BASIC = new Route('/'); |
100 r.BASIC.page = 'basic'; | |
101 r.ADVANCED = new Route('/advanced'); | 97 r.ADVANCED = new Route('/advanced'); |
102 r.ADVANCED.page = 'advanced'; | |
103 r.ABOUT = new Route('/help'); | 98 r.ABOUT = new Route('/help'); |
104 r.ABOUT.page = 'about'; | |
105 | 99 |
106 <if expr="chromeos"> | 100 <if expr="chromeos"> |
107 r.INTERNET = r.BASIC.createSection('/internet', 'internet'); | 101 r.INTERNET = r.BASIC.createSection('/internet', 'internet'); |
108 r.NETWORK_DETAIL = r.INTERNET.createChild('/networkDetail', 'network-detail'); | 102 r.NETWORK_DETAIL = r.INTERNET.createChild('/networkDetail', 'network-detail'); |
109 r.KNOWN_NETWORKS = r.INTERNET.createChild('/knownNetworks', 'known-networks'); | 103 r.KNOWN_NETWORKS = r.INTERNET.createChild('/knownNetworks', 'known-networks'); |
110 </if> | 104 </if> |
111 | 105 |
112 r.APPEARANCE = r.BASIC.createSection('/appearance', 'appearance'); | 106 r.APPEARANCE = r.BASIC.createSection('/appearance', 'appearance'); |
113 r.FONTS = r.APPEARANCE.createChild('/fonts', 'appearance-fonts'); | 107 r.FONTS = r.APPEARANCE.createChild('/fonts', 'appearance-fonts'); |
114 | 108 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 r.DETAILED_BUILD_INFO.section = 'about'; | 257 r.DETAILED_BUILD_INFO.section = 'about'; |
264 </if> | 258 </if> |
265 | 259 |
266 /** | 260 /** |
267 * Use this function (and only this function) to navigate within Settings. | 261 * Use this function (and only this function) to navigate within Settings. |
268 * This function is set by settings-router once it is created. | 262 * This function is set by settings-router once it is created. |
269 * @type {?function(!settings.Route):void} | 263 * @type {?function(!settings.Route):void} |
270 */ | 264 */ |
271 var navigateTo = null; | 265 var navigateTo = null; |
272 | 266 |
| 267 /** |
| 268 * Returns the matching canonical route, or null if none matches. |
| 269 * @param {string} path |
| 270 * @return {?settings.Route} |
| 271 * @private |
| 272 */ |
| 273 var getRouteForPath = function(path) { |
| 274 // TODO(tommycli): Use Object.values once Closure compilation supports it. |
| 275 var matchingKey = Object.keys(Route).find(function(key) { |
| 276 return Route[key].path == path; |
| 277 }); |
| 278 |
| 279 if (!matchingKey) |
| 280 return null; |
| 281 |
| 282 return Route[matchingKey]; |
| 283 }; |
| 284 |
273 return { | 285 return { |
274 Route: Route, | 286 Route: Route, |
275 navigateTo: navigateTo, | 287 navigateTo: navigateTo, |
| 288 getRouteForPath: getRouteForPath, |
276 }; | 289 }; |
277 }); | 290 }); |
OLD | NEW |