proxy-based Twister client written with react-js
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.

202 lines
8.1 KiB

10 years ago
'use strict';
10 years ago
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
10 years ago
var assign = require('react/lib/Object.assign');
var invariant = require('invariant');
var warning = require('./warning');
10 years ago
var PathUtils = require('./PathUtils');
var _currentRoute;
var Route = (function () {
_createClass(Route, null, [{
10 years ago
key: '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;
}
10 years ago
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);
}
10 years ago
} else if (parentRoute) {
// Relative paths extend their parent.
path = PathUtils.join(parentRoute.path, path);
} else {
10 years ago
path = '/' + path;
}
10 years ago
} else {
path = parentRoute ? parentRoute.path : '/';
}
10 years ago
if (options.isNotFound && !/\*$/.test(path)) path += '*'; // Auto-append * to the path of not found routes.
10 years ago
var route = new Route(name, path, options.ignoreScrollBehavior, options.isDefault, options.isNotFound, options.onEnter, options.onLeave, options.handler);
10 years ago
if (parentRoute) {
if (route.isDefault) {
invariant(parentRoute.defaultRoute == null, '%s may not have more than one default route', parentRoute);
10 years ago
parentRoute.defaultRoute = route;
} else if (route.isNotFound) {
invariant(parentRoute.notFoundRoute == null, '%s may not have more than one not found route', parentRoute);
10 years ago
parentRoute.notFoundRoute = route;
}
10 years ago
parentRoute.appendChild(route);
}
10 years ago
// 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;
}
10 years ago
return route;
}
/**
* Creates and returns a route that is rendered when its parent matches
* the current URL.
*/
}, {
key: 'createDefaultRoute',
10 years ago
value: function createDefaultRoute(options) {
return Route.createRoute(assign({}, options, { isDefault: true }));
}
/**
* Creates and returns a route that is rendered when its parent matches
* the current URL but none of its siblings do.
*/
}, {
key: 'createNotFoundRoute',
10 years ago
value: function createNotFoundRoute(options) {
return Route.createRoute(assign({}, options, { isNotFound: true }));
}
/**
* 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
*/
}, {
key: 'createRedirect',
10 years ago
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);
}
}));
}
10 years ago
}]);
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;
}
/**
* Appends the given route to this route's child routes.
*/
_createClass(Route, [{
key: 'appendChild',
value: function appendChild(route) {
invariant(route instanceof Route, 'route.appendChild must use a valid Route');
if (!this.childRoutes) this.childRoutes = [];
this.childRoutes.push(route);
}
}, {
key: 'toString',
value: function toString() {
var string = '<Route';
if (this.name) string += ' name="' + this.name + '"';
string += ' path="' + this.path + '">';
return string;
}
}]);
return Route;
})();
module.exports = Route;