forked from googlearchive/more-routing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouting.html
137 lines (121 loc) · 3.77 KB
/
routing.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!--
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="route.html">
<script>
(function(scope) {
var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
// Route singletons.
var routesByPath = {};
var pathsByName = {};
// Route Management
/**
* Retrieves (or builds) the singleton `Route` for the given path expression or
* route name.
*
* Paths begin with `/`; anything else is considered a name.
*
* For convenience, `Route` objects can also be passed (and will be returned) -
* this can be used as a route coercion function.
*
* @param {String|MoreRouting.Route} pathOrName
* @param {MoreRouting.Route} parent
* @return {MoreRouting.Route}
*/
MoreRouting.getRoute = function getRoute(pathOrName, parent) {
if (typeof pathOrName !== 'string') return pathOrName;
if (this.isPath(pathOrName)) {
return this.getRouteByPath(pathOrName, parent);
} else {
return this.getRouteByName(pathOrName);
}
}
/**
* Retrieves (or builds) the singleton `Route` for the given path expression.
*
* @param {String} path
* @param {MoreRouting.Route} parent
* @return {MoreRouting.Route}
*/
MoreRouting.getRouteByPath = function getRouteByPath(path, parent) {
var fullPath = (parent ? parent.fullPath : '') + path;
if (!routesByPath[fullPath]) {
routesByPath[fullPath] = new this.Route(path, parent);
this.driver.manageRoute(routesByPath[fullPath]);
}
return routesByPath[fullPath];
}
/**
* Retrieves the route registered via `name`.
*
* @param {String} name
* @return {MoreRouting.Route}
*/
MoreRouting.getRouteByName = function getRouteByName(name) {
var path = pathsByName[name];
if (!path) {
throw new Error('No route named "' + name + '" has been registered');
}
return this.getRouteByPath(path);
}
/**
* @param {String} path
* @return {MoreRouting.Route} The newly registered route.
*/
MoreRouting.registerNamedRoute = function registerNamedRoute(name, path, parent) {
if (pathsByName[name]) {
console.warn(
'Overwriting route named "' + name + '" with path:', path,
'previously:', pathsByName[name]);
}
var route = this.getRouteByPath(path, parent);
pathsByName[name] = route.fullPath;
return route;
};
// Route Shortcuts
MoreRouting.urlFor = function urlFor(pathOrName, params) {
return this.getRoute(pathOrName).urlFor(params);
};
MoreRouting.navigateTo = function navigateTo(pathOrName, params) {
return this.getRoute(pathOrName).navigateTo(params);
};
MoreRouting.isCurrentUrl = function isCurrentUrl(pathOrName, params) {
return this.getRoute(pathOrName).isCurrentUrl(params);
};
// Utility
/**
*
*/
MoreRouting.isPath = function isPath(pathOrName) {
return this.Route.isPath(pathOrName);
}
/**
* @param {...String} paths
*/
MoreRouting.joinPath = function joinPath(paths) {
return this.Route.joinPath.apply(this.Route, arguments);
}
// Driver Management
var driver;
Object.defineProperty(MoreRouting, 'driver', {
get: function getDriver() {
if (!driver) {
throw new Error('No routing driver configured. Did you forget <more-routing-config>?');
}
return driver;
},
set: function setDriver(newDriver) {
if (driver) {
console.warn('Changing routing drivers is not supported, ignoring. You should have only one <more-routing-config> on the page!');
return;
}
driver = newDriver;
}
});
})(window);
</script>