react-crossroads
Version:
Client side router for web applications built with React and utilizing the Flux architecture. The backing routing engine is CrossroadsJs.
110 lines (91 loc) • 2.98 kB
JavaScript
var ANONYMOUS, ActiveEndpoint, Logger, React, RouteChain, RouteDefinition, componentClass, createChainableTypeChecker, join,
__slice = [].slice;
React = require('react');
Logger = require('../utils/logger');
ActiveEndpoint = require('./ActiveEndpoint');
RouteChain = require('./RouteChain');
join = require('path').join;
ANONYMOUS = '<<anonymous>>';
createChainableTypeChecker = function(validate) {
var chainedCheckType, checkType;
checkType = function(isRequired, props, propName, componentName) {
componentName = componentName || ANONYMOUS;
if (props[propName] == null) {
if (isRequired) {
return new Error("Required prop `" + propName + "` was not specified in `" + componentName + "`.");
}
} else {
return validate(props, propName, componentName);
}
};
chainedCheckType = checkType.bind(null, false);
chainedCheckType.isRequired = checkType.bind(null, true);
return chainedCheckType;
};
componentClass = (function() {
var validate;
validate = function(props, propName, componentName) {
if (!React.isValidClass(props[propName])) {
return new Error("Invalid prop `" + propName + "` supplied to " + componentName + ", expected a valid React class.");
}
};
return createChainableTypeChecker(validate);
})();
RouteDefinition = (function() {
function RouteDefinition() {
this.validate();
this.name = this.props.name || this.props.path;
}
RouteDefinition.prototype.validate = function() {
var err, error, propName, _results;
if (this.propTypes == null) {
return;
}
_results = [];
for (propName in this.propTypes) {
try {
error = this.propTypes[propName](this.props, propName, this.type);
} catch (_error) {
err = _error;
error = err;
}
if (error != null) {
Logger.development.error(error);
}
_results.push(null);
}
return _results;
};
RouteDefinition.prototype.buildPath = function(_arg, prefix) {
var name, path;
name = _arg.name, path = _arg.path;
if (path != null) {
if (path[0] === '/') {
return path;
} else {
return join(prefix, path);
}
} else if (name != null) {
return join(prefix, name);
} else {
return prefix;
}
};
RouteDefinition.prototype.register = function(parents, routePrefix, routeStore) {
this.path = this.buildPath(this.props, routePrefix);
this.chain = __slice.call(parents).concat([this]);
return routeStore.register(this);
};
RouteDefinition.prototype.makePath = function(params) {
return this.route.interpolate(params);
};
RouteDefinition.prototype.createActiveChain = function(path, params) {
return new ActiveEndpoint(path, this, params);
};
return RouteDefinition;
})();
RouteDefinition.PropTypes = {
componentClass: componentClass,
createChainableTypeChecker: createChainableTypeChecker
};
module.exports = RouteDefinition;