graft-react
Version:
react admin and helper components for graft-db
79 lines (78 loc) • 3.09 kB
JavaScript
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var React = require("react");
var Nav_1 = require("./Nav");
var debounce = require("lodash.debounce");
exports.navigatorContextTypes = {
navigator: React.PropTypes.object,
};
function Router(routes, options) {
var opts = options || {};
return _a = (function (_super) {
__extends(App, _super);
function App() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var _this = _super.apply(this, args) || this;
_this.onNavigate = function (route, params) {
_this.setState({ route: route, params: params }, _this.afterNavigate);
};
_this.afterNavigate = debounce(function () {
if (!opts.afterNavigate) {
return;
}
setTimeout(function () { return opts.afterNavigate && opts.afterNavigate(_this); }, 10);
}, 20);
_this.nav = _this.createNav(_this.props);
_this.state = { route: null, params: {} };
_this.started = false;
return _this;
}
App.prototype.componentWillReceiveProps = function (nextProps) {
if (this.props.url !== nextProps.url) {
this.nav.start();
}
if (this.props.history !== nextProps.history) {
this.nav = this.createNav(nextProps);
}
};
App.prototype.componentDidMount = function () {
if (!this.started) {
this.nav.start();
this.started = true;
}
};
App.prototype.createNav = function (props) {
var history = props.history;
return new Nav_1.Nav({
routes: routes,
history: history || opts.defaultHistory,
onNavigate: this.onNavigate,
encodeParam: opts.encodeParam,
});
};
App.prototype.getChildContext = function () {
return {
navigator: this.nav,
};
};
App.prototype.render = function () {
var _a = this.state, route = _a.route, params = _a.params;
if (!route) {
return React.createElement("div", null, "NO_CONTENT");
}
return route.render.apply(this, [params, this.nav]);
};
return App;
}(React.Component)),
_a.childContextTypes = exports.navigatorContextTypes,
_a;
var _a;
}
exports.Router = Router;