Chromium Code Reviews| 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 a descendant 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 inSubtreeOf: function(route) { |
|
michaelpg
2016/07/28 23:22:32
bikeshedding: kind of a confusing name and concept
tommycli
2016/07/29 00:03:35
It's a confusing name. I changed it to isEqualOrIn
michaelpg
2016/07/29 00:21:11
Like, that's my point: every use in this CL could
| |
| 84 if (this == route) | |
| 85 return true; | |
| 86 | |
| 86 for (var parent = this.parent; parent != null; parent = parent.parent) { | 87 for (var parent = this.parent; parent != null; parent = parent.parent) { |
| 87 if (route == parent) | 88 if (route == 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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 | 284 |
| 287 return Route[matchingKey]; | 285 return Route[matchingKey]; |
| 288 }; | 286 }; |
| 289 | 287 |
| 290 return { | 288 return { |
| 291 Route: Route, | 289 Route: Route, |
| 292 navigateTo: navigateTo, | 290 navigateTo: navigateTo, |
| 293 getRouteForPath: getRouteForPath, | 291 getRouteForPath: getRouteForPath, |
| 294 }; | 292 }; |
| 295 }); | 293 }); |
| OLD | NEW |