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