From 0643ce375d1f140360ec07143e2d077145849eeb Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Tue, 24 Feb 2015 13:18:12 -0800 Subject: [PATCH] Use ES6 notation for Route static methods --- modules/Route.js | 336 +++++++++++++++++++++++------------------------ 1 file changed, 168 insertions(+), 168 deletions(-) diff --git a/modules/Route.js b/modules/Route.js index f7c4d94235..d3d69bdf10 100644 --- a/modules/Route.js +++ b/modules/Route.js @@ -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; @@ -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;