UNPKG

catreact

Version:

Catavolt Core React Components

147 lines (146 loc) 5.44 kB
/** * Created by rburson on 12/23/15. */ "use strict"; var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; var React = require('react'); var catreact_core_1 = require('./catreact-core'); var catavolt_sdk_1 = require('catavolt-sdk'); exports.CvLauncher = React.createClass({ mixins: [catreact_core_1.CvBaseMixin], getChildContext: function () { var ctx = this.getDefaultChildContext(); ctx.cvContext.scopeCtx.scopeObj = this.launchAction(); return ctx; }, getDefaultProps: function () { return { navTarget: null, actionId: null, launchAction: null, workbench: null, renderer: null, launchListeners: [], actionListeners: [], wrapperElemName: 'span', wrapperElemProps: {} }; }, launchAction: function () { var _this = this; if (this.props.launchAction) { return this.props.launchAction; } else { var workbench = this.workbench(); var workbenchLaunchAction_1 = null; workbench.workbenchLaunchActions.some(function (launchAction) { if (launchAction.actionId == _this.props.actionId) { workbenchLaunchAction_1 = launchAction; return true; } else { return false; } }); return workbenchLaunchAction_1; } }, render: function () { var _this = this; var launchAction = this.launchAction(); if (this.props.renderer) { if (launchAction) { return this.props.renderer(this.getChildContext().cvContext, this._getCallbackObject()); } else { var newChildren_1 = []; this.workbench().workbenchLaunchActions.forEach(function (launchAction) { newChildren_1.push(React.createElement(exports.CvLauncher, __assign({}, _this.props, {actionId: launchAction.actionId, key: launchAction.actionId}))); }); return React.createElement(this.props.wrapperElemName, this.props.wrapperElemProps, newChildren_1); } } else { return null; } }, performAction: function () { var _this = this; var launchAction = this.launchAction(); this._publishActionStarted(launchAction.actionId); this.catavolt().performLaunchAction(launchAction).onComplete(function (launchTry) { _this._publishActionFinished(launchAction.actionId); if (launchTry.isSuccess) { var resourceId = _this.resourceIdForObject(launchTry.success); var event_1 = { type: catreact_core_1.CvEventType.NAVIGATION, resourceId: resourceId, eventObj: { navRequest: launchTry.success, workbenchId: launchAction.workbenchId, navTarget: _this.props.navTarget, type: catreact_core_1.CvNavigationResultUtil.determineType(launchTry.success) } }; _this.eventRegistry().publish(event_1, _this.shouldCacheResult()); _this.props.launchListeners.forEach(function (listener) { listener(event_1); }); } else { var event_2 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: 'Launch of ' + _this.launchAction().name + ' failed', messageObj: launchTry.failure, type: catreact_core_1.CvMessageType.ERROR } }; _this.eventRegistry().publish(event_2, false); } }); }, workbench: function () { return this.props.workbench || this.firstInScope(catavolt_sdk_1.Workbench); }, _getCallbackObject: function () { var _this = this; return { fireLaunchAction: function () { _this.performAction(); } }; }, _publishActionStarted: function (actionId) { var e = { type: catreact_core_1.CvEventType.ACTION_FIRED, eventObj: { actionId: actionId, type: catreact_core_1.CvActionFiredResultType.ACTION_STARTED, source: null, clientAction: false } }; this.props.actionListeners.forEach(function (listener) { listener(e); }); this.eventRegistry().publish(e, false); }, _publishActionFinished: function (actionId) { var e = { type: catreact_core_1.CvEventType.ACTION_FIRED, eventObj: { actionId: actionId, type: catreact_core_1.CvActionFiredResultType.ACTION_COMPLETED, source: null, clientAction: false } }; this.props.actionListeners.forEach(function (listener) { listener(e); }); this.eventRegistry().publish(e, false); } });