2 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9 <link rel=
"import" href=
"route.html">
13 var MoreRouting
= scope
.MoreRouting
= scope
.MoreRouting
|| {};
16 var routesByPath
= {};
22 * Retrieves (or builds) the singleton `Route` for the given path expression or
25 * Paths begin with `/`; anything else is considered a name.
27 * For convenience, `Route` objects can also be passed (and will be returned) -
28 * this can be used as a route coercion function.
30 * @param {String|MoreRouting.Route} pathOrName
31 * @param {MoreRouting.Route} parent
32 * @return {MoreRouting.Route}
34 MoreRouting
.getRoute
= function getRoute(pathOrName
, parent
) {
35 if (typeof pathOrName
!== 'string') return pathOrName
;
36 if (this.isPath(pathOrName
)) {
37 return this.getRouteByPath(pathOrName
, parent
);
39 return this.getRouteByName(pathOrName
);
44 * Retrieves (or builds) the singleton `Route` for the given path expression.
46 * @param {String} path
47 * @param {MoreRouting.Route} parent
48 * @return {MoreRouting.Route}
50 MoreRouting
.getRouteByPath
= function getRouteByPath(path
, parent
) {
51 var fullPath
= (parent
? parent
.fullPath
: '') + path
;
52 if (!routesByPath
[fullPath
]) {
53 routesByPath
[fullPath
] = new this.Route(path
, parent
);
54 this.driver
.manageRoute(routesByPath
[fullPath
]);
56 return routesByPath
[fullPath
];
60 * Retrieves the route registered via `name`.
62 * @param {String} name
63 * @return {MoreRouting.Route}
65 MoreRouting
.getRouteByName
= function getRouteByName(name
) {
66 var path
= pathsByName
[name
];
68 throw new Error('No route named "' + name
+ '" has been registered');
70 return this.getRouteByPath(path
);
74 * @param {String} path
75 * @return {MoreRouting.Route} The newly registered route.
77 MoreRouting
.registerNamedRoute
= function registerNamedRoute(name
, path
, parent
) {
78 if (pathsByName
[name
]) {
80 'Overwriting route named "' + name
+ '" with path:', path
,
81 'previously:', pathsByName
[name
]);
83 var route
= this.getRouteByPath(path
, parent
);
84 pathsByName
[name
] = route
.fullPath
;
89 MoreRouting
.urlFor
= function urlFor(pathOrName
, params
) {
90 return this.getRoute(pathOrName
).urlFor(params
);
93 MoreRouting
.navigateTo
= function navigateTo(pathOrName
, params
) {
94 return this.getRoute(pathOrName
).navigateTo(params
);
97 MoreRouting
.isCurrentUrl
= function isCurrentUrl(pathOrName
, params
) {
98 return this.getRoute(pathOrName
).isCurrentUrl(params
);
106 MoreRouting
.isPath
= function isPath(pathOrName
) {
107 return this.Route
.isPath(pathOrName
);
111 * @param {...String} paths
113 MoreRouting
.joinPath
= function joinPath(paths
) {
114 return this.Route
.joinPath
.apply(this.Route
, arguments
);
120 Object
.defineProperty(MoreRouting
, 'driver', {
121 get: function getDriver() {
123 throw new Error('No routing driver configured. Did you forget <more-routing-config>?');
127 set: function setDriver(newDriver
) {
129 console
.warn('Changing routing drivers is not supported, ignoring. You should have only one <more-routing-config> on the page!');