OLD | NEW |
| (Empty) |
1 <!-- | |
2 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | |
3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | |
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | |
6 Code distributed by Google as part of the polymer project is also | |
7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | |
8 --> | |
9 <script> | |
10 (function(scope) { | |
11 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {}; | |
12 MoreRouting.Driver = Driver; | |
13 | |
14 /** | |
15 * TODO(nevir): Docs. | |
16 */ | |
17 function Driver(opt_config) { | |
18 var config = opt_config || {}; | |
19 if (config.prefix) this.prefix = config.prefix; | |
20 | |
21 this._activeRoutes = []; | |
22 | |
23 this._rootRoutes = []; | |
24 } | |
25 | |
26 Driver.prototype.manageRoute = function manageRoute(route) { | |
27 route.driver = this; | |
28 this._appendRoute(route); | |
29 | |
30 if (route.parent) { | |
31 if (route.parent.active) { | |
32 // Remember: `processPathParts` takes just the path parts relative to that | |
33 // route; not the full set. | |
34 route.processPathParts(this.currentPathParts.slice(route.parent.depth)); | |
35 } | |
36 } else { | |
37 route.processPathParts(this.currentPathParts); | |
38 } | |
39 | |
40 if (route.active) this._activeRoutes.push(route); | |
41 }; | |
42 | |
43 Driver.prototype.urlForParts = function urlForParts(parts) { | |
44 return this.prefix + parts.join(this.separator); | |
45 }; | |
46 | |
47 Driver.prototype.navigateToParts = function(parts) { | |
48 return this.navigateToUrl(this.urlForParts(parts)); | |
49 }; | |
50 | |
51 Driver.prototype.navigateToUrl = function navigateToUrl(url) { | |
52 throw new Error(this.constructor.name + '#navigateToUrl not implemented'); | |
53 }; | |
54 | |
55 // Subclass Interface | |
56 | |
57 Driver.prototype.prefix = '/'; | |
58 Driver.prototype.separator = '/'; | |
59 | |
60 Driver.prototype.setCurrentPath = function setCurrentPath(path) { | |
61 this.currentPathParts = this.splitPath(path); | |
62 var newRoutes = this._matchingRoutes(this.currentPathParts); | |
63 | |
64 // active -> inactive. | |
65 for (var i = 0, route; route = this._activeRoutes[i]; i++) { | |
66 if (newRoutes.indexOf(route) === -1) { | |
67 route.processPathParts(null); | |
68 } | |
69 } | |
70 | |
71 this._activeRoutes = newRoutes; | |
72 } | |
73 | |
74 Driver.prototype.splitPath = function splitPath(rawPath) { | |
75 if (this.prefix && rawPath.indexOf(this.prefix) !== 0) { | |
76 throw new Error( | |
77 'Invalid path "' + rawPath + '"; ' + | |
78 'expected it to be prefixed by "' + this.prefix + '"'); | |
79 } | |
80 var path = rawPath.substr(this.prefix.length); | |
81 var parts = path.split(this.separator); | |
82 // Ignore trailing separators. | |
83 if (!parts[parts.length - 1]) parts.pop(); | |
84 | |
85 return parts; | |
86 }; | |
87 | |
88 // Internal Implementation | |
89 Driver.prototype._appendRoute = function _appendRoute(route) { | |
90 if (route.parent) { | |
91 // We only care about root routes. | |
92 return; | |
93 } | |
94 this._rootRoutes.push(route); | |
95 }; | |
96 | |
97 Driver.prototype._matchingRoutes = function _matchingRoutes(parts, rootRoutes) { | |
98 var routes = []; | |
99 var candidates = rootRoutes || this._rootRoutes; | |
100 var route; | |
101 for (var i = 0; i < candidates.length; i++) { | |
102 route = candidates[i]; | |
103 route.processPathParts(parts); | |
104 if (route.active) { | |
105 routes.push(route); | |
106 routes = routes.concat(this._matchingRoutes(parts.slice(route.compiled.len
gth), route.children)); | |
107 } | |
108 } | |
109 return routes; | |
110 } | |
111 | |
112 })(window); | |
113 </script> | |
OLD | NEW |