ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components-chromium / more-routing / driver-extracted.js
blobeaf1862f9f46745caa878aeecc033dcad162577b
2 (function(scope) {
3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
4 MoreRouting.Driver = Driver;
6 /**
7 * TODO(nevir): Docs.
8 */
9 function Driver(opt_config) {
10 var config = opt_config || {};
11 if (config.prefix) this.prefix = config.prefix;
13 this._activeRoutes = [];
15 this._rootRoutes = [];
18 Driver.prototype.manageRoute = function manageRoute(route) {
19 route.driver = this;
20 this._appendRoute(route);
22 if (route.parent) {
23 if (route.parent.active) {
24 // Remember: `processPathParts` takes just the path parts relative to that
25 // route; not the full set.
26 route.processPathParts(this.currentPathParts.slice(route.parent.depth));
28 } else {
29 route.processPathParts(this.currentPathParts);
32 if (route.active) this._activeRoutes.push(route);
35 Driver.prototype.urlForParts = function urlForParts(parts) {
36 return this.prefix + parts.join(this.separator);
39 Driver.prototype.navigateToParts = function(parts) {
40 return this.navigateToUrl(this.urlForParts(parts));
43 Driver.prototype.navigateToUrl = function navigateToUrl(url) {
44 throw new Error(this.constructor.name + '#navigateToUrl not implemented');
47 // Subclass Interface
49 Driver.prototype.prefix = '/';
50 Driver.prototype.separator = '/';
52 Driver.prototype.setCurrentPath = function setCurrentPath(path) {
53 this.currentPathParts = this.splitPath(path);
54 var newRoutes = this._matchingRoutes(this.currentPathParts);
56 // active -> inactive.
57 for (var i = 0, route; route = this._activeRoutes[i]; i++) {
58 if (newRoutes.indexOf(route) === -1) {
59 route.processPathParts(null);
63 this._activeRoutes = newRoutes;
66 Driver.prototype.splitPath = function splitPath(rawPath) {
67 if (this.prefix && rawPath.indexOf(this.prefix) !== 0) {
68 throw new Error(
69 'Invalid path "' + rawPath + '"; ' +
70 'expected it to be prefixed by "' + this.prefix + '"');
72 var path = rawPath.substr(this.prefix.length);
73 var parts = path.split(this.separator);
74 // Ignore trailing separators.
75 if (!parts[parts.length - 1]) parts.pop();
77 return parts;
80 // Internal Implementation
81 Driver.prototype._appendRoute = function _appendRoute(route) {
82 if (route.parent) {
83 // We only care about root routes.
84 return;
86 this._rootRoutes.push(route);
89 Driver.prototype._matchingRoutes = function _matchingRoutes(parts, rootRoutes) {
90 var routes = [];
91 var candidates = rootRoutes || this._rootRoutes;
92 var route;
93 for (var i = 0; i < candidates.length; i++) {
94 route = candidates[i];
95 route.processPathParts(parts);
96 if (route.active) {
97 routes.push(route);
98 routes = routes.concat(this._matchingRoutes(parts.slice(route.compiled.length), route.children));
101 return routes;
104 })(window);