UNPKG

catreact

Version:

Catavolt Core React Components

194 lines (193 loc) 8.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Created by rburson on 1/14/16. */ var React = require("react"); var catreact_core_1 = require("./catreact-core"); var catavolt_sdk_1 = require("catavolt-sdk"); /* *************************************************** * Render a Property *************************************************** */ exports.CvProp = React.createClass({ mixins: [catreact_core_1.CvBaseMixin], componentWillUpdate: function (nextProps, nextState, nextContext) { //Log.debug("updating CvProps") }, componentDidMount: function () { //Log.debug("mounting CvProps"); this.refresh(); }, componentWillReceiveProps: function (nextProps, nextContext) { //reset the loaded state if this is a 'new' prop if (this.props.propName !== nextProps.propName || this.entityRec() !== this.entityRec(nextProps, nextContext)) { this.loaded = false; } this.refresh(nextProps, nextContext); }, entityRec: function (nextProps, nextContext) { if (nextProps && nextProps.entityRec) { return nextProps.entityRec; } else if (nextContext) { return this.findEntityRec(nextContext.cvContext.scopeCtx); } else { return this.findEntityRec(); } }, entityRecDef: function (nextProps, nextContext) { return this.paneContext(nextProps, nextContext).entityRecDef; }, getChildContext: function () { var ctx = this.getDefaultChildContext(); ctx.cvContext.scopeCtx.scopeObj = this.prop(); return ctx; }, getDefaultProps: function () { return { propName: null, defaultValue: null, booleanRenderer: null, binaryRenderer: null, handler: null, isVisible: null, entityRec: null, wrapperElemName: 'span', wrapperElemProps: null, overrideValue: null, style: null, className: null, renderer: null, dataPropNames: [] }; }, getInitialState: function () { return { binary: null }; }, paneContext: function (nextProps, nextContext) { if (nextProps && nextProps.paneContext) { return nextProps.paneContext; } else if (nextContext) { return this.findPaneContext(nextContext.cvContext.scopeCtx); } else { return this.findPaneContext(); } }, prop: function (nextProps, nextContext) { var propName = nextProps ? nextProps.propName : this.props.propName; return this.entityRec(nextProps, nextContext).propAtName(propName); }, propDef: function (nextProps, nextContext) { var propName = nextProps ? nextProps.propName : this.props.propName; return this.entityRecDef(nextProps, nextContext).propDefAtName(propName); }, render: function () { var prop = this.prop(); if (!prop || (this.props.isVisible && !this.props.isVisible(prop))) { return null; } if (this.props.renderer) { return this.props.renderer(this.getChildContext().cvContext); } else if (this.props.handler) { return this.props.handler(prop); } else { if (React.Children.count(this.props.children) > 0) { return this.props.children; } else { /* setup props for the wrapper element */ var propDef = this.propDef(); var nullRenderer = propDef && propDef.isBinaryType; var props_1 = { style: this.props.style, className: this.props.className }; if (this.props.wrapperElemProps) { props_1 = catavolt_sdk_1.ObjUtil.addAllProps(this.props.wrapperElemProps, props_1); } /* overridden value */ if (this.props.overrideValue != null) { return React.createElement(this.props.wrapperElemName, props_1, this.props.overrideValue); /* missing value */ } else if (!nullRenderer && (prop.value === null || prop.value === undefined)) { return this.props.defaultValue !== null ? React.createElement(this.props.wrapperElemName, props_1, this.props.defaultValue) : null; } else { var propDef_1 = this.propDef(); /* binary property */ if (propDef_1 && propDef_1.isBinaryType) { if (this.state.binary) { var dataUrl = this.state.binary.toUrl(); } return this.props.binaryRenderer ? this.props.binaryRenderer(dataUrl) : React.createElement(this.props.wrapperElemName, props_1, '[binary]'); /* boolean property */ } else if (propDef_1 && propDef_1.isBooleanType) { return this.props.booleanRenderer ? this.props.booleanRenderer(prop.value) : React.createElement(this.props.wrapperElemName, props_1, String(prop.value)); } else { /* text property */ var editorContext = this.paneContext(null, null); var value_1 = (editorContext && editorContext.isWriteMode) ? catavolt_sdk_1.PropFormatter.formatForWrite(prop, propDef_1) : catavolt_sdk_1.PropFormatter.formatForRead(prop, propDef_1); /* copy the props rendered value to any attributes that were specified */ if (this.props.dataPropNames && this.props.dataPropNames.length > 0) { this.props.dataPropNames.forEach(function (name) { return props_1[name] = value_1; }); } return React.createElement(this.props.wrapperElemName, props_1, value_1); } } } } }, refresh: function (nextProps, nextContext) { var _this = this; var prop = this.prop(nextProps, nextContext); if (prop) { var propDef = this.propDef(nextProps, nextContext); if (propDef && propDef.isBinaryType) { if (prop.value) { //if this is an update and the propName hasn't changed, don't reload the binary if (this.loaded && nextProps && nextProps.propName === this.props.propName) { return; } var paneContext = this.paneContext(nextProps, nextContext); paneContext.binaryAt(this.props.propName, this.entityRec(nextProps, nextContext)).onComplete(function (binaryTry) { if (binaryTry.isSuccess) { if (_this.isMounted()) { _this.setState({ binary: binaryTry.success }); } } else { var event_1 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: 'Could not load binary property: ' + prop.name, messageObj: binaryTry.failure, type: catreact_core_1.CvMessageType.ERROR } }; _this.eventRegistry().publish(event_1, false); } _this.loaded = true; }); } else { // If there is no value, there is no data to read if (this.isMounted()) { this.setState({ binary: null }); } } } } }, });