| OLD | NEW |
| (Empty) |
| 1 | |
| 2 (function(scope) { | |
| 3 var MoreRouting = scope.MoreRouting = scope.MoreRouting || {}; | |
| 4 | |
| 5 // Route singletons. | |
| 6 var routesByPath = {}; | |
| 7 var pathsByName = {}; | |
| 8 | |
| 9 // Route Management | |
| 10 | |
| 11 /** | |
| 12 * Retrieves (or builds) the singleton `Route` for the given path expression or | |
| 13 * route name. | |
| 14 * | |
| 15 * Paths begin with `/`; anything else is considered a name. | |
| 16 * | |
| 17 * For convenience, `Route` objects can also be passed (and will be returned) - | |
| 18 * this can be used as a route coercion function. | |
| 19 * | |
| 20 * @param {String|MoreRouting.Route} pathOrName | |
| 21 * @param {MoreRouting.Route} parent | |
| 22 * @return {MoreRouting.Route} | |
| 23 */ | |
| 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); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * Retrieves (or builds) the singleton `Route` for the given path expression. | |
| 35 * | |
| 36 * @param {String} path | |
| 37 * @param {MoreRouting.Route} parent | |
| 38 * @return {MoreRouting.Route} | |
| 39 */ | |
| 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]); | |
| 45 } | |
| 46 return routesByPath[fullPath]; | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Retrieves the route registered via `name`. | |
| 51 * | |
| 52 * @param {String} name | |
| 53 * @return {MoreRouting.Route} | |
| 54 */ | |
| 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'); | |
| 59 } | |
| 60 return this.getRouteByPath(path); | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * @param {String} path | |
| 65 * @return {MoreRouting.Route} The newly registered route. | |
| 66 */ | |
| 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]); | |
| 72 } | |
| 73 var route = this.getRouteByPath(path, parent); | |
| 74 pathsByName[name] = route.fullPath; | |
| 75 return route; | |
| 76 }; | |
| 77 | |
| 78 // Route Shortcuts | |
| 79 MoreRouting.urlFor = function urlFor(pathOrName, params) { | |
| 80 return this.getRoute(pathOrName).urlFor(params); | |
| 81 }; | |
| 82 | |
| 83 MoreRouting.navigateTo = function navigateTo(pathOrName, params) { | |
| 84 return this.getRoute(pathOrName).navigateTo(params); | |
| 85 }; | |
| 86 | |
| 87 MoreRouting.isCurrentUrl = function isCurrentUrl(pathOrName, params) { | |
| 88 return this.getRoute(pathOrName).isCurrentUrl(params); | |
| 89 }; | |
| 90 | |
| 91 // Utility | |
| 92 | |
| 93 /** | |
| 94 * | |
| 95 */ | |
| 96 MoreRouting.isPath = function isPath(pathOrName) { | |
| 97 return this.Route.isPath(pathOrName); | |
| 98 } | |
| 99 | |
| 100 /** | |
| 101 * @param {...String} paths | |
| 102 */ | |
| 103 MoreRouting.joinPath = function joinPath(paths) { | |
| 104 return this.Route.joinPath.apply(this.Route, arguments); | |
| 105 } | |
| 106 | |
| 107 // Driver Management | |
| 108 | |
| 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-routin
g-config>?'); | |
| 114 } | |
| 115 return driver; | |
| 116 }, | |
| 117 set: function setDriver(newDriver) { | |
| 118 if (driver) { | |
| 119 console.warn('Changing routing drivers is not supported, ignoring. You sho
uld have only one <more-routing-config> on the page!'); | |
| 120 return; | |
| 121 } | |
| 122 driver = newDriver; | |
| 123 } | |
| 124 }); | |
| 125 | |
| 126 })(window); | |
| OLD | NEW |