catreact
Version:
Catavolt Core React Components
132 lines (131 loc) • 6.39 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');
/*
***************************************************
* Render a FormContext
***************************************************
*/
exports.CvForm = React.createClass({
mixins: [catreact_core_1.CvBaseMixin],
componentDidMount: function () {
},
componentWillUnmount: function () {
var formContext = this.formContext();
if (formContext && formContext.isDestroyed) {
this.eventRegistry().removeFromCache(this.resourceIdForObject(formContext));
}
},
getChildContext: function () {
var ctx = this.getDefaultChildContext();
ctx.cvContext.scopeCtx.scopeObj = this.formContext();
return ctx;
},
getDefaultProps: function () {
return {
formContext: null,
formRenderer: null,
childFormComponentRenderer: null,
listComponentRenderer: null,
detailsComponentRenderer: null,
mapComponentRenderer: null,
graphComponentRenderer: null,
stateChangeListeners: []
};
},
render: function () {
var _this = this;
var formContext = this.formContext();
if (formContext && !formContext.isDestroyed) {
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 {
var childComponents = formContext.childrenContexts.map(function (context, i) {
var childComponent = null;
if (context instanceof catavolt_sdk_1.FormContext) {
childComponent = _this.props.childFormComponentRenderer ?
_this.props.childFormComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.ListContext) {
childComponent = _this.props.listComponentRenderer ?
_this.props.listComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.DetailsContext) {
childComponent = _this.props.detailsComponentRenderer ?
_this.props.detailsComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.MapContext) {
childComponent = _this.props.mapComponentRenderer ?
_this.props.mapComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.GraphContext) {
childComponent = _this.props.graphComponentRenderer ?
_this.props.graphComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.ImagePickerContext) {
childComponent = _this.props.imagePickerComponentRenderer ?
_this.props.imagePickerComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.CalendarContext) {
childComponent = _this.props.calendarComponentRenderer ?
_this.props.calendarComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.BarcodeScanContext) {
childComponent = _this.props.barcodeScanComponentRenderer ?
_this.props.barcodeScanComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.GeoFixContext) {
childComponent = _this.props.geoFixComponentRenderer ?
_this.props.geoFixComponentRenderer(context, _this._stateChangeListener) : null;
}
else if (context instanceof catavolt_sdk_1.GeoLocationContext) {
childComponent = _this.props.geoLocationComponentRenderer ?
_this.props.geoLocationComponentRenderer(context, _this._stateChangeListener) : null;
}
else {
catavolt_sdk_1.Log.info('Not yet handling display for ' + context.constructor['name']);
childComponent = React.createElement("div", null, 'Not yet handling display for ' + context.constructor['name']);
}
return childComponent ? React.cloneElement(childComponent, { key: i }) : null;
});
return this.props.formRenderer ? this.props.formRenderer(this.getChildContext().cvContext, childComponents) : null;
}
}
else {
return null;
}
},
refresh: function () {
this.forceUpdate();
},
formContext: function (nextProps) {
return (nextProps && nextProps.formContext) || this.props.formContext || this.firstInScope(catavolt_sdk_1.FormContext);
},
_checkDestroyed: function () {
var formContext = this.formContext();
if (formContext && formContext.isDestroyed) {
var event_1 = {
type: catreact_core_1.CvEventType.STATE_CHANGE,
resourceId: this.resourceIdForObject(formContext),
eventObj: { source: formContext, type: catreact_core_1.CvStateChangeType.DESTROYED }
};
this.props.stateChangeListeners.forEach(function (listener) {
listener(event_1);
});
}
},
_stateChangeListener: function (event) {
if (event.eventObj.type === catreact_core_1.CvStateChangeType.DESTROYED) {
this._checkDestroyed();
this.refresh();
}
}
});