UNPKG

catreact

Version:

Catavolt Core React Components

141 lines (140 loc) 5.35 kB
"use strict"; /** * Created by rburson on 12/23/15. */ 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; }; Object.defineProperty(exports, "__esModule", { value: true }); 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 (resultCallback) { 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 event_1 = catreact_core_1.CvNavigationResultUtil.publishNavigation(_this.catavolt(), _this.eventRegistry(), launchTry.success, launchAction.actionId, launchAction.workbenchId, _this.props.navTarget, _this.props.launchListeners, false, false); if (resultCallback && typeof resultCallback === 'function') { resultCallback(event_1.eventObj); } } 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 } }; if (resultCallback && typeof resultCallback === 'function') { resultCallback(null, launchTry.failure); } _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 (resultCallback) { _this.performAction(resultCallback); } }; }, _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); } });