Service workers: Allow HTTPS pages arrived at via HTTP redirect to use SW
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components-chromium / more-routing / driver-extracted.js
blob7fbd1e47cef58422ce083ec70c4ca9d47c259b61
1 (function(scope) {
2 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
3 MoreRouting.Driver = Driver;
5 /**
6  * TODO(nevir): Docs.
7  */
8 function Driver(opt_config) {
9   var config = opt_config || {};
10   if (config.prefix) this.prefix = config.prefix;
12   this._activeRoutes = [];
14   this._rootRoutes = [];
17 Driver.prototype.manageRoute = function manageRoute(route) {
18   route.driver = this;
19   this._appendRoute(route);
21   if (route.parent) {
22     if (route.parent.active) {
23       // Remember: `processPathParts` takes just the path parts relative to that
24       // route; not the full set.
25       route.processPathParts(this.currentPathParts.slice(route.parent.depth));
26     }
27   } else {
28     route.processPathParts(this.currentPathParts);
29   }
31   if (route.active) this._activeRoutes.push(route);
34 Driver.prototype.urlForParts = function urlForParts(parts) {
35   return this.prefix + parts.join(this.separator);
38 Driver.prototype.navigateToParts = function(parts) {
39   return this.navigateToUrl(this.urlForParts(parts));
42 Driver.prototype.navigateToUrl = function navigateToUrl(url) {
43   throw new Error(this.constructor.name + '#navigateToUrl not implemented');
46 // Subclass Interface
48 Driver.prototype.prefix = '/';
49 Driver.prototype.separator = '/';
51 Driver.prototype.setCurrentPath = function setCurrentPath(path) {
52   this.currentPathParts = this.splitPath(path);
53   var newRoutes = this._matchingRoutes(this.currentPathParts);
55   // active -> inactive.
56   for (var i = 0, route; route = this._activeRoutes[i]; i++) {
57     if (newRoutes.indexOf(route) === -1) {
58       route.processPathParts(null);
59     }
60   }
62   this._activeRoutes = newRoutes;
65 Driver.prototype.splitPath = function splitPath(rawPath) {
66   if (this.prefix && rawPath.indexOf(this.prefix) !== 0) {
67     throw new Error(
68         'Invalid path "' + rawPath + '"; ' +
69         'expected it to be prefixed by "' + this.prefix + '"');
70   }
71   var path  = rawPath.substr(this.prefix.length);
72   var parts = path.split(this.separator);
73   // Ignore trailing separators.
74   if (!parts[parts.length - 1]) parts.pop();
76   return parts;
79 // Internal Implementation
80 Driver.prototype._appendRoute = function _appendRoute(route) {
81   if (route.parent) {
82     // We only care about root routes.
83     return;
84   }
85   this._rootRoutes.push(route);
88 Driver.prototype._matchingRoutes = function _matchingRoutes(parts, rootRoutes) {
89   var routes = [];
90   var candidates = rootRoutes || this._rootRoutes;
91   var route;
92   for (var i = 0; i < candidates.length; i++) {
93     route = candidates[i];
94     route.processPathParts(parts);
95     if (route.active) {
96       routes.push(route);
97       routes = routes.concat(this._matchingRoutes(parts.slice(route.compiled.length), route.children));
98     }
99   }
100   return routes;
103 })(window);