catreact
Version:
Catavolt Core React Components
78 lines (77 loc) • 2.85 kB
JavaScript
/**
* Created by rburson on 12/23/15.
*/
"use strict";
var React = require('react');
var catreact_core_1 = require('./catreact-core');
/*
***************************************************
* Render a NavRequest
***************************************************
*/
exports.CvNavigation = React.createClass({
mixins: [catreact_core_1.CvBaseMixin],
componentDidMount: function () {
if (this.props.navigationResult) {
this.setState({ navRequest: this.props.navigationResult.navRequest, visible: true });
}
else if (this.props.navigationId) {
var event_1 = this.eventRegistry().getEventByKey(this.props.navigationId);
this.setState({ navRequest: event_1.eventObj.navRequest, visible: true });
}
this.eventRegistry().subscribe(this._navListener, catreact_core_1.CvEventType.NAVIGATION);
},
componentWillUnmount: function () {
this.eventRegistry().unsubscribe(this._navListener);
},
componentWillReceiveProps: function (nextProps) {
if (nextProps.navigationResult) {
this.setState({ navRequest: nextProps.navigationResult.navRequest, visible: true });
}
else if (nextProps.navigationId) {
var event_2 = this.eventRegistry().getEventByKey(nextProps.navigationId);
this.setState({ navRequest: event_2.eventObj.navRequest, visible: true });
}
},
getChildContext: function () {
var ctx = this.getDefaultChildContext();
ctx.cvContext.scopeCtx.scopeObj = this.state.navRequest;
return ctx;
},
getDefaultProps: function () {
return { persistent: false, targetId: null, navigationResult: null, navigationId: null };
},
getInitialState: function () {
return { visible: false, navRequest: null };
},
render: function () {
if (this.state.visible && this.state.navRequest) {
if (this.props.renderer) {
return this.props.renderer(this.getChildContext().cvContext);
}
else if (React.Children.count(this.props.children) > 0) {
return this.props.children;
}
else {
return null;
}
}
else {
return null;
}
},
_navListener: function (navEvent) {
var navRequest = navEvent.eventObj.navRequest;
if (navRequest && this.isMounted()) {
if (navEvent.eventObj.navTarget) {
if (this.props.targetId === navEvent.eventObj.navTarget) {
this.setState({ navRequest: navRequest, visible: true });
}
else {
if (!this.props.persistent)
this.setState({ visible: false });
}
}
}
},
});