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.
108 lines
3.8 KiB
108 lines
3.8 KiB
10 years ago
|
'use strict';
|
||
|
|
||
|
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
|
||
|
|
||
|
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; }; })();
|
||
|
|
||
|
var _inherits = function (subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
|
||
|
|
||
|
var React = require('react');
|
||
|
var ContextWrapper = require('./ContextWrapper');
|
||
|
var assign = require('react/lib/Object.assign');
|
||
|
var PropTypes = require('../PropTypes');
|
||
|
|
||
|
var REF_NAME = '__routeHandler__';
|
||
|
|
||
|
/**
|
||
|
* A <RouteHandler> component renders the active child route handler
|
||
|
* when routes are nested.
|
||
|
*/
|
||
|
|
||
|
var RouteHandler = (function (_React$Component) {
|
||
|
function RouteHandler() {
|
||
|
_classCallCheck(this, RouteHandler);
|
||
|
|
||
|
if (_React$Component != null) {
|
||
|
_React$Component.apply(this, arguments);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_inherits(RouteHandler, _React$Component);
|
||
|
|
||
|
_createClass(RouteHandler, [{
|
||
|
key: 'getChildContext',
|
||
|
value: function getChildContext() {
|
||
|
return {
|
||
|
routeDepth: this.context.routeDepth + 1
|
||
|
};
|
||
|
}
|
||
|
}, {
|
||
|
key: 'componentDidMount',
|
||
|
value: function componentDidMount() {
|
||
|
this._updateRouteComponent(this.refs[REF_NAME]);
|
||
|
}
|
||
|
}, {
|
||
|
key: 'componentDidUpdate',
|
||
|
value: function componentDidUpdate() {
|
||
|
this._updateRouteComponent(this.refs[REF_NAME]);
|
||
|
}
|
||
|
}, {
|
||
|
key: 'componentWillUnmount',
|
||
|
value: function componentWillUnmount() {
|
||
|
this._updateRouteComponent(null);
|
||
|
}
|
||
|
}, {
|
||
|
key: '_updateRouteComponent',
|
||
|
value: function _updateRouteComponent(component) {
|
||
|
this.context.router.setRouteComponentAtDepth(this.getRouteDepth(), component);
|
||
|
}
|
||
|
}, {
|
||
|
key: 'getRouteDepth',
|
||
|
value: function getRouteDepth() {
|
||
|
return this.context.routeDepth;
|
||
|
}
|
||
|
}, {
|
||
|
key: 'createChildRouteHandler',
|
||
|
value: function createChildRouteHandler(props) {
|
||
|
var route = this.context.router.getRouteAtDepth(this.getRouteDepth());
|
||
|
|
||
|
if (route == null) {
|
||
|
return null;
|
||
|
}var childProps = assign({}, props || this.props, {
|
||
|
ref: REF_NAME,
|
||
|
params: this.context.router.getCurrentParams(),
|
||
|
query: this.context.router.getCurrentQuery()
|
||
|
});
|
||
|
|
||
|
return React.createElement(route.handler, childProps);
|
||
|
}
|
||
|
}, {
|
||
|
key: 'render',
|
||
|
value: function render() {
|
||
|
var handler = this.createChildRouteHandler();
|
||
|
// <script/> for things like <CSSTransitionGroup/> that don't like null
|
||
|
return handler ? React.createElement(
|
||
|
ContextWrapper,
|
||
|
null,
|
||
|
handler
|
||
|
) : React.createElement('script', null);
|
||
|
}
|
||
|
}]);
|
||
|
|
||
|
return RouteHandler;
|
||
|
})(React.Component);
|
||
|
|
||
|
// TODO: Include these in the above class definition
|
||
|
// once we can use ES7 property initializers.
|
||
|
// https://github.com/babel/babel/issues/619
|
||
|
|
||
|
RouteHandler.contextTypes = {
|
||
|
routeDepth: PropTypes.number.isRequired,
|
||
|
router: PropTypes.router.isRequired
|
||
|
};
|
||
|
|
||
|
RouteHandler.childContextTypes = {
|
||
|
routeDepth: PropTypes.number.isRequired
|
||
|
};
|
||
|
|
||
|
module.exports = RouteHandler;
|