ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components / more-routing / routing.html
blob0b1cf5092f9d20b96d33fc9f235cae946d36e3f6
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.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
8 -->
9 <link rel="import" href="route.html">
11 <script>
12 (function(scope) {
13 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
15 // Route singletons.
16 var routesByPath = {};
17 var pathsByName = {};
19 // Route Management
21 /**
22 * Retrieves (or builds) the singleton `Route` for the given path expression or
23 * route name.
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);
38 } else {
39 return this.getRouteByName(pathOrName);
43 /**
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];
59 /**
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];
67 if (!path) {
68 throw new Error('No route named "' + name + '" has been registered');
70 return this.getRouteByPath(path);
73 /**
74 * @param {String} path
75 * @return {MoreRouting.Route} The newly registered route.
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]);
83 var route = this.getRouteByPath(path, parent);
84 pathsByName[name] = route.fullPath;
85 return route;
88 // Route Shortcuts
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);
101 // Utility
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);
117 // Driver Management
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-routing-config>?');
125 return driver;
127 set: function setDriver(newDriver) {
128 if (driver) {
129 console.warn('Changing routing drivers is not supported, ignoring. You should have only one <more-routing-config> on the page!');
130 return;
132 driver = newDriver;
136 })(window);
137 </script>