UNPKG

catreact

Version:

Catavolt Core React Components

247 lines (246 loc) 11.1 kB
/** * Created by rburson on 2/10/16. */ "use strict"; var React = require('react'); var catreact_core_1 = require('./catreact-core'); var catavolt_sdk_1 = require('catavolt-sdk'); exports.CvActionBase = { /* Find a PaneContext in the hierarchy that has a MenuDef with this actionId */ findPaneContextWithActionId: function (actionId) { var paneContext = null; if (this.props.paneContext) { var menuDef = this.props.paneContext.findMenuDefAt(actionId); if (menuDef) { paneContext = this.props.paneContext; } } else { var scopeCtx = this.findFirstScopeCtx(function (scopeCtx) { if (scopeCtx.scopeObj instanceof catavolt_sdk_1.PaneContext) { return scopeCtx.scopeObj.findMenuDefAt(actionId) != null; } else { return false; } }); if (scopeCtx) { paneContext = scopeCtx.scopeObj; } } return paneContext; }, getSelectedTargets: function () { return (this.props.selectionProvider && this.props.selectionProvider.value) || []; }, paneContext: function () { var paneContext = null; if (this.props.paneContext) { paneContext = this.props.paneContext; } else if (this.props.actionId) { paneContext = this.findPaneContextWithActionId(this.props.actionId); } return paneContext ? paneContext : this.findPaneContext(); }, menuDef: function () { if (this.props.menuDef) { return this.props.menuDef; } else { var paneContext = this.paneContext(); return paneContext.findMenuDefAt(this.props.actionId); } }, performAction: function () { var _this = this; var paneContext = this.paneContext(); if (paneContext) { var menuDef_1 = this.menuDef(); if (menuDef_1) { var result = null; /* * Only navigate if the action is a not a 'client-side' action. * If it is, the 'action' will still be published so that the client can handle it appropriately. */ var actionMeta_1 = this._getActionMeta(menuDef_1.actionId); if (!actionMeta_1.isClientAction) { if (paneContext instanceof catavolt_sdk_1.QueryContext) { result = paneContext.performMenuAction(menuDef_1, this.getSelectedTargets()); } else if (paneContext instanceof catavolt_sdk_1.DetailsContext) { var detailsContext = paneContext; var entityRec = detailsContext.entityRec; /* @TODO maybe we want to save changes here, maybe not... they are currently discarded */ var pendingWrites = catavolt_sdk_1.EntityRecUtil.newEntityRec(entityRec.objectId, []); result = detailsContext.performMenuAction(menuDef_1, pendingWrites); } else if (paneContext instanceof catavolt_sdk_1.FormContext) { result = paneContext.performMenuAction(menuDef_1); } } if (actionMeta_1.isClientAction) { /* let the designated handler perform the action */ if (this.props.actionHandler) { /* Publish the action fired */ var event_1 = this._publishActionStarted(menuDef_1.actionId, paneContext, actionMeta_1.isClientAction, this.props.actionListeners, this.eventRegistry()); this.props.actionHandler({ event: event_1, callback: function (success, error) { if (error) { var de = new catavolt_sdk_1.DialogException(); de.message = '' + error; var event_2 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: '' + error, messageObj: de, type: catreact_core_1.CvMessageType.ERROR } }; _this.eventRegistry().publish(event_2, false); } /* Publish the action finished */ _this._publishActionFinished(menuDef_1.actionId, paneContext, _this.props.actionListeners, _this.eventRegistry()); } }); } else { /* If there's no handler for a client action, publish both start and finish events */ this._publishActionStarted(menuDef_1.actionId, paneContext, actionMeta_1.isClientAction, this.props.actionListeners, this.eventRegistry()); this._publishActionFinished(menuDef_1.actionId, paneContext, this.props.actionListeners, this.eventRegistry()); } } else if (result) { /* Publish the action fired */ var event_3 = this._publishActionStarted(menuDef_1.actionId, paneContext, actionMeta_1.isClientAction, this.props.actionListeners, this.eventRegistry()); /* Publish the navigation */ result.onComplete(function (navRequestTry) { /* Publish the action finished */ _this._publishActionFinished(menuDef_1.actionId, paneContext, _this.props.actionListeners, _this.eventRegistry()); if (navRequestTry.isSuccess) { var resourceId = _this.resourceIdForObject(navRequestTry.success); var e_1 = { type: catreact_core_1.CvEventType.NAVIGATION, resourceId: resourceId, eventObj: { navRequest: navRequestTry.success, actionId: _this.props.actionId, navTarget: _this.props.navTarget, noTransition: actionMeta_1.noTransition, sourceIsDestroyed: paneContext.isDestroyed, type: catreact_core_1.CvNavigationResultUtil.determineType(navRequestTry.success) } }; _this.eventRegistry().publish(e_1, _this.shouldCacheResult()); _this.props.navigationListeners.forEach(function (listener) { listener(e_1); }); _this._checkDestroyed(paneContext); } else { var event_4 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: 'Navigation failed', messageObj: navRequestTry.failure, type: catreact_core_1.CvMessageType.ERROR } }; _this.eventRegistry().publish(event_4, false); } }); } } } else { catavolt_sdk_1.Log.info("Failed to find menuDef for action: " + this.props.actionId); } }, _checkDestroyed: function (paneContext) { if (paneContext && paneContext.isDestroyed) { var event_5 = { type: catreact_core_1.CvEventType.STATE_CHANGE, resourceId: null, eventObj: { source: paneContext, type: catreact_core_1.CvStateChangeType.DESTROYED } }; this.props.stateChangeListeners.forEach(function (listener) { listener(event_5); }); } }, _getCallbackObj: function () { var _this = this; return { fireAction: function () { _this.performAction(); } }; }, /* static */ _getActionMeta: function (actionId) { //treat search like a server action if (actionId === "#search") { return { isClientAction: false, noTransition: true }; } else { return { isClientAction: (actionId.substr(0, 1) === "#"), noTransition: false }; } }, /* static */ _publishActionStarted: function (actionId, source, clientAction, actionListeners, eventRegistry) { var e = { type: catreact_core_1.CvEventType.ACTION_FIRED, eventObj: { actionId: actionId, type: catreact_core_1.CvActionFiredResultType.ACTION_STARTED, source: source, clientAction: clientAction } }; actionListeners.forEach(function (listener) { listener(e); }); eventRegistry.publish(e, false); return e; }, /* static */ _publishActionFinished: function (actionId, source, actionListeners, eventRegistry) { var e = { type: catreact_core_1.CvEventType.ACTION_FIRED, eventObj: { actionId: actionId, type: catreact_core_1.CvActionFiredResultType.ACTION_COMPLETED, source: source, clientAction: false } }; actionListeners.forEach(function (listener) { listener(e); }); eventRegistry.publish(e, false); return e; } }; /* *************************************************** * A catavolt action *************************************************** */ exports.CvAction = React.createClass({ mixins: [catreact_core_1.CvBaseMixin, exports.CvActionBase], getChildContext: function () { var ctx = this.getDefaultChildContext(); ctx.cvContext.scopeCtx.scopeObj = this.menuDef(); return ctx; }, getDefaultProps: function () { return { actionId: null, paneContext: null, navTarget: null, menuDef: null, actionListeners: [], navigationListeners: [], stateChangeListeners: [], selectionProvider: null, renderer: null, wrapperElemName: 'span', wrapperElemProps: {}, actionHandlerProvider: null }; }, render: function () { if (this.props.renderer) { return this.props.renderer(this.getChildContext().cvContext, this._getCallbackObj()); } else { var props = catavolt_sdk_1.ObjUtil.addAllProps(this.props.wrapperElemProps, {}); props['onClick'] = this.performAction; return React.createElement(this.props.wrapperElemName, props, this.props.children); } } });