OLD | NEW |
| (Empty) |
1 <!-- | |
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.g
ithub.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/CONTRI
BUTORS.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/PATEN
TS.txt | |
8 --> | |
9 <link rel="import" href="route.html"> | |
10 | |
11 <script> | |
12 (function(scope) { | |
13 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {}; | |
14 | |
15 // Route singletons. | |
16 var routesByPath = {}; | |
17 var pathsByName = {}; | |
18 | |
19 // Route Management | |
20 | |
21 /** | |
22 * Retrieves (or builds) the singleton `Route` for the given path expression or | |
23 * route name. | |
24 * | |
25 * Paths begin with `/`; anything else is considered a name. | |
26 * | |
27 * For convenience, `Route` objects can also be passed (and will be returned) - | |
28 * this can be used as a route coercion function. | |
29 * | |
30 * @param {String|MoreRouting.Route} pathOrName | |
31 * @param {MoreRouting.Route} parent | |
32 * @return {MoreRouting.Route} | |
33 */ | |
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); | |
38 } else { | |
39 return this.getRouteByName(pathOrName); | |
40 } | |
41 } | |
42 | |
43 /** | |
44 * Retrieves (or builds) the singleton `Route` for the given path expression. | |
45 * | |
46 * @param {String} path | |
47 * @param {MoreRouting.Route} parent | |
48 * @return {MoreRouting.Route} | |
49 */ | |
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]); | |
55 } | |
56 return routesByPath[fullPath]; | |
57 } | |
58 | |
59 /** | |
60 * Retrieves the route registered via `name`. | |
61 * | |
62 * @param {String} name | |
63 * @return {MoreRouting.Route} | |
64 */ | |
65 MoreRouting.getRouteByName = function getRouteByName(name) { | |
66 var path = pathsByName[name]; | |
67 if (!path) { | |
68 throw new Error('No route named "' + name + '" has been registered'); | |
69 } | |
70 return this.getRouteByPath(path); | |
71 } | |
72 | |
73 /** | |
74 * @param {String} path | |
75 * @return {MoreRouting.Route} The newly registered route. | |
76 */ | |
77 MoreRouting.registerNamedRoute = function registerNamedRoute(name, path, parent)
{ | |
78 if (pathsByName[name]) { | |
79 console.warn( | |
80 'Overwriting route named "' + name + '" with path:', path, | |
81 'previously:', pathsByName[name]); | |
82 } | |
83 var route = this.getRouteByPath(path, parent); | |
84 pathsByName[name] = route.fullPath; | |
85 return route; | |
86 }; | |
87 | |
88 // Route Shortcuts | |
89 MoreRouting.urlFor = function urlFor(pathOrName, params) { | |
90 return this.getRoute(pathOrName).urlFor(params); | |
91 }; | |
92 | |
93 MoreRouting.navigateTo = function navigateTo(pathOrName, params) { | |
94 return this.getRoute(pathOrName).navigateTo(params); | |
95 }; | |
96 | |
97 MoreRouting.isCurrentUrl = function isCurrentUrl(pathOrName, params) { | |
98 return this.getRoute(pathOrName).isCurrentUrl(params); | |
99 }; | |
100 | |
101 // Utility | |
102 | |
103 /** | |
104 * | |
105 */ | |
106 MoreRouting.isPath = function isPath(pathOrName) { | |
107 return this.Route.isPath(pathOrName); | |
108 } | |
109 | |
110 /** | |
111 * @param {...String} paths | |
112 */ | |
113 MoreRouting.joinPath = function joinPath(paths) { | |
114 return this.Route.joinPath.apply(this.Route, arguments); | |
115 } | |
116 | |
117 // Driver Management | |
118 | |
119 var driver; | |
120 Object.defineProperty(MoreRouting, 'driver', { | |
121 get: function getDriver() { | |
122 if (!driver) { | |
123 throw new Error('No routing driver configured. Did you forget <more-routin
g-config>?'); | |
124 } | |
125 return driver; | |
126 }, | |
127 set: function setDriver(newDriver) { | |
128 if (driver) { | |
129 console.warn('Changing routing drivers is not supported, ignoring. You sho
uld have only one <more-routing-config> on the page!'); | |
130 return; | |
131 } | |
132 driver = newDriver; | |
133 } | |
134 }); | |
135 | |
136 })(window); | |
137 </script> | |
OLD | NEW |