2 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
3 MoreRouting.Driver = Driver;
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) {
19 this._appendRoute(route);
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));
28 route.processPathParts(this.currentPathParts);
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');
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);
62 this._activeRoutes = newRoutes;
65 Driver.prototype.splitPath = function splitPath(rawPath) {
66 if (this.prefix && rawPath.indexOf(this.prefix) !== 0) {
68 'Invalid path "' + rawPath + '"; ' +
69 'expected it to be prefixed by "' + this.prefix + '"');
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();
79 // Internal Implementation
80 Driver.prototype._appendRoute = function _appendRoute(route) {
82 // We only care about root routes.
85 this._rootRoutes.push(route);
88 Driver.prototype._matchingRoutes = function _matchingRoutes(parts, rootRoutes) {
90 var candidates = rootRoutes || this._rootRoutes;
92 for (var i = 0; i < candidates.length; i++) {
93 route = candidates[i];
94 route.processPathParts(parts);
97 routes = routes.concat(this._matchingRoutes(parts.slice(route.compiled.length), route.children));