'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /* * Licensed Materials - Property of IBM * IBM Cognos Products: Dashboard * (C) Copyright IBM Corp. 2019, 2020 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', '../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../lib/@waca/dashboard-common/dist/api/ContentActionsProviderAPI', '../../../api/DashboardAPI', './api/EventGroupsAPI', '../../../lib/@waca/dashboard-common/dist/utils/ContentUtil', '../../../app/nls/StringResources'], function (_, APIFactory, ContentActionsProviderAPI, DashboardAPI, EventGroupsAPI, ContentUtil, stringResources) { var EventGroups = function () { function EventGroups(_ref) { var features = _ref.features; _classCallCheck(this, EventGroups); this.dashboard = features.API; this._dashboardState = features.DashboardState; this._api = APIFactory.createAPI(this, [EventGroupsAPI, ContentActionsProviderAPI]); features.ContentActions.registerProvider('EventGroups', this.getAPI()); this._icons = features.Icons; } EventGroups.prototype.getAPI = function getAPI() { return this._api; }; EventGroups.prototype.initialize = function initialize() { var _this = this; // if this promise fails, the system is in really bad shape... therefore not checking for an error case this.dashboard.getCanvasWhenReady().then(function (canvas) { // if there is a dashboard, the instance hasn't been destroyed (just in case the instance gets destroyed before initialization is completed) if (_this.dashboard) { _this.canvas = canvas; _this.canvas.on('move:content', _this.onMoveContent, _this); } }); return Promise.resolve(); }; EventGroups.prototype.onMoveContent = function onMoveContent(payload) { var _this2 = this; var pageId = ContentUtil.getPageContent(this.canvas.getContent(payload.info.containerId)).getId(); var defaultEventGroupId = this.createDefaultGroupId(pageId); var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; var groupsOnPage = _.pluck(eventGroups.getGroupsOnPage(pageId), 'id'); payload.info.contentIdList.forEach(function (id) { var widgetIds = []; var widgets = _this2.canvas.getContent(id).findContent('type:widget'); if (widgets.length) { widgets.forEach(function (content) { var widgetId = content.getId(); var groupModel = eventGroups.findGroup(widgetId); if (groupsOnPage.length && groupModel) { var idx = groupsOnPage.findIndex(function (group) { return group !== groupModel.id; }); if (idx !== -1) { widgetIds.push(widgetId); } } else { widgetIds.push(widgetId); } }); } else { widgetIds.push(id); } _this2.addToGroup(defaultEventGroupId, widgetIds, payload.transactionToken); }); }; EventGroups.prototype.destroy = function destroy() { if (this.canvas) { this.canvas.off('move:content', this.onMoveContent, this); } this.dashboard = null; this._dashboardState = null; this._api = null; }; EventGroups.prototype.groupExists = function groupExists(groupId) { var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; return !!eventGroups.get(groupId); }; EventGroups.prototype.getGroupId = function getGroupId(widgetId) { var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; var group = eventGroups.findGroup(widgetId); if (group) { return group.getId(); } return null; }; EventGroups.prototype.getGroupIdsOnPage = function getGroupIdsOnPage(pageId) { var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; var groups = eventGroups.getGroupsOnPage(pageId); var groupIds = []; if (groups && groups.length) { groups.forEach(function (group) { groupIds.push(group.id); }); } return groupIds; }; EventGroups.prototype.getContentIdList = function getContentIdList(groupId) { var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; var group = eventGroups.get(groupId); if (group && group.widgetIds) { return JSON.parse(JSON.stringify(group.widgetIds)); } }; EventGroups.prototype.addToGroup = function addToGroup(groupId, widgetIds, transactionToken) { var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; var transaction = this.dashboard.getFeature('Transaction'); var token = transaction.startTransaction(transactionToken); var options = { payloadData: { undoRedoTransactionId: token.transactionId, transactionToken: token } }; if (this.groupExists(groupId)) { eventGroups.addToGroup(groupId, widgetIds, options); } else { // create requires only the id, not the event group id options.targetPageId = groupId.slice(0, groupId.indexOf(':')); eventGroups.createGroup(widgetIds, options); } transaction.endTransaction(token); }; EventGroups.prototype.createGroup = function createGroup(widgetIds, transactionToken) { var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; var transaction = this.dashboard.getFeature('Transaction'); var token = transaction.startTransaction(transactionToken); var options = { payloadData: { undoRedoTransactionId: token.transactionId, transactionToken: token } }; var groupId = eventGroups.createGroup(widgetIds, options); transaction.endTransaction(token); return groupId; }; EventGroups.prototype.removeFromGroup = function removeFromGroup(widgetIds, transactionToken) { var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; var transaction = this.dashboard.getFeature('Transaction'); var token = transaction.startTransaction(transactionToken); var options = { payloadData: { undoRedoTransactionId: token.transactionId, transactionToken: token } }; var groupIds = eventGroups.disconnectFromGroup(widgetIds, options); transaction.endTransaction(token); return groupIds; }; EventGroups.prototype._getBoardModel = function _getBoardModel() { var internal = this.dashboard.getFeature('internal'); return internal.getBoardModel(); }; EventGroups.prototype.createDefaultGroupId = function createDefaultGroupId(pageId) { var boardModel = this._getBoardModel(); var eventGroups = boardModel.eventGroups; return eventGroups.createDefaultGroupId(pageId); }; /** * @implements ContentActionProviderAPI#getContentActionList */ EventGroups.prototype.getContentActionList = function getContentActionList(idList) { var _this3 = this; var uiState = this._dashboardState.getUiState(); if (idList.length > 0 || uiState.authoring === false) { return []; } return [{ name: 'EventGroups', type: 'ToggleButton', baseClass: 'toolbar', order: 1, checked: uiState.eventGroups, disabled: uiState.focus, label: stringResources.get('eventGroupButtonLabel'), icon: this._icons.getIcon('connect').id, action: function action() { return _this3._dashboardState.setEventGroups(true); }, offAction: function offAction() { return _this3._dashboardState.setEventGroups(false); } }]; }; return EventGroups; }(); return EventGroups; }); //# sourceMappingURL=EventGroups.js.map