UNPKG

catreact

Version:

Catavolt Core React Components

345 lines (344 loc) 12.9 kB
/** * Created by rburson on 1/6/16. */ "use strict"; var React = require('react'); var catavolt_sdk_1 = require('catavolt-sdk'); /** Base Mixin for all catavolt components */ exports.CvBaseMixin = { contextTypes: { cvContext: React.PropTypes.object }, childContextTypes: { cvContext: React.PropTypes.object }, catavolt: function () { return this.props.catavolt || (this.context.cvContext && this.context.cvContext.catavolt); }, eventRegistry: function () { return this.props.eventRegistry || (this.context.cvContext && this.context.cvContext.eventRegistry); }, findFirstDescendant: function (elem, filter) { var result = null; if (elem.props && elem.props.children) { var elems = React.Children.toArray(elem.props.children); for (var i = 0; i < elems.length; i++) { var child = elems[i]; console.log(child); if (filter(child)) { result = child; } else if (child.props.children) { result = this.findFirstDescendant(child, filter); } } } return result ? result : null; }, findAllDescendants: function (elem, filter, results) { if (results === void 0) { results = []; } if (elem.props && elem.props.children) { var elems = React.Children.toArray(elem.props.children); for (var i = 0; i < elems.length; i++) { var child = elems[i]; console.log(child); if (filter(child)) { results.push(child); } if (child.props && child.props.children) { this.findAllDescendants(child, filter, results); } } } return results; }, findFirstScopeCtx: function (matchFn, startingCtx) { var scopeCtx = startingCtx || this.scopeCtx(); var result = null; while (scopeCtx && !result) { if (matchFn(scopeCtx)) { result = scopeCtx; } else { scopeCtx = scopeCtx.parentScopeCtx; } } return result; }, findEntityRec: function (startingContext) { if (this.props.entityRec) { return this.props.entityRec; } else { var e = null; var scopeCtx = this.findFirstScopeCtx(function (scopeCtx) { return scopeCtx.scopeObj instanceof catavolt_sdk_1.EntityBuffer || scopeCtx.scopeObj instanceof catavolt_sdk_1.EntityRecImpl || scopeCtx.scopeObj instanceof catavolt_sdk_1.NullEntityRec; }, startingContext); return scopeCtx ? scopeCtx.scopeObj : null; } }, findPaneContext: function (startingContext) { if (this.props.paneContext) { return this.props.paneContext; } else { var scopeCtx = this.findFirstScopeCtx(function (scopeCtx) { return scopeCtx.scopeObj instanceof catavolt_sdk_1.PaneContext; }, startingContext); return scopeCtx ? scopeCtx.scopeObj : null; } }, findPaneTitle: function (paneContext) { var title = paneContext.paneTitle; if (!title) title = paneContext.actionSource instanceof catavolt_sdk_1.WorkbenchLaunchAction ? paneContext.actionSource.name : null; if (!title) title = paneContext.paneDef.settings['dialogDescription']; if (!title || title === 'null') { return ''; } else { return title; } }, firstInScope: function (type, startingScopeCtx) { var scopeCtx = this.findFirstScopeCtx(function (scopeCtx) { return scopeCtx.scopeObj instanceof type; }, startingScopeCtx); return scopeCtx ? scopeCtx.scopeObj : null; }, getDefaultChildContext: function () { return { cvContext: { catavolt: this.catavolt(), eventRegistry: this.eventRegistry(), scopeCtx: { scopeObj: null, parentScopeCtx: this.scopeCtx() } } }; }, resourceIdForObject: function (o) { if (o instanceof catavolt_sdk_1.FormContext) { var formContext = o; return formContext.paneDef.dialogHandle.handleValue.toString(); } else if (o instanceof catavolt_sdk_1.AppWinDef) { if (this.catavolt().sessionContextTry.isSuccess) { //right now the 'windowId' is the session handle. may need to change this if want to have multiple windows... return this.catavolt().sessionContextTry.success.sessionHandle; } else { return null; } } else if (o instanceof catavolt_sdk_1.WebRedirection) { var webRedirection = o; return 'ext_' + catavolt_sdk_1.StringUtil.hashCode(webRedirection.webURL); } else { catavolt_sdk_1.Log.debug('No resourceId found for object'); return null; } }, scopeCtx: function () { return this.context.cvContext && this.context.cvContext.scopeCtx; }, shouldCacheResult: function () { //cache everything for now, can later set this preference component-by-component via a property return this.eventRegistry().isCacheEnabled(); }, }; /** * Enumeration of event types to be used with {@link CvEvent} * There is also a corresponding 'payload' object type for each of these * enum values, that is used as the {@link CvEvent.eventObj} type */ (function (CvEventType) { CvEventType[CvEventType["LOGIN"] = 0] = "LOGIN"; CvEventType[CvEventType["LOGOUT"] = 1] = "LOGOUT"; CvEventType[CvEventType["ACTION_FIRED"] = 2] = "ACTION_FIRED"; CvEventType[CvEventType["NAVIGATION"] = 3] = "NAVIGATION"; CvEventType[CvEventType["STATE_CHANGE"] = 4] = "STATE_CHANGE"; CvEventType[CvEventType["MESSAGE"] = 5] = "MESSAGE"; })(exports.CvEventType || (exports.CvEventType = {})); var CvEventType = exports.CvEventType; (function (CvNavigationResultType) { CvNavigationResultType[CvNavigationResultType["FORM"] = 0] = "FORM"; CvNavigationResultType[CvNavigationResultType["URL"] = 1] = "URL"; CvNavigationResultType[CvNavigationResultType["NULL"] = 2] = "NULL"; })(exports.CvNavigationResultType || (exports.CvNavigationResultType = {})); var CvNavigationResultType = exports.CvNavigationResultType; (function (CvMessageType) { CvMessageType[CvMessageType["ERROR"] = 0] = "ERROR"; CvMessageType[CvMessageType["WARN"] = 1] = "WARN"; CvMessageType[CvMessageType["INFO"] = 2] = "INFO"; })(exports.CvMessageType || (exports.CvMessageType = {})); var CvMessageType = exports.CvMessageType; var CvNavigationResultUtil = (function () { function CvNavigationResultUtil() { } CvNavigationResultUtil.determineType = function (navRequest) { if (navRequest instanceof catavolt_sdk_1.FormContext) { return CvNavigationResultType.FORM; } else if (navRequest instanceof catavolt_sdk_1.WebRedirection) { return CvNavigationResultType.URL; } else { return CvNavigationResultType.NULL; } }; return CvNavigationResultUtil; }()); exports.CvNavigationResultUtil = CvNavigationResultUtil; (function (CvStateChangeType) { CvStateChangeType[CvStateChangeType["DATA_CHANGE"] = 0] = "DATA_CHANGE"; CvStateChangeType[CvStateChangeType["DESTROYED"] = 1] = "DESTROYED"; })(exports.CvStateChangeType || (exports.CvStateChangeType = {})); var CvStateChangeType = exports.CvStateChangeType; (function (CvActionFiredResultType) { CvActionFiredResultType[CvActionFiredResultType["ACTION_STARTED"] = 0] = "ACTION_STARTED"; CvActionFiredResultType[CvActionFiredResultType["ACTION_COMPLETED"] = 1] = "ACTION_COMPLETED"; })(exports.CvActionFiredResultType || (exports.CvActionFiredResultType = {})); var CvActionFiredResultType = exports.CvActionFiredResultType; /** * The {@link CvEventRegistry} handles decoupled communication between components. Any component may subscribe to and publish * {@link CvEvent}s. See also {@link CvListener} and {@link CvEventType}. It also provides resource caching (i.e. caching of * some server provided objects to allow retrieval by 'id') */ var CvEventRegistry = (function () { function CvEventRegistry() { this._cache = null; this._listenerMap = []; } CvEventRegistry.prototype.clearAll = function () { this.clearCache(); this.removeAllListeners(); }; CvEventRegistry.prototype.clearCache = function () { this._cache = {}; }; CvEventRegistry.prototype.enableCache = function () { this._cache = this._cache || {}; }; CvEventRegistry.prototype.disableCache = function () { this._cache = null; }; CvEventRegistry.prototype.isCacheEnabled = function () { return this._cache !== null; }; CvEventRegistry.prototype.getEventByKey = function (key) { var result = this._cache[key]; if (!result) { catavolt_sdk_1.Log.debug('No resource entry exists for resourceId ' + key); } return this._cache[key]; }; CvEventRegistry.prototype.publish = function (event, shouldCache) { if (shouldCache === void 0) { shouldCache = false; } var listenerArray = this._listenerMap[event.type]; if (listenerArray) { listenerArray.forEach(function (listener) { console.debug('publishing ' + JSON.stringify(CvEventType[event.type]) + ' to ' + listener); listener(event); }); } if (shouldCache && event.resourceId) { this._cache[event.resourceId] = event; } }; CvEventRegistry.prototype.removeFromCache = function (key) { delete this._cache[key]; }; CvEventRegistry.prototype.subscribe = function (listener, eventType) { var listenerArray = this._listenerMap[eventType]; if (!listenerArray) { listenerArray = []; this._listenerMap[eventType] = listenerArray; } if (listenerArray.indexOf(listener) < 0) { listenerArray.push(listener); } }; CvEventRegistry.prototype.removeAllListeners = function () { this._listenerMap = []; }; CvEventRegistry.prototype.unsubscribe = function (listener) { for (var eventType in this._listenerMap) { var listenerArray = this._listenerMap[eventType]; if (listenerArray) { var index = listenerArray.indexOf(listener); if (index > -1) { listenerArray.splice(index, 1); } } } }; return CvEventRegistry; }()); exports.CvEventRegistry = CvEventRegistry; var CvValueAdapter = (function () { function CvValueAdapter() { this._subscriberArray = []; } Object.defineProperty(CvValueAdapter.prototype, "value", { get: function () { return this._value; }, set: function (value) { this._value = value; }, enumerable: true, configurable: true }); CvValueAdapter.prototype.createValueListener = function () { var _this = this; return (function (value) { _this.value = value; _this._subscriberArray.forEach(function (updateListener) { updateListener(value); }); }); }; CvValueAdapter.prototype.subscribe = function (updateListener) { if (this._subscriberArray.indexOf(updateListener) < 0) { this._subscriberArray.push(updateListener); } }; return CvValueAdapter; }()); exports.CvValueAdapter = CvValueAdapter; /** * Localization */ var CvLocale = (function () { function CvLocale(currencySymbol, percentageSymbol) { this.currencySymbol = currencySymbol; this.percentageSymbol = percentageSymbol; } CvLocale.prototype.getAssociatedSymbol = function (value, propDef) { if (propDef) { if (propDef.isMoneyType) { return this.currencySymbol + value; } else if (propDef.isPercentType) { return value + this.percentageSymbol; } else { return value; } } else { return value; } }; return CvLocale; }()); exports.CvLocale = CvLocale; exports.DefaultLocale = new CvLocale('$', '%');