catreact
Version:
Catavolt Core React Components
195 lines (194 loc) • 7.76 kB
JavaScript
"use strict";
/**
* 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) {
//Log.debug("receive props CvProps")
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,
handler: null,
isVisible: null,
entityRec: null,
wrapperElemName: 'span',
wrapperElemProps: null,
overrideValue: null,
currencySymbol: '$',
percentageSymbol: '%',
boolTrueClassName: 'cv-icon cv-bool-true',
boolFalseClassName: 'cv-icon cv-bool-false',
imageClassName: null,
imageStyle: null,
style: null,
className: null,
renderer: null,
};
},
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) {
return this.entityRec(nextProps, nextContext).propAtName(this.props.propName);
},
propDef: function (nextProps, nextContext) {
return this.entityRecDef(nextProps, nextContext).propDefAtName(this.props.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 {
if (this.props.overrideValue) {
var props = this.props.wrapperElemProps || {
style: this.props.style,
className: this.props.className
};
return React.createElement(this.props.wrapperElemName, props, this.props.overrideValue);
}
else if (prop.value === null || prop.value === undefined) {
var props = this.props.wrapperElemProps || {
style: this.props.style,
className: this.props.className
};
return this.props.defaultValue !== null ?
React.createElement(this.props.wrapperElemName, props, this.props.defaultValue) : null;
}
else {
var propDef = this.propDef();
if (propDef && propDef.isBinaryType) {
return this.state.binary ? React.createElement("img", {style: this.props.imageStyle, src: this.state.binary.toUrl(), className: this.props.imageClassName}) : null;
}
else if (propDef && propDef.isBooleanType) {
return prop.value ? React.createElement("span", {className: this.props.boolTrueClassName}) : React.createElement("span", {className: this.props.boolFalseClassName});
}
else {
var props = this.props.wrapperElemProps || {
style: this.props.style,
className: this.props.className
};
return React.createElement(this.props.wrapperElemName, props, catreact_core_1.DefaultLocale.getAssociatedSymbol(exports.CvProp.formatDataType(prop, propDef), propDef));
}
}
}
}
},
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 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;
});
}
}
},
statics: {
formatDataType: function (prop, propDef) {
if (propDef) {
if (propDef.isDateTimeType) {
return prop.value.toString();
}
else if (propDef.isDateType) {
return prop.value.toLocaleDateString();
}
else if (propDef.isTimeType) {
var timeValue = prop.value;
return timeValue.toString();
}
else if (propDef.isCodeRefType) {
return prop.value.description;
}
else if (propDef.isObjRefType) {
return prop.value.description;
}
else if (propDef.isPasswordType) {
return prop.value.replace(/./g, "*");
}
else {
return catavolt_sdk_1.PropFormatter.formatForRead(prop.value, propDef);
}
}
else {
return String(prop.value);
}
}
}
});