'use strict'; /** * Licensed Materials - Property of IBM * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2016, 2019 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * */ define(['../../../lib/@waca/core-client/js/core-client/ui/core/Class', 'jquery', '../../../lib/@waca/dashboard-common/dist/utils/Flyout', 'underscore', '../../../widgets/livewidget/nls/StringResources'], function (Class, $, Flyout, _, StringResources) { 'use strict'; //The cross tab vis control object. var CrosstabControl = Class.extend({ init: function init(options) { CrosstabControl.inherited('init', this, arguments); this.domNode = options.domNode; this.visualization = options.visualization; this._factSlots = null; // Array of fact type names. }, /** * should be called before the first call to addDataRows */ resetData: function resetData(visModel) { this.visModel = visModel; this._resetInternalData(); }, _resetInternalData: function _resetInternalData() { this._slots = this.visualization.getSlots().getSlotList(); for (var idx = 0; idx < this._slots.length; idx++) { if (!this._slotRowSortOrder && this._slots[idx].getId().indexOf('row') > -1) { this._slotRowSortOrder = this._slots[idx].getDefinition().getSortOrder(); this._slotRowStartIndex = idx; } else if (this._slots[idx].getId().indexOf('value') > -1) { this._slotValueStartIndex = idx; break; } } this._populateFactSlots(); }, clear: function clear() { var $node = $(this.domNode); $node.empty(); }, /** * @public * Pass the required parameter to vis control. */ setGridData: function setGridData(crosstabGrid) { this._crosstabGrid = crosstabGrid; }, _removeNonProjectedSelections: function _removeNonProjectedSelections(aSelectionTuple) { for (var i = 0; i < aSelectionTuple.length; i++) { var selections = aSelectionTuple[i]; //the key for selections is slotIndex which is set to -1 if column is not projected. if (selections[-1]) { delete selections[-1]; } } return aSelectionTuple; }, /** * Apply highlight decoration to the selected measure header cell * @param $node: the node has been selected */ decorateMeasureHeader: function decorateMeasureHeader(node) { this._crosstabGrid.setMeasureHeaderDecorations(node); }, /** * Clear selected measure header cell decoration. */ resetMeasureHeaderDecorations: function resetMeasureHeaderDecorations() { this._crosstabGrid.clearMeasureHeaderDecorations(); }, /** * Apply highlight decoration to the summary cells */ highlightSummary: function highlightSummary(node) { this._crosstabGrid.highlightSummary(node); }, /** * Clear summary cell hightlight. */ resetSummaryHighlight: function resetSummaryHighlight() { this._crosstabGrid.resetSummaryHighlight(); }, clearSelection: function clearSelection(options) { this._crosstabGrid.setSelections({ colSelection: null, datapointSelection: null, rowSelection: null }, options); }, /** * @returns true if the current node includes the 'selected' or 'selected_secondary' or 'implied_selection' class. */ isCellSelected: function isCellSelected(node) { return $(node).hasClass('dashboard-grid-cell-selected'); }, isEdgeNodeSelected: function isEdgeNodeSelected(node) { var $node = $(node); return $node.attr('area') === 'values' || $node.attr('area') === 'cell_edge_column' || $node.attr('area') === 'cell_edge_row'; }, isCrosstaMeasureNodeSelected: function isCrosstaMeasureNodeSelected(node) { return node.textContent && $(node).attr('area') === 'values'; }, /** * @returns a map that with the vis slotSortOrder as the key and the cell clicked as the value. * */ nodeToSelection: function nodeToSelection(event) { if (this._crosstabGrid && this._crosstabGrid.data) { var oDataSelection = {}; var selectionKey = []; var $target = $(event.target); var selectedColumnIdx = $target.attr('col') || $target.parent().attr('col'); var selectedRowIdx = $target.attr('row') || $target.parent().attr('row'); if (selectedColumnIdx !== undefined && selectedRowIdx !== undefined) { var dataCell = this._crosstabGrid.data[selectedRowIdx][selectedColumnIdx]; if (dataCell.ancestor_or_self && dataCell.ancestor_or_self.length >= 2) { oDataSelection.isInnerEdge = true; } if (dataCell.columnEdge) { this._getSelectionOnEdges(oDataSelection, dataCell.columnEdge.descendant_or_self, true, dataCell, selectionKey, 'column_level1'); } if (dataCell.rowEdge) { this._getSelectionOnEdges(oDataSelection, dataCell.rowEdge.descendant_or_self, false, dataCell, selectionKey, 'row_level1'); } if (!dataCell.rowEdge) { this._getSelectionOnEdges(oDataSelection, dataCell.ancestor_or_self, dataCell.type === 'column', dataCell, selectionKey); } if (dataCell.type !== 'column' && dataCell.type !== 'row') { oDataSelection.datapoint = dataCell.value; } if (this.isCrosstaMeasureNodeSelected(event.target) || $(event.target).attr('measure') === 'true') { this._getMeasureHeaderItemValues(oDataSelection, event); } } return oDataSelection; } }, _getSelectionOnEdges: function _getSelectionOnEdges(oDataSelection, values, isColumnMemmber, dataCell, selectionKey) { var sSlotOfItem = isColumnMemmber ? 'column_level1' : 'row_level1'; oDataSelection.selectionContext = { 'slotID': sSlotOfItem, 'level': dataCell.nestLevel }; _.each(values, function (value) { if (!this.isFactSlot(value)) { selectionKey.push(value.u); if (!oDataSelection[sSlotOfItem]) { oDataSelection[sSlotOfItem] = { 'value': [value] }; } else { oDataSelection[sSlotOfItem].value.push(value); } } }.bind(this)); }, _populateFactSlots: function _populateFactSlots() { var facts = []; var slots = this._slots; var factDataslots = []; for (var i = 0; i < slots.length; ++i) { if (slots[i].getDefinition().getType() === 'ordinal') { _.each(slots[i].getDataItemList(), function (entry) { facts.push(entry.getLabel()); //TODO: Review this legacy }); factDataslots.push(slots[i]); } } this._factSlots = facts; this._factDataslots = factDataslots; }, isFactSlot: function isFactSlot(name) { if (!this._factSlots) { this._populateFactSlots(); } return this._factSlots.indexOf(name) >= 0; }, edgeNodeToSelectionObject: function edgeNodeToSelectionObject($target, slot) { var selectedColumnIdx = $target.attr('col') || $target.parent().attr('col'); var selectedRowIdx = $target.attr('row') || $target.parent().attr('row'); var dataCell = this._crosstabGrid.data[selectedRowIdx][selectedColumnIdx]; if (!dataCell.type) { return; } var selectionObj = { 'attributeSelections': [{ column: slot.getMapping(), value: dataCell.value, edgeType: dataCell.type }] }; return selectionObj; }, /** * @returns an array of selected slots mappings for a given data point. */ dataPointNodeToSlotsSelections: function dataPointNodeToSlotsSelections(event) { var $target = $(event.target); var selectedColumnIdx = $target.attr('col') || $target.parent().attr('col'); var selectedRowIdx = $target.attr('row') || $target.parent().attr('row'); var dataCell = this._crosstabGrid.data[selectedRowIdx][selectedColumnIdx]; if (dataCell.type) { return; } var aSlotsSelections = []; this._getSlotsOnEdges(event, aSlotsSelections); if (aSlotsSelections.length === 0) { return; } // Push the cell slot as the last selection. var factSlotSelection = void 0; for (var i = 0; i < aSlotsSelections.length; i++) { if (this.isFactSlot(aSlotsSelections[i].value)) { break; } } if (i < aSlotsSelections.length - 1) { factSlotSelection = aSlotsSelections.splice(i, 1)[0]; } var factSelections = []; var fSelection = void 0; var selectionContext = void 0; if (factSlotSelection) { fSelection = { columnName: factSlotSelection.value, value: dataCell.rawValue }; // The mapIndex of the data item within the values slot that this cell corresponds to selectionContext = this._getFactDataItemIndex(dataCell); } else { // When values are not on (column) edge, it's at the corner edge. find and add the first value slot mapping. var valueSlot = this._slots[2]; //HardCoded if (valueSlot) { fSelection = { columnName: valueSlot.getDataItemList()[0].getLabel(), value: dataCell.rawValue }; } } factSelections.push(fSelection); if (dataCell) { var heatSlot = _.find(this._slots, function (slot) { return slot.getId() === 'heat'; }); var heatDataItemList = heatSlot && heatSlot.getDataItemList(); if (fSelection && heatDataItemList && heatDataItemList.length > 0 && heatDataItemList[0].getLabel() !== fSelection.columnName) { factSelections.push({ columnName: heatDataItemList[0].getLabel(), value: dataCell.condValue ? dataCell.condValue.formatted : dataCell.value }); } } return { attributeSelections: aSlotsSelections, factSelections: factSelections, selectionContext: selectionContext }; }, _getFactDataItemIndex: function _getFactDataItemIndex(dataCell) { // The lowest level subCategory will have the index of the data item within the slot var dataCellCategory = dataCell.columnEdge; while (dataCellCategory.subCategory) { dataCellCategory = dataCellCategory.subCategory; } return dataCellCategory.index; }, _getFactDataslot: function _getFactDataslot(name) { return _.find(this._factDataslots, function (slot) { var mapping = slot.getMapping(); return this.visModel.getLabelByMapping(mapping) === name; }.bind(this)); }, isSummaryNodeSelected: function isSummaryNodeSelected(node) { var name = node.textContent; return name === StringResources.get('summaryCaption'); }, _getSlotsOnEdges: function _getSlotsOnEdges(event, oDataSelection) { var $target = $(event.target); var selectedColumnIdx = $target.attr('col') || $target.parent().attr('col'); var selectedRowIdx = $target.attr('row') || $target.parent().attr('row'); var dataCell = this._crosstabGrid.data[selectedRowIdx][selectedColumnIdx]; if (dataCell.columnEdge) { this._getSlotsSelectionsOnEdges(oDataSelection, dataCell.columnEdge); } if (dataCell.rowEdge) { this._getSlotsSelectionsOnEdges(oDataSelection, dataCell.rowEdge); } if (!dataCell.rowEdge) { this._getSlotsSelectionsOnEdges(oDataSelection, dataCell); } }, edgeNodeToDataslots: function edgeNodeToDataslots(node) { if (!this.isEdgeNodeSelected(node)) { return; } var name = node.textContent; if (this.isFactSlot(name)) { return this._getFactDataslot(name); } var $node = $(node); var slotIdx = Number($node.attr('nestLevel')); if ($(node).hasClass('row')) { slotIdx += this._slotRowStartIndex; } return this._slots[slotIdx]; }, measureNodeToDataslots: function measureNodeToDataslots(node) { if (!this.isCrosstaMeasureNodeSelected(node)) { return; } return this._slots[this._slotValueStartIndex]; }, _getSlotsSelectionsOnEdges: function _getSlotsSelectionsOnEdges(aSlotsSelections, edge) { var category = edge; while (category) { var slot; if (category.isMeasure) { // the edge is a measure slot = this.visualization.getSlots().getSlot('values'); } else { var slotName = category.type === 'column' ? 'column_level1' : 'row_level1'; slot = this.visualization.getSlots().getSlot(slotName); } aSlotsSelections.push({ column: slot.getDataItemList()[0], value: category.value, edgeType: category.type }); category = category.subCategory; } }, _getMeasureHeaderItemValues: function _getMeasureHeaderItemValues(oDataSelection, event) { var _this = this; var targets = void 0; var itemValues = []; // If the header selected and another measure is selected, obtain items for both (for calculation action) var selectedHeaders = this.getSelectedMeasureNodes(); if (selectedHeaders && selectedHeaders.length === 2 && selectedHeaders[0] !== selectedHeaders[1]) { targets = selectedHeaders; } else { targets = [$(event.target)]; } _.each(targets, function (target) { var slotIdx = void 0; var slots = _this.visualization.getSlots().getMappedSlotList(); _.find(slots, function (slot, index) { if (slot.getId() === 'values') { slotIdx = index; return true; } }); var $target = $(target); var dataCell = _this._crosstabGrid.data[$target.attr('row')][$target.attr('col')]; var dataItems = _this.visualization.getSlots().getSlot('values').getDataItemList(); var dataItemIdx = void 0; _.find(dataItems, function (dataItem, index) { if (dataItem.getLabel() === dataCell.value) { dataItemIdx = index; return true; } }); var oMeasure = dataItems[dataItemIdx]; var oItemClass = { 'u': oMeasure.getColumnId(), 'd': oMeasure.getLabel(), 'mapIndex': dataItemIdx }; if (!itemValues[slotIdx]) { itemValues[slotIdx] = []; } itemValues[slotIdx].push(oItemClass); }); oDataSelection.itemClassValues = itemValues; }, /** * Gets a list of the nodes that have been selected * @return {Array.node} - an array of nodes */ getSelectedMeasureNodes: function getSelectedMeasureNodes() { return this._crosstabGrid.getSelectedMeasureNodes(); } }); return CrosstabControl; }); //# sourceMappingURL=CrosstabControl.js.map