Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components-chromium / more-routing / routing-extracted.js
blob008b8fa0eb6db6db67a1035abb2f46d16220b92d
2 (function(scope) {
3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
5 // Route singletons.
6 var routesByPath = {};
7 var pathsByName = {};
9 // Route Management
11 /**
12 * Retrieves (or builds) the singleton `Route` for the given path expression or
13 * route name.
15 * Paths begin with `/`; anything else is considered a name.
17 * For convenience, `Route` objects can also be passed (and will be returned) -
18 * this can be used as a route coercion function.
20 * @param {String|MoreRouting.Route} pathOrName
21 * @param {MoreRouting.Route} parent
22 * @return {MoreRouting.Route}
24 MoreRouting.getRoute = function getRoute(pathOrName, parent) {
25 if (typeof pathOrName !== 'string') return pathOrName;
26 if (this.isPath(pathOrName)) {
27 return this.getRouteByPath(pathOrName, parent);
28 } else {
29 return this.getRouteByName(pathOrName);
33 /**
34 * Retrieves (or builds) the singleton `Route` for the given path expression.
36 * @param {String} path
37 * @param {MoreRouting.Route} parent
38 * @return {MoreRouting.Route}
40 MoreRouting.getRouteByPath = function getRouteByPath(path, parent) {
41 var fullPath = (parent ? parent.fullPath : '') + path;
42 if (!routesByPath[fullPath]) {
43 routesByPath[fullPath] = new this.Route(path, parent);
44 this.driver.manageRoute(routesByPath[fullPath]);
46 return routesByPath[fullPath];
49 /**
50 * Retrieves the route registered via `name`.
52 * @param {String} name
53 * @return {MoreRouting.Route}
55 MoreRouting.getRouteByName = function getRouteByName(name) {
56 var path = pathsByName[name];
57 if (!path) {
58 throw new Error('No route named "' + name + '" has been registered');
60 return this.getRouteByPath(path);
63 /**
64 * @param {String} path
65 * @return {MoreRouting.Route} The newly registered route.
67 MoreRouting.registerNamedRoute = function registerNamedRoute(name, path, parent) {
68 if (pathsByName[name]) {
69 console.warn(
70 'Overwriting route named "' + name + '" with path:', path,
71 'previously:', pathsByName[name]);
73 var route = this.getRouteByPath(path, parent);
74 pathsByName[name] = route.fullPath;
75 return route;
78 // Route Shortcuts
79 MoreRouting.urlFor = function urlFor(pathOrName, params) {
80 return this.getRoute(pathOrName).urlFor(params);
83 MoreRouting.navigateTo = function navigateTo(pathOrName, params) {
84 return this.getRoute(pathOrName).navigateTo(params);
87 MoreRouting.isCurrentUrl = function isCurrentUrl(pathOrName, params) {
88 return this.getRoute(pathOrName).isCurrentUrl(params);
91 // Utility
93 /**
96 MoreRouting.isPath = function isPath(pathOrName) {
97 return this.Route.isPath(pathOrName);
101 * @param {...String} paths
103 MoreRouting.joinPath = function joinPath(paths) {
104 return this.Route.joinPath.apply(this.Route, arguments);
107 // Driver Management
109 var driver;
110 Object.defineProperty(MoreRouting, 'driver', {
111 get: function getDriver() {
112 if (!driver) {
113 throw new Error('No routing driver configured. Did you forget <more-routing-config>?');
115 return driver;
117 set: function setDriver(newDriver) {
118 if (driver) {
119 console.warn('Changing routing drivers is not supported, ignoring. You should have only one <more-routing-config> on the page!');
120 return;
122 driver = newDriver;
126 })(window);