'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 Watson Analytics (C) Copyright IBM Corp. 2018, 2019 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', '../../utils/DatasourceUtil'], function (_, DatasourceUtil) { var getMetadataColumnsCallback = function getMetadataColumnsCallback() { return true; }; var _getProjectedDataItems = function _getProjectedDataItems(dataSlots) { var itemIds = []; dataSlots.forEach(function (slotAPI) { var slotDataItems = slotAPI.getDataItemList() || []; slotDataItems.forEach(function (dataItemAPI) { itemIds.push(dataItemAPI.getColumnId()); }); }); return itemIds; }; //Collect only the hierarchies that are in the same tables var _isSameTable = function _isSameTable(tableNames, column) { var colTableName = column.getTableName(); for (var _iterator = tableNames, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref; if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref = _i.value; } var name = _ref; if (colTableName === name) { return true; } } return false; }; var _retrieveItems = function _retrieveItems(metadaColumns, nonProjectedItems, matchById, tableNames) { var result = []; nonProjectedItems.forEach(function (value) { for (var _iterator2 = metadaColumns, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { var _ref2; if (_isArray2) { if (_i2 >= _iterator2.length) break; _ref2 = _iterator2[_i2++]; } else { _i2 = _iterator2.next(); if (_i2.done) break; _ref2 = _i2.value; } var column = _ref2; var columnName = column.getLabel().toLocaleLowerCase(); if (matchById && column.getId() === value) { result.push(columnName); break; } else if (!matchById && columnName === value && _isSameTable(tableNames, column)) { result.push(column.getId()); break; } } }); return _.uniq(result); }; var _getColumnNames = function _getColumnNames(sourceModule, targetModule, tableNames, searchItemIds) { var columnNames = _retrieveItems(sourceModule.getMetadataColumns(getMetadataColumnsCallback), searchItemIds, true, tableNames); if (columnNames.length === 0) { //The item ids might be in the target module so check it here //This case occur when there are multiple viz in same module involve where one vis does not have a projected item but other vis does have the projected item. So //depends on who gets to process first, it can get here. columnNames = _retrieveItems(targetModule.getMetadataColumns(getMetadataColumnsCallback), searchItemIds, true, tableNames); } return columnNames; }; var SynchSlotDataItemAPI = function () { function SynchSlotDataItemAPI() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, SynchSlotDataItemAPI); this.itemId = options.itemId; } SynchSlotDataItemAPI.prototype.getColumnId = function getColumnId() { return this.itemId; }; return SynchSlotDataItemAPI; }(); var SynchSlotAPI = function () { function SynchSlotAPI() { var _this = this; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, SynchSlotAPI); this.generateDataItemIds(options.itemIds); return { getDataItemList: function getDataItemList() { return _this.dataItemAPIs; } }; } SynchSlotAPI.prototype.generateDataItemIds = function generateDataItemIds() { var _this2 = this; var itemIds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; this.dataItemAPIs = []; itemIds.forEach(function (itemId) { _this2.dataItemAPIs.push(new SynchSlotDataItemAPI({ itemId: itemId })); }); }; return SynchSlotAPI; }(); /** * Wrapper class representing a visualization's project items */ var SynchProjectedItem = function () { function SynchProjectedItem(module, item) { _classCallCheck(this, SynchProjectedItem); if (!item) { throw new Error('item object is not provided.'); } this.item = item; this.module = module; return { getLabel: this.getLabel.bind(this), getItemId: this.getItemId.bind(this), getId: this.getItemId.bind(this), getTableName: this.getTableName.bind(this) }; } SynchProjectedItem.prototype.getLabel = function getLabel() { return this.item.itemLabel || this.item.getLabel(); }; SynchProjectedItem.prototype.getItemId = function getItemId() { return this.item.itemId || this.item.getItemId(); }; SynchProjectedItem.prototype.getTableName = function getTableName() { var metadataColumn = this.module.getMetadataColumn(this.getItemId()); return metadataColumn ? metadataColumn.getTableName() : undefined; }; return SynchProjectedItem; }(); /** * This class represents a synchronize context entry use for both page context origin [visualization | filter] types */ var SynchContext = function () { function SynchContext() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, SynchContext); if (!options.fromObject) { throw new Error('fromObject is not provided.'); } this.fromObject = options.fromObject; this.module = options.module; this.synchDataEntry = options.synchDataEntry; this.projectedItems = SynchContextHelper.createSynchProjectedItems(this.module, options.projectedItems); this.ownerWidget = options.ownerWidget; this.widgetIds = [options.widgetId]; this.getTableRef(); } SynchContext.prototype.getAPI = function getAPI() { if (!this.api) { this.api = { getSourceId: this.getSourceId.bind(this), getEventGroupId: this.getEventGroupId.bind(this), getEventSourceId: this.getEventSourceId.bind(this), getTableRef: this.getTableRef.bind(this), getOrigin: this.getOrigin.bind(this), getModule: this.getModule.bind(this), setModule: this.setModule.bind(this), getSynchDataEntry: this.getSynchDataEntry.bind(this), getProjectedItems: this.getProjectedItems.bind(this), setProjectedItems: this.setProjectedItems.bind(this), haveJoinedTables: this.haveJoinedTables.bind(this), getSynchronizeItemId: this.synchDataEntry.getSynchronizeItemId.bind(this.synchDataEntry), getSynchronizeItems: this.synchDataEntry.getSynchronizeItems.bind(this.synchDataEntry), addSynchronizeItem: this.synchDataEntry.addSynchronizeItem.bind(this.synchDataEntry), toJSON: this.synchDataEntry.toJSON.bind(this.synchDataEntry), getWidgetIds: this.getWidgetIds.bind(this) }; } return this.api; }; SynchContext.prototype.getSourceId = function getSourceId() { return this.fromObject.getSourceId(); }; SynchContext.prototype.getEventGroupId = function getEventGroupId() { return this.fromObject.getEventGroupId(); }; SynchContext.prototype.getEventSourceId = function getEventSourceId() { return this.fromObject.getEventSourceId(); }; SynchContext.prototype.getTableRef = function getTableRef() { this.tableRef = this.tableRef || (this.fromObject.getTableRef ? this.fromObject.getTableRef() : DatasourceUtil.getTableRef(this.module, this.projectedItems.map(function (item) { return item.getItemId(); }))); return this.tableRef; }; SynchContext.prototype.getOrigin = function getOrigin() { return this.fromObject.getOrigin(); }; SynchContext.prototype.getModule = function getModule() { return this.module; }; SynchContext.prototype.setModule = function setModule(module) { this.module = module; }; SynchContext.prototype.getSynchDataEntry = function getSynchDataEntry() { return this.synchDataEntry; }; SynchContext.prototype.setSynchDataEntry = function setSynchDataEntry(value) { this.synchDataEntry = value; }; /** * todo: livewidget_cleanup: we should remove this since it is still using * deprecated visAPI.getdataSlots */ SynchContext.prototype.getProjectedItems = function getProjectedItems() { if (this.projectedItems) { return this.projectedItems; } else if (this.visAPI) { this.projectedItems = this.visAPI.getDataSlots(); } return this.projectedItems; }; /** * todo: livewidget_cleanup: we should remove this since it is still using * deprecated visAPI */ SynchContext.prototype.setProjectedItems = function setProjectedItems() { var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; this.projectedItems = items; }; SynchContext.prototype.mergeProjectedItems = function mergeProjectedItems(contextEntry) { var _this3 = this; var itemsToMerge = contextEntry.getProjectedItems(); this.projectedItems = this.projectedItems || []; itemsToMerge.forEach(function (itemToMerge) { var exist = _.find(_this3.projectedItems, function (item) { return itemToMerge.getItemId() === item.getItemId(); }); if (!exist) { _this3.projectedItems.push(itemToMerge); } }); this.tableRef = _.uniq(this.getTableRef().concat(contextEntry.getTableRef())); }; SynchContext.prototype.haveJoinedTables = function haveJoinedTables(synchContextToVerify) { return SynchContextHelper.haveJoinedTables(this, synchContextToVerify); }; SynchContext.prototype.getTableNames = function getTableNames() { return this.module.getTableNames(); }; SynchContext.prototype.addWidgetId = function addWidgetId(id) { if (this.widgetIds.indexOf(id) === -1) { this.widgetIds.push(id); } }; SynchContext.prototype.getWidgetIds = function getWidgetIds() { return this.widgetIds; }; return SynchContext; }(); var SynchContextHelper = function () { function SynchContextHelper() { _classCallCheck(this, SynchContextHelper); } SynchContextHelper.createNewSynchContext = function createNewSynchContext() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return DatasourceUtil.isSupportedNonJoinTableDatascourceType(options.module, options.projectedItems ? options.projectedItems.map(function (item) { return item.getItemId(); }) : null) ? new SynchContext(options) : null; }; SynchContextHelper.getNetSlotsToClearBrushing = function getNetSlotsToClearBrushing() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var sourceModule = options.sourceModule; var targetModule = options.targetModule; var dataSlots = options.dataSlots || []; var tableNames = options.tableNames || []; var pageContext = options.pageContext; var selector = options.selector; var itemIds = options.itemIds || []; var newSelectionIsEmpty = options.newSelectionIsEmpty; var newSectionFromSource = options.newSectionFromSource; var projectedItemIds = _getProjectedDataItems(dataSlots); var nonProjectedItems = _.difference(itemIds, projectedItemIds); var selectionFromProjectedItems = nonProjectedItems.length === 0; if (selectionFromProjectedItems && !newSectionFromSource) { return dataSlots; } var netPageContext = pageContext.getNetPageContext(selector); //Gather all brushed hierarchies var potentialItemIds = []; netPageContext.forEach(function (entry) { potentialItemIds = potentialItemIds.concat(_.pluck(entry.hierarchies, 'hierarchyUniqueName')); }); var columnNames = _getColumnNames(sourceModule, targetModule, tableNames, newSelectionIsEmpty ? potentialItemIds : itemIds); nonProjectedItems = _retrieveItems(targetModule.getMetadataColumns(getMetadataColumnsCallback), columnNames, false, tableNames); if (newSelectionIsEmpty) { var slotsToClear = _.clone(dataSlots); if (nonProjectedItems.length > 0) { slotsToClear.push(new SynchSlotAPI({ itemIds: nonProjectedItems })); } return slotsToClear; } else { //Get a list of column names return nonProjectedItems.length === 0 ? [] : [new SynchSlotAPI({ itemIds: nonProjectedItems })]; } }; SynchContextHelper.getKey = function getKey(sourceId, tableRef) { return sourceId + (tableRef ? tableRef.join('_') : ''); }; SynchContextHelper.haveJoinedTables = function haveJoinedTables(firstSynchContext, secondSynchContext) { if (firstSynchContext.getSourceId() !== secondSynchContext.getSourceId()) { //Different data sources do not have joint table relationships return false; } var module = firstSynchContext.getModule(); var tableNames = firstSynchContext.getTableRef(); var otherTableNames = secondSynchContext.getTableRef(); return DatasourceUtil.haveTableJoinsInSameDataSource(module, tableNames, otherTableNames); }; SynchContextHelper.createSynchProjectedItems = function createSynchProjectedItems(module) { var projectedItems = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var results = []; if (module) { projectedItems.forEach(function (item) { if (!DatasourceUtil.isMultiMeasuresSeriesOrValue(item.getItemId())) { results.push(new SynchProjectedItem(module, item)); } }); } return results; }; SynchContextHelper.replaceUseValue = function replaceUseValue(column, value) { var regex = new RegExp('([^->].*)(->.*)'); if (regex.test(value.u)) { value.u = value.u.replace(regex, column.getId() + '$2'); return true; } }; return SynchContextHelper; }(); return SynchContextHelper; }); //# sourceMappingURL=SynchContextHelper.js.map