'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * Licensed Materials - Property of IBM * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2021 * 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', './SlotsAPISpec', '../SlotsAPI', './Slot', './MappedDataItem', './model/SlotMappingModel'], function (_, APIFactory, SlotsAPISpec, SlotsAPI, Slot, MappedDataItem, SlotMappingModel) { var SlotsImpl = function (_SlotsAPISpec) { _inherits(SlotsImpl, _SlotsAPISpec); function SlotsImpl(widgetModel, dataModel, visualizationImpl, transaction, locale, filterSupport) { _classCallCheck(this, SlotsImpl); var _this = _possibleConstructorReturn(this, _SlotsAPISpec.call(this)); _this.widgetModel = widgetModel; _this.visualizationImpl = visualizationImpl; _this.transaction = transaction; _this.locale = locale; _this.filterSupport = filterSupport; _this._dataItemImplMap = {}; _this.dataModel = dataModel; if (!_this.widgetModel.slotmapping) { _this.widgetModel.slotmapping = {}; } _this.slotMappingModel = new SlotMappingModel(_this.widgetModel.slotmapping); _this._slots = {}; _this._slotAPIList = null; _this.setDefinition(_this.visualizationImpl.getDefinition()); _this._dataItemImplMap = null; _this._initializeDataItems(); _this._deleteDataItemsHandlers = []; //An optional list of handlers that will be called before data items are deleted _this._api = APIFactory.createAPI(_this, [SlotsAPI]); return _this; } SlotsImpl.prototype.destroy = function destroy() { this.widgetModel = null; this.visualizationImpl = null; this.transaction = null; this.locale = null; this.filterSupport = null; this.dataModel = null; this.slotMappingModel = null; for (var id in this._slots) { this._slots[id].destroy(); } this._slots = null; this._slotAPIList = null; for (var _id in this._dataItemImplMap) { this._dataItemImplMap[_id].destroy(); } this._dataItemImplMap = null; this._api = null; }; SlotsImpl.prototype.getLocalFilters = function getLocalFilters() { return this.filterSupport.getLocalFilters(); }; SlotsImpl.prototype._initializeDataItems = function _initializeDataItems() { var _this2 = this; this._dataItemImplMap = {}; this.dataModel.getDataItemList().forEach(function (dataItemModel) { _this2._createDataItemImpl(dataItemModel); }); }; SlotsImpl.prototype._createDataItemImpl = function _createDataItemImpl(model) { var slotModel = this.slotMappingModel.getMappedSlot(model.id); var slot = slotModel ? this.getSlot(slotModel.name) : null; var impl = new MappedDataItem(this.filterSupport, model, slot, this.getDataSource(), this.transaction, this.locale, this.visualizationImpl, this); APIFactory.setParentChildRelation(this, impl); this._dataItemImplMap[model.id] = impl; return impl; }; SlotsImpl.prototype.setDefinition = function setDefinition(definition) { this.definition = definition; this._initializeSlots(); }; SlotsImpl.prototype._initializeSlots = function _initializeSlots() { var _this3 = this; var newSlotAPIList = []; var newSlotsMap = {}; this.definition.getSlotList().forEach(function (slotDefinition) { var slot = _this3.getSlotImpl(slotDefinition.getId()); if (slot) { slot.setDefinition(slotDefinition); } else { slot = new Slot(slotDefinition, _this3.filterSupport, _this3, _this3.transaction, _this3.locale, _this3.visualizationImpl, _this3.dataModel, _this3.slotMappingModel); APIFactory.setParentChildRelation(_this3, slot); } newSlotsMap[slotDefinition.getId()] = slot; newSlotAPIList.push(slot.getAPI()); }); var oldSlots = this._slots; this._slots = newSlotsMap; this._slotAPIList = newSlotAPIList; Object.freeze(this._slotAPIList); // Go over the current list of data items.. and clear the slot for those that are not mapped to a valid slot for (var id in this._dataItemImplMap) { var dataItem = this._dataItemImplMap[id]; var slot = this._dataItemImplMap[id].getSlot(); if (slot && !this._slots[slot.getId()]) { this.slotMappingModel.unmapDataItemsFromSlots([id]); dataItem.setSlot(null); } } // destoy old slot that are no longer valid after the new definition for (var _id2 in oldSlots) { if (!this._slots[_id2]) { oldSlots[_id2].destroy(); } } }; SlotsImpl.prototype.getDataSource = function getDataSource() { return this.visualizationImpl.getDataSource(); }; SlotsImpl.prototype.createDataItems = function createDataItems(dataItemSpecList) { var _this4 = this; var result = []; if (dataItemSpecList) { var ids = []; dataItemSpecList.forEach(function (spec) { ids.push(spec.id); var model = _this4.dataModel.addDataItem(spec); if (!_this4._dataItemImplMap[spec.id]) { _this4._createDataItemImpl(model); } result.push(_this4._dataItemImplMap[spec.id].getAPI()); }); } return result; }; SlotsImpl.prototype.deleteDataItems = function deleteDataItems(dataItemIdList, transaction) { var _this5 = this; var hasLocalFilter = this.visualizationImpl.getLocalFilters().getFilterList().length > 0; var result = []; if (dataItemIdList) { this._deleteDataItemsHandlers.forEach(function (handler) { return handler(dataItemIdList, transaction); }); dataItemIdList.forEach(function (id) { var dataItem = _this5._dataItemImplMap[id]; if (dataItem) { result.push({ id: dataItem.getId(), columnId: dataItem.getColumnId() }); dataItem.destroy(); delete _this5._dataItemImplMap[id]; _this5.slotMappingModel.unmapDataItemsFromSlots([id]); _this5.dataModel.removeDataItem(id, hasLocalFilter); } }); } return result; }; SlotsImpl.prototype.registerDeleteDataItemsHandler = function registerDeleteDataItemsHandler(handler) { this._deleteDataItemsHandlers.push(handler); }; SlotsImpl.prototype.getDataItemImpl = function getDataItemImpl(id) { return this._dataItemImplMap[id] ? this._dataItemImplMap[id] : null; }; SlotsImpl.prototype.getDataItemImplList = function getDataItemImplList() { return Object.values(this._dataItemImplMap); }; SlotsImpl.prototype.getDataItem = function getDataItem(id) { var impl = this.getDataItemImpl(id); return impl ? impl.getAPI() : null; }; SlotsImpl.prototype.getDataItemList = function getDataItemList() { // TODO - livewidget cleanup - cache and free the list for performance var result = []; for (var id in this._dataItemImplMap) { result.push(this._dataItemImplMap[id].getAPI()); } return result; }; /** * set DataItems@dataItemIds to given slot@slotId (and remove the dataItems from Original slot) * @param {*} dataItemIds * @param {*} slotId * @param {*} options * * e.g. * slotA: item1, item2 * slotB: item3 * if try move item2 from slotA to slotB, * * function will be called setDataItems([item2, item3], slotB) */ SlotsImpl.prototype.setDataItems = function setDataItems(dataItemIds, slotId, transactionToken) { var _this6 = this; //step2: set dataItems to target slot // We use the implementation to access certain slot functionality that is not pubblic API // We also use the implementation to avoid firing individual slot events when using some slot functions var targetSlot = this.getSlot(slotId); if (targetSlot) { var subTransaction = this.transaction.startTransaction(transactionToken); //step1:unmap the data items if they are mapped to another slot _.each(dataItemIds, function (dataItemId) { var currentMapping = _this6.getMappingInfo(dataItemId); if (currentMapping && currentMapping.slot && currentMapping.slot.getId() !== slotId) { currentMapping.slot.removeDataItemsMapping([currentMapping.dataItem.getId()], subTransaction); } }); var dataItemsTobeRemoved = []; var currentDataItemIds = []; targetSlot.getDataItemList().forEach(function (dataItem) { var id = dataItem.getId(); currentDataItemIds.push(id); if (dataItemIds.indexOf(id) === -1) { dataItemsTobeRemoved.push(dataItem.getId()); } }); // step2: clear target slot if (currentDataItemIds.length > 0) { targetSlot.removeDataItemsMapping(currentDataItemIds, subTransaction); } // step3: add the new mappings if (dataItemIds.length > 0) { targetSlot.addDataItemsMapping(dataItemIds, null, subTransaction); } // TODO livewidget_cleanup - the original implementation did not clean up unmapped items // we just need to decide the behavior of this API // step4: remove what used to be in the target slot and not longer mapped. //this.getAPI().deleteDataItems(dataItemsTobeRemoved, subTransaction); this.transaction.endTransaction(subTransaction); } }; SlotsImpl.prototype.swapSlots = function swapSlots(sourceSlotId, targetSlotId, transactionToken) { var subTransaction = this.transaction.startTransaction(transactionToken); var sourceSlot = this.getSlot(sourceSlotId); var targetSlot = this.getSlot(targetSlotId); var sourceSlotDataItems = sourceSlot.getDataItemList(); sourceSlot.removeDataItemsMapping(sourceSlotDataItems.map(function (dataItem) { return dataItem.getId(); }), subTransaction); var targetSlotDataItems = targetSlot.getDataItemList(); targetSlot.removeDataItemsMapping(targetSlotDataItems.map(function (dataItem) { return dataItem.getId(); }), subTransaction); sourceSlot.addDataItemsMapping(targetSlotDataItems.map(function (dataItem) { return dataItem.getId(); }), null, subTransaction); targetSlot.addDataItemsMapping(sourceSlotDataItems.map(function (dataItem) { return dataItem.getId(); }), null, subTransaction); this.transaction.endTransaction(subTransaction); }; SlotsImpl.prototype.getSlot = function getSlot(slotId) { var slots = this.getSlotList(); return _.find(slots, function (slot) { return slot.getId() === slotId; }); }; SlotsImpl.prototype.getSlotList = function getSlotList() { // TODO - livewidget cleanup - cache and free the list for performance return this._slotAPIList; }; SlotsImpl.prototype.getMappedSlotList = function getMappedSlotList() { // TODO - livewidget cleanup - cache and free the list for performance var slots = this.getSlotList(); var slotsWithDataItems = []; for (var i = 0; i < slots.length; i++) { var dataItems = slots[i].getDataItemList() || []; if (dataItems.length > 0) { slotsWithDataItems.push(slots[i]); } } return slotsWithDataItems; }; SlotsImpl.prototype.getMappingInfo = function getMappingInfo(dataItemId) { var mappingInfoList = this.getMappingInfoList(); return _.find(mappingInfoList, function (mappingInfo) { return mappingInfo.dataItem.getId() === dataItemId; }); }; SlotsImpl.prototype.getMappingInfoList = function getMappingInfoList() { var _this7 = this; // TODO - livewidget cleanup - cache and free the list for performance var result = []; this.definition.getSlotList().forEach(function (slotDefinition) { var slot = _this7.getSlot(slotDefinition.getId()); var slotImpl = _this7.getSlotImpl(slotDefinition.getId()); slotImpl.getDataItemList().forEach(function (dataItem) { result.push({ slot: slot, dataItem: dataItem, indexInSlot: slot ? slotImpl.getDataItemIndex(dataItem.getId()) : -1 }); }); }); return result; }; /** * @implements SlotsAPI#isMappingComplete */ SlotsImpl.prototype.isMappingComplete = function isMappingComplete(layerId) { var mappedSlotIds = this.getMappedSlotList().map(function (slot) { return slot.getDefinition().getId(); }); var requiredSlots = this.getSlotList().filter(function (slot) { return !slot.getDefinition().isOptional(); }); var datasetList = this.visualizationImpl.getDefinition().getDatasetList(); if (layerId) { datasetList = [{ id: layerId }]; } var sharingSlots = this.getSlotList().filter(function (slot) { return slot.getDefinition().getDatasetIdList().length > 1; }); // if the slot is sharing in two datasets, it's not multilayer var isMultilayers = datasetList.length > 1 && !sharingSlots.length > 0; var mappedDatasetList = datasetList.filter(function (dataset) { var unmappedRequiredSlots = requiredSlots.filter(function (slot) { var slotDatasetIdList = slot.getDefinition().getDatasetIdList(); return slotDatasetIdList.indexOf(dataset.id) !== -1 && !(mappedSlotIds.indexOf(slot.getDefinition().getId()) !== -1); }); return unmappedRequiredSlots.length === 0; }); // If there is only one layer, all datasets should be mapped. if (!isMultilayers) { return datasetList.length === mappedDatasetList.length; } return mappedDatasetList.length > 0; }; SlotsImpl.prototype.clearAllSlots = function clearAllSlots(transactionToken) { this.getSlotList().forEach(function (slot) { slot.removeDataItems(slot.getDataItemList().map(function (dataItem) { return dataItem.getId(); }), transactionToken); }); }; SlotsImpl.prototype.getSlotImpl = function getSlotImpl(slotId) { return this._slots[slotId]; }; SlotsImpl.prototype.getAPI = function getAPI() { return this._api; }; return SlotsImpl; }(SlotsAPISpec); return SlotsImpl; }); //# sourceMappingURL=Slots.js.map