catreact
Version:
Catavolt Core React Components
155 lines (154 loc) • 7.01 kB
JavaScript
"use strict";
/**
* Created by rburson on 12/23/15.
*/
Object.defineProperty(exports, "__esModule", { value: true });
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(catreact_core_1.CvResourceManager.resourceIdForObject(formContext, this.catavolt()));
}
},
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,
printMarkupComponentRenderer: 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 = _this._getChildComponent(context, i);
if (childComponent && childComponent.key != null) {
return childComponent;
}
else {
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: catreact_core_1.CvResourceManager.resourceIdForObject(formContext, this.catavolt()),
eventObj: { source: formContext, type: catreact_core_1.CvStateChangeType.DESTROYED }
};
this.props.stateChangeListeners.forEach(function (listener) {
listener(event_1);
});
}
},
_getChildComponent: function (context, index) {
var childComponent = null;
if (context.hasError) {
catavolt_sdk_1.Log.error("Failed to load pane with: " + JSON.stringify(context.error));
}
else if (context instanceof catavolt_sdk_1.FormContext) {
childComponent = this.props.childFormComponentRenderer ?
this.props.childFormComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.ListContext) {
childComponent = this.props.listComponentRenderer ?
this.props.listComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.DetailsContext) {
childComponent = this.props.detailsComponentRenderer ?
this.props.detailsComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.MapContext) {
childComponent = this.props.mapComponentRenderer ?
this.props.mapComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.PrintMarkupContext) {
childComponent = this.props.printMarkupComponentRenderer ?
this.props.printMarkupComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.GraphContext) {
childComponent = this.props.graphComponentRenderer ?
this.props.graphComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.ImagePickerContext) {
childComponent = this.props.imagePickerComponentRenderer ?
this.props.imagePickerComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.CalendarContext) {
childComponent = this.props.calendarComponentRenderer ?
this.props.calendarComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.BarcodeScanContext) {
childComponent = this.props.barcodeScanComponentRenderer ?
this.props.barcodeScanComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.GeoFixContext) {
childComponent = this.props.geoFixComponentRenderer ?
this.props.geoFixComponentRenderer(context, this._stateChangeListener, index) : null;
}
else if (context instanceof catavolt_sdk_1.GeoLocationContext) {
childComponent = this.props.geoLocationComponentRenderer ?
this.props.geoLocationComponentRenderer(context, this._stateChangeListener, index) : 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;
},
_stateChangeListener: function (event) {
if (event.eventObj.type === catreact_core_1.CvStateChangeType.DESTROYED) {
this._checkDestroyed();
this.refresh();
}
else {
this.props.stateChangeListeners.forEach(function (listener) {
listener(event);
});
}
},
});