| OLD | NEW |
| (Empty) |
| 1 | |
| 2 (function(scope) { | |
| 3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {}; | |
| 4 MoreRouting.Driver = Driver; | |
| 5 | |
| 6 /** | |
| 7 * TODO(nevir): Docs. | |
| 8 */ | |
| 9 function Driver(opt_config) { | |
| 10 var config = opt_config || {}; | |
| 11 if (config.prefix) this.prefix = config.prefix; | |
| 12 | |
| 13 this._activeRoutes = []; | |
| 14 | |
| 15 this._rootRoutes = []; | |
| 16 } | |
| 17 | |
| 18 Driver.prototype.manageRoute = function manageRoute(route) { | |
| 19 route.driver = this; | |
| 20 this._appendRoute(route); | |
| 21 | |
| 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)); | |
| 27 } | |
| 28 } else { | |
| 29 route.processPathParts(this.currentPathParts); | |
| 30 } | |
| 31 | |
| 32 if (route.active) this._activeRoutes.push(route); | |
| 33 }; | |
| 34 | |
| 35 Driver.prototype.urlForParts = function urlForParts(parts) { | |
| 36 return this.prefix + parts.join(this.separator); | |
| 37 }; | |
| 38 | |
| 39 Driver.prototype.navigateToParts = function(parts) { | |
| 40 return this.navigateToUrl(this.urlForParts(parts)); | |
| 41 }; | |
| 42 | |
| 43 Driver.prototype.navigateToUrl = function navigateToUrl(url) { | |
| 44 throw new Error(this.constructor.name + '#navigateToUrl not implemented'); | |
| 45 }; | |
| 46 | |
| 47 // Subclass Interface | |
| 48 | |
| 49 Driver.prototype.prefix = '/'; | |
| 50 Driver.prototype.separator = '/'; | |
| 51 | |
| 52 Driver.prototype.setCurrentPath = function setCurrentPath(path) { | |
| 53 this.currentPathParts = this.splitPath(path); | |
| 54 var newRoutes = this._matchingRoutes(this.currentPathParts); | |
| 55 | |
| 56 // active -> inactive. | |
| 57 for (var i = 0, route; route = this._activeRoutes[i]; i++) { | |
| 58 if (newRoutes.indexOf(route) === -1) { | |
| 59 route.processPathParts(null); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 this._activeRoutes = newRoutes; | |
| 64 } | |
| 65 | |
| 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 + '"'); | |
| 71 } | |
| 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(); | |
| 76 | |
| 77 return parts; | |
| 78 }; | |
| 79 | |
| 80 // Internal Implementation | |
| 81 Driver.prototype._appendRoute = function _appendRoute(route) { | |
| 82 if (route.parent) { | |
| 83 // We only care about root routes. | |
| 84 return; | |
| 85 } | |
| 86 this._rootRoutes.push(route); | |
| 87 }; | |
| 88 | |
| 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.len
gth), route.children)); | |
| 99 } | |
| 100 } | |
| 101 return routes; | |
| 102 } | |
| 103 | |
| 104 })(window); | |
| OLD | NEW |