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 if (this == route) |
michaelpg
2016/07/29 20:28:53
optional alternative:
for (; route != null; r
tommycli
2016/07/29 21:04:39
Done. Though I had to make it a slight variant si
| |
87 if (route == parent) | 85 return true; |
86 | |
87 for (var parent = route.parent; parent != null; parent = parent.parent) { | |
88 if (this == parent) | |
88 return true; | 89 return true; |
89 } | 90 } |
90 | 91 |
91 return false; | 92 return false; |
92 }, | 93 }, |
93 }; | 94 }; |
94 | 95 |
95 // Abbreviated variable for easier definitions. | 96 // Abbreviated variable for easier definitions. |
96 var r = Route; | 97 var r = Route; |
97 | 98 |
98 // Root pages. | 99 // Root pages. |
99 r.BASIC = new Route('/'); | 100 r.BASIC = new Route('/'); |
100 r.BASIC.page = 'basic'; | |
101 r.ADVANCED = new Route('/advanced'); | 101 r.ADVANCED = new Route('/advanced'); |
102 r.ADVANCED.page = 'advanced'; | |
103 r.ABOUT = new Route('/help'); | 102 r.ABOUT = new Route('/help'); |
104 r.ABOUT.page = 'about'; | |
105 | 103 |
106 <if expr="chromeos"> | 104 <if expr="chromeos"> |
107 r.INTERNET = r.BASIC.createSection('/internet', 'internet'); | 105 r.INTERNET = r.BASIC.createSection('/internet', 'internet'); |
108 r.NETWORK_DETAIL = r.INTERNET.createChild('/networkDetail', 'network-detail'); | 106 r.NETWORK_DETAIL = r.INTERNET.createChild('/networkDetail', 'network-detail'); |
109 r.KNOWN_NETWORKS = r.INTERNET.createChild('/knownNetworks', 'known-networks'); | 107 r.KNOWN_NETWORKS = r.INTERNET.createChild('/knownNetworks', 'known-networks'); |
110 </if> | 108 </if> |
111 | 109 |
112 r.APPEARANCE = r.BASIC.createSection('/appearance', 'appearance'); | 110 r.APPEARANCE = r.BASIC.createSection('/appearance', 'appearance'); |
113 r.FONTS = r.APPEARANCE.createChild('/fonts', 'appearance-fonts'); | 111 r.FONTS = r.APPEARANCE.createChild('/fonts', 'appearance-fonts'); |
114 | 112 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 r.DETAILED_BUILD_INFO.section = 'about'; | 261 r.DETAILED_BUILD_INFO.section = 'about'; |
264 </if> | 262 </if> |
265 | 263 |
266 /** | 264 /** |
267 * Use this function (and only this function) to navigate within Settings. | 265 * Use this function (and only this function) to navigate within Settings. |
268 * This function is set by settings-router once it is created. | 266 * This function is set by settings-router once it is created. |
269 * @type {?function(!settings.Route):void} | 267 * @type {?function(!settings.Route):void} |
270 */ | 268 */ |
271 var navigateTo = null; | 269 var navigateTo = null; |
272 | 270 |
271 /** | |
272 * Returns the matching canonical route, or null if none matches. | |
273 * @param {string} path | |
274 * @return {settings.Route} | |
michaelpg
2016/07/29 20:28:53
{?s.R}
tommycli
2016/07/29 21:04:39
Done.
| |
275 * @private | |
276 */ | |
277 var getRouteForPath = function(path) { | |
278 // TODO(tommycli): Use Object.values once Closure compilation supports it. | |
279 var matchingKey = Object.keys(Route).find(function(key) { | |
280 return Route[key].path == path; | |
281 }); | |
282 | |
283 if (!matchingKey) | |
284 return null; | |
285 | |
286 return Route[matchingKey]; | |
287 }; | |
288 | |
273 return { | 289 return { |
274 Route: Route, | 290 Route: Route, |
275 navigateTo: navigateTo, | 291 navigateTo: navigateTo, |
292 getRouteForPath: getRouteForPath, | |
276 }; | 293 }; |
277 }); | 294 }); |
OLD | NEW |