'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; 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: BI Dashboard *| (C) Copyright IBM Corp. 2019 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['underscore'], function (_) { 'use strict'; /** * This Class processes query response results and extracts the top-bottom rank to a separate result data * Used by JQGrid.js during rendering process */ var RankProcessor = function () { /** * @Constructor * @param {Object} options */ function RankProcessor(queryResult, findSlotDetailsByRealColumnIndex) { _classCallCheck(this, RankProcessor); this._queryResult = queryResult; this.dataRows = this._queryResult.getDataPointValueList(); this.dataItems = this._queryResult.getResultItemList(); this.findSlotDetailsByRealColumnIndex = findSlotDetailsByRealColumnIndex; // Array of item ids that are being referenced by rank columns this.rankRefIds = []; this.rankDataItems = []; this._prepareRankDataItems(); } /** * Called during initialization. Looks at the original data * and detects the rank columns. For each rank column, generates a * mock rankDataItem which will later be used while rendering the JQGrid. */ RankProcessor.prototype._prepareRankDataItems = function _prepareRankDataItems() { var _this = this; // Look at first row to determine if each column is a rank column this.dataRows[0] && this.dataRows[0].pt.forEach(function (colData, colIndex) { if (_this._getRankValue(colData, colIndex)) { _this.rankRefIds.push(_this.dataItems[colIndex].getId()); var _findSlotDetailsByRea = _this.findSlotDetailsByRealColumnIndex(colIndex), slot = _findSlotDetailsByRea.slot, mapIndex = _findSlotDetailsByRea.mapIndex; var rankDataItemOpt = { refColumnIndex: colIndex, resultDataItem: _this.dataItems[colIndex], aggregationType: slot.getDataItemList()[mapIndex].getAggregation() }; _this.rankDataItems.push(new RankDataItem(rankDataItemOpt)); } }); }; /** * Process Query result based on the passed options to constructor. * @return {Array} rankDataItems */ RankProcessor.prototype.processData = function processData() { if (this.dataRows.length) { this._processDataRows(); this._processDataItems(); } return this.rankDataItems; }; RankProcessor.prototype._processDataRows = function _processDataRows() { _.each(this.dataRows, function (dataRow) { this._processDataPoint(dataRow.pt); }.bind(this)); }; RankProcessor.prototype._processDataPoint = function _processDataPoint(rowData) { var ranks = []; for (var col = 0; col < rowData.length; col++) { var colData = rowData[col]; var rank = this._getRankValue(colData, col); if (rank > 0) { // obtain the reference unique id from the dataitems var ref = this.dataItems[col].getId(); // order of the rank is determined by the order of the references mapped in the rank slot var rankIndex = this.rankRefIds.indexOf(ref); // collect the ranks from each decoration ranks[rankIndex] = { v: rank }; } } // prepare the parameters for splice ranks.splice(0, 0, 0, 0); // prepend all collected ranks to the row Array.prototype.splice.apply(rowData, ranks); }; RankProcessor.prototype._processDataItems = function _processDataItems() { for (var index = 0; index < this.rankRefIds.length; index++) { // append an empty place holder for the ranks append to the datapoints var rankedDataItem = _.find(this.dataItems, function (dataItem) { return dataItem.getId() === this.rankRefIds[index]; }.bind(this)); this.dataItems.splice(index, 0, rankedDataItem); } }; /** * Tries to read the rank value from column data (i.e. each entry inside dataRow.pt) * If finds, returns the rank value, if not returns 0 * * @param {*} colData * @param {*} colIndex * @return {Number} rank value from the data point */ RankProcessor.prototype._getRankValue = function _getRankValue(colData, colIndex) { var rankValue = 0; if ((typeof colData === 'undefined' ? 'undefined' : _typeof(colData)) === 'object') { // continuous data with rank decoration if (colData.deco && colData.deco.rank) { rankValue = colData.deco.rank; } } else { // reference the categorical data items to obtain the rank decoration var tupleItem = this.dataItems[colIndex].getValue(colData)[0]; if (tupleItem.deco && tupleItem.deco.rank) { rankValue = tupleItem.deco.rank; } } return rankValue; }; return RankProcessor; }(); var RankDataItem = function () { /** * @Constructor * @param {Object} options */ function RankDataItem(options) { _classCallCheck(this, RankDataItem); this._refColumnIndex = options.refColumnIndex; this._resultDataItem = options.resultDataItem; this._aggregationType = options.aggregationType; } RankDataItem.prototype.isRank = function isRank() { return true; }; RankDataItem.prototype.getType = function getType() { return 'fact'; }; RankDataItem.prototype.getLabel = function getLabel() { // There should be only one column return this._resultDataItem.getDataItemList()[0].getLabel(); }; RankDataItem.prototype.getId = function getId() { return this._resultDataItem.getId(); }; RankDataItem.prototype.getAggregation = function getAggregation() { return this._aggregationType; }; RankDataItem.prototype.getRefColumnIndex = function getRefColumnIndex() { return this._refColumnIndex; }; return RankDataItem; }(); return RankProcessor; }); //# sourceMappingURL=RankProcessor.js.map