You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
8.2 KiB
207 lines
8.2 KiB
"use strict"; |
|
|
|
var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); |
|
|
|
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; |
|
|
|
var assign = require("react/lib/Object.assign"); |
|
var invariant = require("react/lib/invariant"); |
|
var warning = require("react/lib/warning"); |
|
var PathUtils = require("./PathUtils"); |
|
|
|
var _currentRoute; |
|
|
|
var Route = (function () { |
|
function Route(name, path, ignoreScrollBehavior, isDefault, isNotFound, onEnter, onLeave, handler) { |
|
_classCallCheck(this, Route); |
|
|
|
this.name = name; |
|
this.path = path; |
|
this.paramNames = PathUtils.extractParamNames(this.path); |
|
this.ignoreScrollBehavior = !!ignoreScrollBehavior; |
|
this.isDefault = !!isDefault; |
|
this.isNotFound = !!isNotFound; |
|
this.onEnter = onEnter; |
|
this.onLeave = onLeave; |
|
this.handler = handler; |
|
} |
|
|
|
_createClass(Route, { |
|
appendChild: { |
|
|
|
/** |
|
* Appends the given route to this route's child routes. |
|
*/ |
|
|
|
value: function appendChild(route) { |
|
invariant(route instanceof Route, "route.appendChild must use a valid Route"); |
|
|
|
if (!this.childRoutes) this.childRoutes = []; |
|
|
|
this.childRoutes.push(route); |
|
} |
|
}, |
|
toString: { |
|
value: function toString() { |
|
var string = "<Route"; |
|
|
|
if (this.name) string += " name=\"" + this.name + "\""; |
|
|
|
string += " path=\"" + this.path + "\">"; |
|
|
|
return string; |
|
} |
|
} |
|
}, { |
|
createRoute: { |
|
|
|
/** |
|
* Creates and returns a new route. Options may be a URL pathname string |
|
* with placeholders for named params or an object with any of the following |
|
* properties: |
|
* |
|
* - name The name of the route. This is used to lookup a |
|
* route relative to its parent route and should be |
|
* unique among all child routes of the same parent |
|
* - path A URL pathname string with optional placeholders |
|
* that specify the names of params to extract from |
|
* the URL when the path matches. Defaults to `/${name}` |
|
* when there is a name given, or the path of the parent |
|
* route, or / |
|
* - ignoreScrollBehavior True to make this route (and all descendants) ignore |
|
* the scroll behavior of the router |
|
* - isDefault True to make this route the default route among all |
|
* its siblings |
|
* - isNotFound True to make this route the "not found" route among |
|
* all its siblings |
|
* - onEnter A transition hook that will be called when the |
|
* router is going to enter this route |
|
* - onLeave A transition hook that will be called when the |
|
* router is going to leave this route |
|
* - handler A React component that will be rendered when |
|
* this route is active |
|
* - parentRoute The parent route to use for this route. This option |
|
* is automatically supplied when creating routes inside |
|
* the callback to another invocation of createRoute. You |
|
* only ever need to use this when declaring routes |
|
* independently of one another to manually piece together |
|
* the route hierarchy |
|
* |
|
* The callback may be used to structure your route hierarchy. Any call to |
|
* createRoute, createDefaultRoute, createNotFoundRoute, or createRedirect |
|
* inside the callback automatically uses this route as its parent. |
|
*/ |
|
|
|
value: function createRoute(options, callback) { |
|
options = options || {}; |
|
|
|
if (typeof options === "string") options = { path: options }; |
|
|
|
var parentRoute = _currentRoute; |
|
|
|
if (parentRoute) { |
|
warning(options.parentRoute == null || options.parentRoute === parentRoute, "You should not use parentRoute with createRoute inside another route's child callback; it is ignored"); |
|
} else { |
|
parentRoute = options.parentRoute; |
|
} |
|
|
|
var name = options.name; |
|
var path = options.path || name; |
|
|
|
if (path && !(options.isDefault || options.isNotFound)) { |
|
if (PathUtils.isAbsolute(path)) { |
|
if (parentRoute) { |
|
invariant(path === parentRoute.path || parentRoute.paramNames.length === 0, "You cannot nest path \"%s\" inside \"%s\"; the parent requires URL parameters", path, parentRoute.path); |
|
} |
|
} else if (parentRoute) { |
|
// Relative paths extend their parent. |
|
path = PathUtils.join(parentRoute.path, path); |
|
} else { |
|
path = "/" + path; |
|
} |
|
} else { |
|
path = parentRoute ? parentRoute.path : "/"; |
|
} |
|
|
|
if (options.isNotFound && !/\*$/.test(path)) path += "*"; // Auto-append * to the path of not found routes. |
|
|
|
var route = new Route(name, path, options.ignoreScrollBehavior, options.isDefault, options.isNotFound, options.onEnter, options.onLeave, options.handler); |
|
|
|
if (parentRoute) { |
|
if (route.isDefault) { |
|
invariant(parentRoute.defaultRoute == null, "%s may not have more than one default route", parentRoute); |
|
|
|
parentRoute.defaultRoute = route; |
|
} else if (route.isNotFound) { |
|
invariant(parentRoute.notFoundRoute == null, "%s may not have more than one not found route", parentRoute); |
|
|
|
parentRoute.notFoundRoute = route; |
|
} |
|
|
|
parentRoute.appendChild(route); |
|
} |
|
|
|
// Any routes created in the callback |
|
// use this route as their parent. |
|
if (typeof callback === "function") { |
|
var currentRoute = _currentRoute; |
|
_currentRoute = route; |
|
callback.call(route, route); |
|
_currentRoute = currentRoute; |
|
} |
|
|
|
return route; |
|
} |
|
}, |
|
createDefaultRoute: { |
|
|
|
/** |
|
* Creates and returns a route that is rendered when its parent matches |
|
* the current URL. |
|
*/ |
|
|
|
value: function createDefaultRoute(options) { |
|
return Route.createRoute(assign({}, options, { isDefault: true })); |
|
} |
|
}, |
|
createNotFoundRoute: { |
|
|
|
/** |
|
* Creates and returns a route that is rendered when its parent matches |
|
* the current URL but none of its siblings do. |
|
*/ |
|
|
|
value: function createNotFoundRoute(options) { |
|
return Route.createRoute(assign({}, options, { isNotFound: true })); |
|
} |
|
}, |
|
createRedirect: { |
|
|
|
/** |
|
* Creates and returns a route that automatically redirects the transition |
|
* to another route. In addition to the normal options to createRoute, this |
|
* function accepts the following options: |
|
* |
|
* - from An alias for the `path` option. Defaults to * |
|
* - to The path/route/route name to redirect to |
|
* - params The params to use in the redirect URL. Defaults |
|
* to using the current params |
|
* - query The query to use in the redirect URL. Defaults |
|
* to using the current query |
|
*/ |
|
|
|
value: function createRedirect(options) { |
|
return Route.createRoute(assign({}, options, { |
|
path: options.path || options.from || "*", |
|
onEnter: function onEnter(transition, params, query) { |
|
transition.redirect(options.to, options.params || params, options.query || query); |
|
} |
|
})); |
|
} |
|
} |
|
}); |
|
|
|
return Route; |
|
})(); |
|
|
|
module.exports = Route; |