'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: 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. */ /** * @class DataPointSelections * @hideconstructor */ define(['underscore', '../../../../lib/@waca/dashboard-common/dist/core/APIFactory', './api/DataPointSelectionsAPI', './api/deprecated/DataPointSelectionsAPI', './api/DataPointSelectionsAPISpec', '../../../../util/ContentUtil', '../../../../lib/@waca/dashboard-common/dist/utils/ContentUtil', './VisSelected', './PendingSelections'], function (_, APIFactory, DataPointSelectionsAPI, DeprecatedDataPointSelectionsAPI, DataPointSelectionsAPISpec, ContentUtil, CommonContentUtil, VisSelected, PendingSelections) { var DataPointSelections = function (_DataPointSelectionsA) { _inherits(DataPointSelections, _DataPointSelectionsA); function DataPointSelections(options) { _classCallCheck(this, DataPointSelections); var _this = _possibleConstructorReturn(this, _DataPointSelectionsA.call(this, options)); _this.content = options.content; _this.dashboard = options.dashboardAPI; return _this; } DataPointSelections.prototype.getAPI = function getAPI(type) { if (type === 'deprecated') { if (!this._deprecatedApi) { this._deprecatedApi = APIFactory.createAPI(this, [DataPointSelectionsAPI, DeprecatedDataPointSelectionsAPI]); } return this._deprecatedApi; } if (type === 'pending') { if (!this._pending) { var pendingSelections = new PendingSelections(); this._pending = APIFactory.createAPI(pendingSelections, [DataPointSelectionsAPI]); } return this._pending; } if (!this._api) { this._api = APIFactory.createAPI(this, [DataPointSelectionsAPI]); } return this._api; }; DataPointSelections.prototype.select = function select(selections, transactionToken) { var legacySelection = this._getLegacySelection(selections); var multiTuple = selections && selections.length > 1; var controller = this._getController(); controller.select({ itemIds: legacySelection.columnIds, tuple: legacySelection.tuple, command: 'update', slotsToClear: [], pending: false, edgeSelect: legacySelection.edgeSelect, multiTuple: multiTuple }, transactionToken); }; /** * deselect current selections. * @param {*} selections The selected items to deselect * @param {*} transactionToken the transaction Token * @param {*} forceClearSlots if true, clear all slots regardless if there is a selection */ DataPointSelections.prototype.deselect = function deselect(selections, transactionToken) { var forceClearSlots = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var legacySelection = this._getLegacySelection(selections); var multiTuple = selections && selections.length > 1; var visualization = this.content.getFeature('Visualization'); var controller = this._getController(); var _slotsToClear = forceClearSlots ? visualization.getSlots().getMappedSlotList() : this._getSlotsToClear(selections); return controller.select({ itemIds: legacySelection.columnIds, tuple: legacySelection.tuple, command: 'remove', slotsToClear: _slotsToClear, pending: false, edgeSelect: legacySelection.edgeSelect, multiTuple: multiTuple }, transactionToken); }; DataPointSelections.prototype._getSelectionValues = function _getSelectionValues(selections) { // Default. Use the current selections return _.chain(selections || this.getSelections()).map(function (selection) { return selection.categories; }).flatten().map(function (selection) { return selection.value; }).value(); }; DataPointSelections.prototype._getSlotsToClear = function _getSlotsToClear(selections) { var values = this._getSelectionValues(selections); var current = this._getSelectionValues(); if (values.length === current.length && !_.difference(values, current).length) { var visualization = this.content.getFeature('Visualization'); return visualization.getSlots().getMappedSlotList(); } return []; }; DataPointSelections.prototype.clearAll = function clearAll(transactionToken) { var selections = this._buildSelections( /*selectedDataItemList*/[], /*strictMatch*/false) || []; return this.deselect(selections, transactionToken, true); }; DataPointSelections.prototype._getVisualization = function _getVisualization() { if (!this.visualization) { this.visualization = this.content.getFeature('Visualization'); } return this.visualization; }; DataPointSelections.prototype._getController = function _getController() { if (!this.controller) { this.controller = this.content.getFeature('SelectionController.deprecated'); } return this.controller; }; DataPointSelections.prototype._getLegacySelection = function _getLegacySelection(selections) { var legacySelection = { columnIds: [], tuple: [], edgeSelect: !!_.find(selections, function (selection) { return selection.edgeSelect; }) }; selections.forEach(function (selection) { var values = []; var addedValues = []; selection.categories.forEach(function (category) { if (!(legacySelection.columnIds.indexOf(category.columnId) !== -1)) { legacySelection.columnIds.push(category.columnId); } if (!(addedValues.indexOf(category.columnId + category.value) !== -1)) { values.push({ u: category.value, d: category.label }); addedValues.push(category.columnId + category.value); } }); if (values.length) { legacySelection.tuple.push(values); } }); return legacySelection; }; DataPointSelections.prototype.getSelections = function getSelections() { var selectedDataItemList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return this._buildSelections(selectedDataItemList); }; DataPointSelections.prototype._getScope = function _getScope() { var type = this.dashboard.getAppConfig('pageContainerType'); var page = CommonContentUtil.getPageContent(this.content, type); var scope = page && page.getId(); return scope; }; DataPointSelections.prototype._getEventGroupId = function _getEventGroupId() { var eventGroups = this.dashboard.getFeature('EventGroups'); var eventGroupId = eventGroups.getGroupId(this.content.getId()); return eventGroupId; }; DataPointSelections.prototype._getPageContext = function _getPageContext() { if (!this._pageContext) { this._pageContext = ContentUtil.getPageContext(this.dashboard); } return this._pageContext; }; DataPointSelections.prototype._buildSelections = function _buildSelections(selectedDataItemList) { var strictMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var dataPointSelections = []; var dataSource = this._getVisualization().getDataSource(); var sourceId = dataSource && dataSource.getId(); if (sourceId) { var _dataPointSelections; var selectors = { scope: this._getScope(), eventGroupId: this._getEventGroupId(), sourceId: sourceId, origin: 'visualization', eventSourceId: this.content.getId(), _strictMatch: strictMatch }; var slots = this._getVisualization().getSlots(); var mappedDataItemList = slots.getDataItemList(); /** * If there is either brushing or pending filters collect data points or edge values. * Note: pageContextEntry.getDataPoints() returns an array of array, i.e. an array of tuples, * while pageContextEntry.getValues() returns an array of values. */ var netPageContext = this._getPageContext().getNetPageContextItems(selectors); (_dataPointSelections = dataPointSelections).push.apply(_dataPointSelections, this._getValues(netPageContext, selectedDataItemList, mappedDataItemList)); if (dataPointSelections.length) { dataPointSelections = this._uniqueValues(dataPointSelections); } } return dataPointSelections; }; DataPointSelections.prototype._getValues = function _getValues(pageContextEntries, selectedDataItemList, mappedDataItemList) { var _this2 = this; var payloadValueList = []; var payloadValue = null; var dataItem = null; if (pageContextEntries && pageContextEntries.length) { pageContextEntries.forEach(function (pEntry) { var columnIdList = pEntry.getItemIds(); if (pEntry.isDataPointType()) { var payloadTupleList = []; var tupleList = pEntry.getDataPoints(); tupleList.forEach(function (tuple) { var payloadTuple = []; tuple.forEach(function (value, valueIdx) { dataItem = _this2._getDataItem(columnIdList[valueIdx], selectedDataItemList, mappedDataItemList); if (dataItem && dataItem.getType() === 'attribute') { payloadValue = _this2._toPayLoadValue(value, columnIdList[valueIdx], dataItem.getId()); payloadTuple.push(payloadValue); } }); payloadTupleList.push({ categories: payloadTuple }); }); payloadValueList.push.apply(payloadValueList, payloadTupleList); } else { var values = pEntry.getValues(); values.forEach(function (value) { dataItem = _this2._getDataItem(columnIdList[0], selectedDataItemList, mappedDataItemList); if (dataItem && dataItem.getType() === 'attribute') { payloadValue = _this2._toPayLoadValue(value, columnIdList[0], dataItem.getId()); payloadValueList.push({ categories: [payloadValue] }); } }); } }); } return payloadValueList; }; DataPointSelections.prototype._getDataItem = function _getDataItem(columnId, selectedDataItemList, mappedDataItemList) { var dataItem = _.find(selectedDataItemList, function (dataItem) { return dataItem.getColumnId() === columnId; }); if (!dataItem) { dataItem = _.find(mappedDataItemList, function (dataItem) { return dataItem.getColumnId() === columnId; }); } return dataItem; }; DataPointSelections.prototype._toPayLoadValue = function _toPayLoadValue(value, columnId, dataItemId) { var resultPayLoad = { dataItemId: dataItemId, columnId: columnId, value: value.u, label: value.d }; if (value.p) { resultPayLoad.parentId = value.p.u; } return resultPayLoad; }; DataPointSelections.prototype._uniqueValues = function _uniqueValues() { var dataPointSelections = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var uniqueValues = []; var valuesMap = {}; dataPointSelections.forEach(function (dataPoint) { var categoryKey = _.pluck(dataPoint.categories, 'value').join(); if (!valuesMap[categoryKey]) { valuesMap[categoryKey] = 1; uniqueValues.push(dataPoint); } }); return uniqueValues; }; DataPointSelections.prototype.isSelected = function isSelected(selectionPayload) { var resolvedSelections = this._getLegacySelection(selectionPayload); return resolvedSelections.edgeSelect ? this._isEdgeSelected(selectionPayload) : this._isDataPointSelected(resolvedSelections); }; /** * @return {boolean} true if the resolved items are identical to the current selection */ DataPointSelections.prototype._isDataPointSelected = function _isDataPointSelected(resolvedSelections) { var selection = this.getSelection(resolvedSelections.columnIds); return selection ? selection.isSelected(_.flatten(resolvedSelections.tuple)) : false; }; /** * @return {boolean} true if the resolved items are identical to the current selection */ DataPointSelections.prototype._isEdgeSelected = function _isEdgeSelected(selections) { var _this3 = this; var getSelectedEdgeValues = function getSelectedEdgeValues(columnId) { return _this3._getController().getSelectedTuples([columnId]); }; // The format of resolved and the result of visSelectionController.getSelectedTuples() do not match. // Perform a sequence of operations to transform the output of getSelectedTuples() into the same format as resolved. var selectionTuples = _.chain(_.map(this._getVisualization().getSlots().getDataItemList(), function (dataItem) { var columnId = dataItem.getColumnId(); var values = getSelectedEdgeValues(columnId); return _.map(values, function (value) { value.label = _.isUndefined(value.label) ? value.d : value.label; value.value = _.isUndefined(value.value) ? value.u : value.value; return { columnId: columnId, tupleItem: value }; }); })).flatten().filter(function (itemTuplePair) { return !!itemTuplePair.tupleItem; }) // Remove items with empty tuples .value(); // End sequence of transformations - get final array // check if the resolved tuple is a subset of the selectionTuples var isSelected = true; var categorySelections = _.pluck(selections, 'categories'); _.each(categorySelections, function (resolvedTuple) { _.each(resolvedTuple, function (tupleValue) { isSelected = isSelected ? !!_.find(selectionTuples, function (selectedTuple) { return selectedTuple.columnId === tupleValue.columnId && selectedTuple.tupleItem.u === tupleValue.value; }) : false; }); }); return isSelected; }; /** * Get a selection object that corresponds to the supplied list of itemIds. * @param itemIds array of item ids to be considered part of the selection * If omitted, apply all dataitem * @param {object} options selection options to describe the selection * @param {boolean} options.includeParents[includeParents=false]- if includes parents; default to false * @param {boolean} options.ignoreEdgeSelections[ignoreEdgeSelections=false] - if includes parents; default to false * @return selection object */ DataPointSelections.prototype.getSelection = function getSelection(itemIds, options) { if (!itemIds) { itemIds = ContentUtil.getColumnIdList(this._getVisualization().getSlots().getMappedSlotList(), 'attribute'); } return this._getSelected(itemIds, options); }; /** * Return a proxy object that returns the selected items/tuples as an object for fast lookup. * @param itemIds - the itemIds that form a tuple as an array ['Products', 'Years'] * (also supports a single itemId) * @returns a proxy object that returns an isSelected interface about what is selected by the itemIds. * or null if no selections exist for any of the set of items. */ DataPointSelections.prototype._getSelected = function _getSelected(itemIds, options) { if (!this._getVisualization().getDataSource()) { return null; } var visualization = this._getVisualization(); var dataSource = visualization.getDataSource(); var selected = new VisSelected({ pageContextAPI: this._getPageContext(), itemIds: Array.isArray(itemIds) ? itemIds : [itemIds], scope: this._getScope(), eventGroupId: this._getEventGroupId(), sourceId: dataSource && dataSource.getId(), includeParents: options && options.includeParents === true ? true : false, //Default including parents to false (not applicable to charts) ignoreEdgeSelections: options && options.ignoreEdgeSelections === true ? true : false, synchronizeData: this._getController().synchronizeData // TODO will be replaced with synchronized feature soon }); return selected.hasSelections() ? selected.getAPI() : null; }; return DataPointSelections; }(DataPointSelectionsAPISpec); return DataPointSelections; }); //# sourceMappingURL=DataPointSelections.js.map