catreact
Version:
Catavolt Core React Components
82 lines (81 loc) • 2.75 kB
JavaScript
/**
* Created by rburson on 12/23/15.
*/
"use strict";
var React = require('react');
var catreact_core_1 = require('./catreact-core');
var catavolt_sdk_1 = require('catavolt-sdk');
/**
* A component analogous to Catavolt AppWinDef
*/
exports.CvAppWindow = React.createClass({
propTypes: {
/**
* loginResult?:CvLoginResult;
*/
loginResult: React.PropTypes.shape({ appWinDef: React.PropTypes.instanceOf(catavolt_sdk_1.AppWinDef) }),
windowId: React.PropTypes.string,
catavolt: React.PropTypes.instanceOf(catavolt_sdk_1.AppContext),
eventRegistry: React.PropTypes.instanceOf(catreact_core_1.CvEventRegistry),
/**
* renderer signature: (cvContext:CvContext, callbackObj?:any)=>{}
*/
renderer: React.PropTypes.func
},
mixins: [catreact_core_1.CvBaseMixin],
componentDidMount: function () {
if (this.props.loginResult || this.props.windowId) {
this.setAppWinDef(this.props.loginResult, this.props.windowId);
}
else {
this.eventRegistry().subscribe(this._loginListener, catreact_core_1.CvEventType.LOGIN);
}
},
componentWillUnmount: function () {
this.eventRegistry().unsubscribe(this._loginListener);
},
componentWillReceiveProps: function (nextProps) {
if (nextProps.loginResult || nextProps.windowId) {
this.setAppWinDef(nextProps.loginResult, nextProps.windowId);
}
},
setAppWinDef: function (loginResult, windowId) {
if (loginResult) {
this.setState({ appWinDef: loginResult.appWinDef });
}
else if (windowId) {
var event_1 = this.eventRegistry().getEventByKey(this.props.windowId);
this.setState({ appWinDef: event_1.eventObj.appWinDef });
}
},
getChildContext: function () {
var ctx = this.getDefaultChildContext();
ctx.cvContext.scopeCtx.scopeObj = this.state.appWinDef;
return ctx;
},
getDefaultProps: function () {
return { loginResult: null, windowId: null };
},
getInitialState: function () {
return { appWinDef: null };
},
render: function () {
if (this.state.appWinDef) {
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;
}
},
_loginListener: function (loginEvent) {
this.setAppWinDef(loginEvent.eventObj);
}
});