UNPKG

react-crossroads

Version:

Client side router for web applications built with React and utilizing the Flux architecture. The backing routing engine is CrossroadsJs.

118 lines (94 loc) 3.62 kB
var Logger, React, Redirect, RedirectChain, RouteDefinition, TYPE, factory, join, merge, pattern, _, _onlyOne, __slice = [].slice, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; React = require('react'); RouteDefinition = require('./RouteDefinition'); Logger = require('../utils/logger'); RedirectChain = require('./RedirectChain'); pattern = require('crossroads').patternLexer; merge = require('react/lib/merge'); join = require('path').join; _ = require('lodash'); TYPE = 'Redirect'; _onlyOne = function(restrictedPropNames) { var message; message = (function() { var last, list, propNamesString, _i; list = 2 <= restrictedPropNames.length ? __slice.call(restrictedPropNames, 0, _i = restrictedPropNames.length - 1) : (_i = 0, []), last = restrictedPropNames[_i++]; propNamesString = list.join(', '); if (last != null) { propNamesString += ", or " + last; } return "Must provide either " + propNamesString + " but only one of them"; })(); return function(props, propName) { var assigned; assigned = restrictedPropNames.map((function(_this) { return function(n) { if (props[n] != null) { return 1; } else { return 0; } }; })(this)).reduce(function(x, y) { return x + y; }); if (assigned !== 1) { return new Error(message); } return React.PropTypes.string.apply(null, arguments); }; }; Redirect = (function(_super) { __extends(Redirect, _super); Redirect.prototype.type = TYPE; Redirect.prototype.priority = 1; function Redirect(props) { this.props = props; Redirect.__super__.constructor.call(this); this.name = "Redirect-FROM[" + (this.props.from || this.props.fromPath) + "]TO[" + (this.props.to || this.props.path) + "]"; } Redirect.prototype.propTypes = { from: _onlyOne(['from', 'fromPath']), fromPath: _onlyOne(['from', 'fromPath']), to: _onlyOne(['to', 'path']), path: _onlyOne(['to', 'path']) }; Redirect.prototype.resolveRoutePath = function(name, path, prefix) { var route; if (path != null) { if (path[0] === '/') { return path; } else { return join(prefix, path); } } route = this.routeStore.getRoute(name); if (route == null) { Logger.development.error("Redirect to/from `" + name + "` could not be registered since there are no registered routes by that name."); return; } return route.endpoint.path; }; Redirect.prototype.register = function(parents, routePrefix, routeStore) { this.routeStore = routeStore; this.chain = __slice.call(parents).concat([this]); this.path = this.resolveRoutePath(this.props.from, this.props.fromPath, routePrefix); this.toPath = this.resolveRoutePath(this.props.to, this.props.path, routePrefix); return this.routeStore.register(this); }; Redirect.prototype.createActiveChain = function(request, params) { return new RedirectChain(this.routeStore.routerActions, pattern.interpolate(this.toPath, params), this.routeStore.getCurrentChain()); }; return Redirect; })(RouteDefinition); factory = function(props) { if (arguments.length > 1) { throw new Error('Redirect does not support children'); } return new Redirect(props); }; factory.type = TYPE; module.exports = factory;