Skip to content

Commit

Permalink
Use ES6 notation for Route static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Feb 24, 2015
1 parent 98909f0 commit 0643ce3
Showing 1 changed file with 168 additions and 168 deletions.
336 changes: 168 additions & 168 deletions modules/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,176 @@ var invariant = require('react/lib/invariant');
var warning = require('react/lib/warning');
var PathUtils = require('./PathUtils');

var _currentRoute;

class Route {

/**
* 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.
*/
static 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) {
if (PathUtils.isAbsolute(path)) {
if (parentRoute) {
invariant(
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;
}

/**
* Creates and returns a route that is rendered when its parent matches
* the current URL.
*/
static 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.
*/
static 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
*/
static createRedirect(options) {
return Route.createRoute(
assign({}, options, {
path: options.path || options.from || '*',
onEnter: function (transition, params, query) {
transition.redirect(options.to, options.params || params, options.query || query);
}
})
);
}

constructor(name, path, ignoreScrollBehavior, isDefault, isNotFound, onEnter, onLeave, handler) {
this.name = name;
this.path = path;
Expand Down Expand Up @@ -45,172 +213,4 @@ class Route {

}

var _currentRoute;

/**
* 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.
*/
Route.createRoute = function (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) {
if (PathUtils.isAbsolute(path)) {
if (parentRoute) {
invariant(
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;
};

/**
* Creates and returns a route that is rendered when its parent matches
* the current URL.
*/
Route.createDefaultRoute = function (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.
*/
Route.createNotFoundRoute = function (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
*/
Route.createRedirect = function (options) {
return Route.createRoute(
assign({}, options, {
path: options.path || options.from || '*',
onEnter: function (transition, params, query) {
transition.redirect(options.to, options.params || params, options.query || query);
}
})
);
};

module.exports = Route;

0 comments on commit 0643ce3

Please sign in to comment.