3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
4 MoreRouting.Driver = Driver;
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) {
20 this._appendRoute(route);
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));
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');
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) {
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();
80 // Internal Implementation
81 Driver.prototype._appendRoute = function _appendRoute(route) {
83 // We only care about root routes.
86 this._rootRoutes.push(route);
89 Driver.prototype._matchingRoutes = function _matchingRoutes(parts, rootRoutes) {
91 var candidates = rootRoutes || this._rootRoutes;
93 for (var i = 0; i < candidates.length; i++) {
94 route = candidates[i];
95 route.processPathParts(parts);
98 routes = routes.concat(this._matchingRoutes(parts.slice(route.compiled.length), route.children));