Pivot.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. 'use strict';
  2. 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; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Cognos Products: Dashboard
  7. * (C) Copyright IBM Corp. 2016, 2020
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. define(['../../../util/DashboardFormatter', '../../../widgets/livewidget/nls/StringResources', './CrosstabEdgeIterator', '../../../widgets/livewidget/query/QueryResultDataUtils'], function (Formatter, StringResources, CrosstabEdgeIterator, QueryResultDataUtils) {
  11. /**
  12. This class pivots the query data,
  13. [[column-level1, column-level2, row-level1, row-level2, measure1, measure2, .. measure-n ],
  14. [column-level1, column-level2, row-level1, row-level2, measure1, measure2, .. measure-n ],
  15. [ ... ]]
  16. into a table that can be easily rendered into a crosstab
  17. The transformed table looks something like :
  18. [ [ '' '' row-level1 row-level1 row-level1 row-level1 ... ]
  19. [ '' '' row-level2 row-level2 row-level2 row-level2 ... ]
  20. [ ... ]
  21. [ column-level1 column-level2 fact fact fact fact fact ... ]
  22. [ column-level1 column-level2 fact fact fact fact fact ... ]
  23. [ ... ]
  24. ]
  25. **/
  26. 'use strict';
  27. var SUMMARY_ID = StringResources.get('summaryCaption');
  28. var ROW = 'row';
  29. var COL = 'col';
  30. var COLUMN = 'column';
  31. var ORDINAL = 'ordinal';
  32. var HEAT = 'heat';
  33. var DATE = 'date';
  34. var UNDEFINED = 'undefined';
  35. var Pivot = function () {
  36. function Pivot(options) {
  37. var _this = this;
  38. _classCallCheck(this, Pivot);
  39. this.slots = options.slots;
  40. this.data = options.data;
  41. this.visModel = options.visModel;
  42. this.dashboard = options.dashboard;
  43. this.conditionalFormatting = options.conditionalFormatting;
  44. this.rowEdge = {};
  45. this.columnEdge = {};
  46. this.valuesMap = {};
  47. this.colEdgeNestingLevel = 0;
  48. this.rowEdgeNestingLevel = 0;
  49. // Index tof the first value (measure)
  50. this._valuesStartIdx = 0;
  51. this._measuresInfo = [];
  52. this._rowInfo = [];
  53. this._columnInfo = [];
  54. this._conditionalMeasureInfoNew = [];
  55. this.indexInfo = {
  56. row: [],
  57. col: []
  58. };
  59. this.levelInfo = {
  60. row: {},
  61. col: {}
  62. };
  63. this.summaryValuePromises = [];
  64. var slotsAPIs = this.slots.getMappedSlotList();
  65. if (options && options.data && options.data.getVersion && options.data.getVersion() === '2') {
  66. this._v2Summaries = true;
  67. } else {
  68. this.summaryAPI = this.visModel.ownerWidget.getFeature('summaryFeature');
  69. }
  70. var resultDataItemList = QueryResultDataUtils.getResultDataItemList(this.data.getResultItemList());
  71. var oSlot = void 0,
  72. aDataItems = void 0,
  73. dataItem = void 0;
  74. for (var slotIndex = 0; slotIndex < slotsAPIs.length; slotIndex++) {
  75. oSlot = slotsAPIs[slotIndex];
  76. aDataItems = oSlot.getDataItemList();
  77. if (oSlot.getId().indexOf(ROW) !== -1) {
  78. this._valuesStartIdx++;
  79. for (var dataItemIndex = 0; dataItemIndex < aDataItems.length; dataItemIndex++) {
  80. dataItem = aDataItems[dataItemIndex];
  81. this.rowEdgeNestingLevel++;
  82. this._addEdgeDataItemInfo(slotIndex, dataItemIndex, ROW);
  83. this._rowInfo.push({
  84. id: dataItem.getId(),
  85. dataType: oSlot.getDefinition().getType(),
  86. formatSpec: dataItem.getFormat()
  87. });
  88. }
  89. }
  90. if (oSlot.getId().indexOf(COLUMN) !== -1) {
  91. this._valuesStartIdx++;
  92. for (var _dataItemIndex = 0; _dataItemIndex < aDataItems.length; _dataItemIndex++) {
  93. dataItem = aDataItems[_dataItemIndex];
  94. this.colEdgeNestingLevel++;
  95. this._addEdgeDataItemInfo(slotIndex, _dataItemIndex, COL);
  96. this._columnInfo.push({
  97. id: dataItem.getId(),
  98. dataType: oSlot.getDefinition().getType(),
  99. formatSpec: dataItem.getFormat()
  100. });
  101. }
  102. }
  103. if (oSlot.getDefinition().getType() === ORDINAL) {
  104. for (var _dataItemIndex2 = 0; _dataItemIndex2 < aDataItems.length; _dataItemIndex2++) {
  105. dataItem = aDataItems[_dataItemIndex2];
  106. var aggregationType = dataItem.getAggregation();
  107. var info = {
  108. id: dataItem.getId(),
  109. label: dataItem.getLabel(),
  110. aggregationType: aggregationType,
  111. formatSpec: dataItem.getFormat(),
  112. columnId: dataItem.getColumnId()
  113. };
  114. // exclude the conditional measure from other measures
  115. if (oSlot.getId() === HEAT) {
  116. this._conditionalMeasureInfo = info;
  117. if (this.conditionalFormatting) {
  118. this._conditionalMeasureInfoNew.push(info);
  119. }
  120. } else if (this._measureItemHasData(resultDataItemList, dataItem)) {
  121. this._measuresInfo.push(info);
  122. }
  123. }
  124. // set measure index for the conditionals
  125. if (this.conditionalFormatting) {
  126. (function () {
  127. var condFormats = _this.conditionalFormatting.getConditionalFormatting();
  128. if (condFormats && condFormats.conditionalFormats) {
  129. _this._conditionalMeasureInfoNew.forEach(function (conditional) {
  130. var condFormat = condFormats.conditionalFormats.find(function (cf) {
  131. return cf.heatId === conditional.id;
  132. });
  133. if (condFormat) {
  134. _this._measuresInfo.forEach(function (measureInfo, index) {
  135. if (measureInfo.columnId === condFormat.dataItemId) {
  136. conditional.measureIndex = index;
  137. }
  138. });
  139. }
  140. });
  141. }
  142. })();
  143. }
  144. }
  145. }
  146. this._colRowInfo = this._columnInfo.concat(this._rowInfo);
  147. this._columnEdgeHasMeasure = this._measuresInfo.length > 1 || this.colEdgeNestingLevel === 0;
  148. if (this._columnEdgeHasMeasure) {
  149. this.colEdgeNestingLevelWithMeasure = this.colEdgeNestingLevel + 1;
  150. } else {
  151. this._crosstabMeasure = this._measuresInfo.length ? this._measuresInfo[0].label : null;
  152. }
  153. }
  154. //Add the dataItem index info to cache
  155. Pivot.prototype._addEdgeDataItemInfo = function _addEdgeDataItemInfo(slotIndex, nestedIndex, rowOrCol) {
  156. if (this.data) {
  157. if (rowOrCol === ROW) {
  158. this.indexInfo.row.push({
  159. slotIndex: slotIndex,
  160. nestedIndex: nestedIndex
  161. });
  162. } else {
  163. this.indexInfo.col.push({
  164. slotIndex: slotIndex,
  165. nestedIndex: nestedIndex
  166. });
  167. }
  168. }
  169. };
  170. Pivot.prototype.create = function create() {
  171. this._hideSummaries = this.visModel.getPropertyValue('hideSummaries');
  172. this._generateEdgesAndValueMap();
  173. //Build the pivot table
  174. var pivotTable = [];
  175. this._addColumnEdgeCategories(pivotTable);
  176. this._addRowEdgeCategoriesAndFactCells(pivotTable);
  177. return pivotTable;
  178. };
  179. Pivot.prototype.getNestingLevels = function getNestingLevels() {
  180. if (this._columnEdgeHasMeasure) {
  181. return {
  182. colNestingLevel: this.colEdgeNestingLevel + 1,
  183. rowNestingLevel: this.rowEdgeNestingLevel
  184. };
  185. } else {
  186. return {
  187. colNestingLevel: this.colEdgeNestingLevel,
  188. rowNestingLevel: this.rowEdgeNestingLevel
  189. };
  190. }
  191. };
  192. Pivot.prototype.getLevelInfo = function getLevelInfo() {
  193. return this.levelInfo;
  194. };
  195. /**
  196. * Extract either a cell value or the "tuple part" of an edge value at the specified nesting depth.
  197. * eg: for row n containing tuple(2004, Canada) on the rows (measure on columns)
  198. * getCellValue(n, 0, 0) would return '2004'
  199. * getCellValue(n, 0, 1) would return 'Canada'
  200. * getCellValue(n, 1) would return the measure value.
  201. * @param {number} i - the row
  202. * @param {number} c - the column base (it will be the same for all levels of nesting in a tuple)
  203. * @param {number} nestedIndex - the nesting level (which tuple part?)
  204. */
  205. Pivot.prototype.getCellValue = function getCellValue(i, c, nestedIndex) {
  206. var originalResult = this.data.getValue(i, c);
  207. var result = originalResult;
  208. var isSummary = result && result.type === this.data.VALUE_TYPE.SUMMARY;
  209. if (Array.isArray(result) && nestedIndex !== undefined) {
  210. result = result[nestedIndex];
  211. }
  212. if (isSummary) {
  213. //This code is specific for v2 summaries.
  214. //If this is an 'outersummary', the tuple length will be shorter than the nesting depth.
  215. //Backfill with the most detailed summary.
  216. result = result || originalResult && originalResult.length && originalResult[originalResult.length - 1];
  217. if (result) {
  218. //Tuples (not tuple parts) are marked as summaries but we extract tupleParts in crosstab.
  219. //If we need to mark this tuplepart as a summary, mark a copy not the original!
  220. result = JSON.parse(JSON.stringify(result));
  221. result.label = SUMMARY_ID;
  222. result.isSummary = true;
  223. }
  224. }
  225. return result;
  226. };
  227. /**
  228. * This function calculate the levelInfo for rowEdge and colEdge
  229. * @param {string} row or col
  230. * @param {number} rowIndex or colIndex
  231. * @param {number} levelNumber
  232. */
  233. Pivot.prototype._determineLevelInfo = function _determineLevelInfo(type, index, levelNumber) {
  234. if (this.levelInfo[type][index] !== undefined) {
  235. var _levelInfo$type$index = this.levelInfo[type][index],
  236. rootLevelnumber = _levelInfo$type$index.rootLevelnumber,
  237. maxLevelDepth = _levelInfo$type$index.maxLevelDepth;
  238. this.levelInfo[type][index] = {
  239. rootLevelnumber: Math.min(rootLevelnumber, levelNumber),
  240. maxLevelDepth: Math.max(maxLevelDepth, levelNumber - rootLevelnumber)
  241. };
  242. } else {
  243. this.levelInfo[type][index] = {
  244. rootLevelnumber: levelNumber,
  245. maxLevelDepth: 0
  246. };
  247. }
  248. };
  249. /**
  250. * this function process the incoming data and generates the colEdge, rowEdge and valuesMap which is then used to
  251. * to create the pivot table
  252. */
  253. Pivot.prototype._generateEdgesAndValueMap = function _generateEdgesAndValueMap() {
  254. var _this2 = this;
  255. var rowEdge = {};
  256. var columnEdge = {};
  257. var numberOfRows = this.data.getRowCount();
  258. var numberOfColumns = QueryResultDataUtils.getDataItemCount(this.data.getResultItemList());
  259. // projected measure ending index
  260. var heatIndex = this._valuesStartIdx + this._measuresInfo.length;
  261. for (var i = 0; i < numberOfRows; i++) {
  262. // get the row edge dimension values
  263. var isSummaryCell = false;
  264. var rowDimensions = [];
  265. for (var cr = 0; cr < numberOfColumns; cr++) {
  266. // Make sure we have index info for this column
  267. if (this.indexInfo.row[cr] !== undefined) {
  268. var cellValue = this.getCellValue(i, this.indexInfo.row[cr].slotIndex, this.indexInfo.row[cr].nestedIndex);
  269. //Cellvalue can be null for some values in unbalanced hierarchies. Only contribute dimensions that are populated.
  270. if (cellValue) {
  271. isSummaryCell = isSummaryCell || cellValue.isSummary; //This will only be set for v2.
  272. rowDimensions.push(cellValue);
  273. // populate the levelInfo for row edge (fixed columns)
  274. var levelNumber = cellValue && cellValue.ln;
  275. if (levelNumber !== undefined) {
  276. this._determineLevelInfo(COL, cr, levelNumber);
  277. }
  278. }
  279. }
  280. }
  281. this._addDimensionToEdge(rowEdge, rowDimensions, this.rowEdgeNestingLevel, ROW);
  282. // get the column edge dimension values
  283. var columnDimensions = [];
  284. for (var cc = 0; cc < numberOfColumns; cc++) {
  285. // Make sure we have index info for this row
  286. if (this.indexInfo.col[cc] !== undefined) {
  287. var _cellValue = this.getCellValue(i, this.indexInfo.col[cc].slotIndex, this.indexInfo.col[cc].nestedIndex);
  288. //Cellvalue can be null for some values in unbalanced hierarchies. Only contribute dimensions that are populated.
  289. if (_cellValue) {
  290. isSummaryCell = isSummaryCell || _cellValue.isSummary; //This will only be set for v2.
  291. columnDimensions.push(_cellValue);
  292. // populate the levelInfo for column edge (fixed rows)
  293. var _levelNumber = _cellValue && _cellValue.ln;
  294. if (_levelNumber !== undefined) {
  295. this._determineLevelInfo(ROW, cc, _levelNumber);
  296. }
  297. }
  298. }
  299. }
  300. this._addDimensionToEdge(columnEdge, columnDimensions, this.colEdgeNestingLevel, COLUMN, this._columnEdgeHasMeasure ? this._measuresInfo : undefined);
  301. // separate the conditional measure from other measure values
  302. var measureValues = [];
  303. for (var cm = this._valuesStartIdx; cm < heatIndex; cm++) {
  304. measureValues.push(this.getCellValue(i, cm));
  305. }
  306. if (this.conditionalFormatting) {
  307. // get the conditional values
  308. for (var _cm = 0; _cm < this._conditionalMeasureInfoNew.length; _cm++) {
  309. this._conditionalMeasureInfoNew[_cm].condValue = !isSummaryCell ? this.getCellValue(i, _cm + heatIndex).value : undefined;
  310. }
  311. }
  312. var conditionalValue = this._conditionalMeasureInfo && !isSummaryCell ? this.getCellValue(i, heatIndex).value : undefined;
  313. var dimensions = void 0;
  314. if (this._columnEdgeHasMeasure) {
  315. var _loop = function _loop(j) {
  316. dimensions = columnDimensions.concat(_this2._measuresInfo[j].label).concat(rowDimensions);
  317. if (_this2.conditionalFormatting) {
  318. var conditionalMeasureInfo = _this2._conditionalMeasureInfoNew.find(function (condMeasure) {
  319. return condMeasure.measureIndex === j;
  320. });
  321. conditionalValue = conditionalMeasureInfo ? conditionalMeasureInfo.condValue : undefined;
  322. _this2._addValuesToMap(dimensions, measureValues[j] ? measureValues[j].value : measureValues[j], _this2._measuresInfo[j], conditionalValue, conditionalMeasureInfo);
  323. } else {
  324. _this2._addValuesToMap(dimensions, measureValues[j] ? measureValues[j].value : measureValues[j], _this2._measuresInfo[j], conditionalValue);
  325. }
  326. };
  327. //for each measure, add its column label as part of the dimensions array to be process as normal
  328. for (var j = 0; j < this._measuresInfo.length; j++) {
  329. _loop(j);
  330. }
  331. } else {
  332. dimensions = columnDimensions.concat(rowDimensions);
  333. if (this.conditionalFormatting) {
  334. conditionalValue = this._conditionalMeasureInfoNew[0] ? this._conditionalMeasureInfoNew[0].condValue : undefined;
  335. this._addValuesToMap(dimensions, measureValues[0] ? measureValues[0].value : measureValues[0], this._measuresInfo[0], conditionalValue, this._conditionalMeasureInfoNew[0]);
  336. } else {
  337. this._addValuesToMap(dimensions, measureValues[0] ? measureValues[0].value : measureValues[0], this._measuresInfo[0], conditionalValue);
  338. }
  339. }
  340. }
  341. this.rowEdge = this._getEdgeWithSortedKeys(rowEdge, this.indexInfo.row, this.rowEdgeNestingLevel);
  342. this.columnEdge = this._getEdgeWithSortedKeys(columnEdge, this.indexInfo.col, this.colEdgeNestingLevelWithMeasure || this.colEdgeNestingLevel);
  343. };
  344. /**
  345. * Returns a new object with edge and sortedKeys info.
  346. */
  347. Pivot.prototype._getEdgeWithSortedKeys = function _getEdgeWithSortedKeys(edge, indicesMap, nestingLevel, level, nestedCategories) {
  348. level = level || 0;
  349. var sortedKeys = this._getSortedKeys(edge, indicesMap[level], nestedCategories);
  350. var newEdgeObject = {
  351. edge: edge,
  352. sortedKeys: sortedKeys
  353. };
  354. if (level < nestingLevel - 1) {
  355. var categories = nestedCategories || [];
  356. for (var _category in edge) {
  357. if (edge.hasOwnProperty(_category)) {
  358. var nestedEdges = edge[_category].nestedEdges;
  359. if (nestedEdges) {
  360. var currLevel = _category === SUMMARY_ID ? nestingLevel : level + 1;
  361. categories[level] = _category;
  362. edge[_category].nestedEdges = this._getEdgeWithSortedKeys(nestedEdges, indicesMap, nestingLevel, currLevel, categories);
  363. // reset catergory nesting
  364. if (level === 0) {
  365. categories = [];
  366. // pop catergory nesting
  367. } else if (categories.length) {
  368. categories = categories.slice(categories.length);
  369. }
  370. }
  371. }
  372. }
  373. }
  374. return newEdgeObject;
  375. };
  376. Pivot.prototype._getSortedKeys = function _getSortedKeys(srcObject, indices, nestedCategories) {
  377. var sortedKeys = [];
  378. if ((typeof indices === 'undefined' ? 'undefined' : _typeof(indices)) === UNDEFINED) {
  379. for (var key in srcObject) {
  380. if (key !== SUMMARY_ID && srcObject.hasOwnProperty(key)) {
  381. sortedKeys.push(key);
  382. }
  383. }
  384. } else {
  385. var resultItem = this.data.getResultItemList()[indices.slotIndex];
  386. for (var i = 0, tupleCount = resultItem.getRowCount(); i < tupleCount; i++) {
  387. var tuple = resultItem.getValue(i);
  388. // need to compare the tuple nesting to nested categories in order to prevent
  389. // processing the wrong tuple. First, we need to get the values from the tuple, then
  390. // based on the nested index (level), only use up to that level for the comparison.
  391. var good = (typeof nestedCategories === 'undefined' ? 'undefined' : _typeof(nestedCategories)) === UNDEFINED;
  392. if (!good) {
  393. good = true;
  394. // PERFORMANCE - do the comparison without creating or copying arrays as was done below...
  395. // let selectVals = _.pluck(tuple, 'value');
  396. // good = _.difference(nestedCategories, selectVals.slice(0, indices.nestedIndex)).length === 0;
  397. for (var k = 0; k < indices.nestedIndex; k++) {
  398. if (_typeof(tuple[k]) !== UNDEFINED && _typeof(nestedCategories[k]) !== UNDEFINED) {
  399. if (nestedCategories[k] !== tuple[k].value) {
  400. good = false;
  401. break;
  402. }
  403. }
  404. }
  405. }
  406. if (good) {
  407. for (var j = 0; j < tuple.length; j++) {
  408. var _key = tuple[j].value;
  409. //Only include key in the sortedKeys if it actualy exists in srcObject
  410. //(i.e. if a row/column doesn't include a given data item member, don't include it in the crosstab)
  411. if (_key && srcObject[_key] && sortedKeys.indexOf(_key) === -1) {
  412. sortedKeys.push(_key);
  413. break;
  414. }
  415. }
  416. }
  417. }
  418. }
  419. if (_typeof(srcObject[SUMMARY_ID]) !== UNDEFINED) {
  420. // add the summary back as the last key
  421. sortedKeys.push(SUMMARY_ID);
  422. }
  423. return sortedKeys;
  424. };
  425. Pivot.prototype._addColumnEdgeCategories = function _addColumnEdgeCategories(pivotTable) {
  426. var colEdgeNestingLevel = this.colEdgeNestingLevelWithMeasure || this.colEdgeNestingLevel;
  427. var i = void 0,
  428. j = void 0;
  429. for (i = 0; i < colEdgeNestingLevel; i++) {
  430. var emptyCells = [];
  431. for (j = 0; j < this.rowEdgeNestingLevel; j++) {
  432. emptyCells.push({
  433. type: 'corner',
  434. value: this._crosstabMeasure || ''
  435. });
  436. }
  437. pivotTable.push(emptyCells);
  438. }
  439. var columnEdgeIterator = new CrosstabEdgeIterator(this.columnEdge);
  440. var nextColumnCategory = void 0,
  441. lastValue = void 0,
  442. rowIndex = void 0,
  443. category = void 0;
  444. while (nextColumnCategory = columnEdgeIterator.next()) {
  445. rowIndex = 0;
  446. category = nextColumnCategory;
  447. while (category) {
  448. lastValue = category;
  449. pivotTable[rowIndex].push(category);
  450. rowIndex++;
  451. rowIndex += this._handleUnbalancedCategory(category, pivotTable, rowIndex);
  452. category = category.subCategory;
  453. }
  454. // if an edge is missing a value for a nested row, we add the value of the parent
  455. for (j = rowIndex; j < colEdgeNestingLevel; j++) {
  456. pivotTable[j].push(lastValue);
  457. }
  458. }
  459. };
  460. Pivot.prototype._addRowEdgeCategoriesAndFactCells = function _addRowEdgeCategoriesAndFactCells(pivotTable) {
  461. var rowEdgeIterator = new CrosstabEdgeIterator(this.rowEdge);
  462. var nextRowCategory = void 0,
  463. pivotTableRow = void 0,
  464. category = void 0,
  465. lastValue = void 0,
  466. index = void 0;
  467. while (nextRowCategory = rowEdgeIterator.next()) {
  468. pivotTableRow = [];
  469. pivotTable.push(pivotTableRow);
  470. category = nextRowCategory;
  471. index = 0;
  472. while (category) {
  473. lastValue = category;
  474. pivotTableRow.push(category);
  475. index += this._handleUnbalancedCategory(category, pivotTableRow);
  476. category = category.subCategory;
  477. index++;
  478. }
  479. // if an edge is missing a value for a nested row, we add the value of the parent
  480. for (var i = index; i < this.rowEdgeNestingLevel; i++) {
  481. if (lastValue) {
  482. pivotTableRow.push(lastValue);
  483. nextRowCategory.descendant_or_self.push(lastValue.value.value ? lastValue.value.value : lastValue.value);
  484. }
  485. }
  486. if (!this.conditionalFormatting) {
  487. this._addFactCellsToPivotTableRow(nextRowCategory, pivotTableRow);
  488. } else {
  489. this._addFactCellsToPivotTableRowNewCF(nextRowCategory, pivotTableRow);
  490. }
  491. }
  492. };
  493. //If this category is an 'unbalanced parent', it will have a span similar to a row or columnspan.
  494. //Fill the edges of the pivotTable for each of missing descendant up to the span.
  495. //Note: for the rowEdge, rowIndex is undefined and pivotTable represents a single row.
  496. Pivot.prototype._handleUnbalancedCategory = function _handleUnbalancedCategory(category, pivotTable) {
  497. var rowIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
  498. if (!category.isSummary && category.value && category.value.span) {
  499. var unbalancedCellDepth = category.value.span - 1;
  500. for (var i = 0; i < unbalancedCellDepth; ++i) {
  501. //Clone the cell, clear the value and mark it as an unbalanced cell.
  502. var unbalancedCell = JSON.parse(JSON.stringify(category));
  503. unbalancedCell.isUnbalanced = true;
  504. delete unbalancedCell.value;
  505. if (rowIndex !== undefined) {
  506. //For the column edge, we need to push data 1 cell to the right for span nested "rows" of the column edge
  507. pivotTable[rowIndex + i].push(unbalancedCell);
  508. } else {
  509. //For the row edge, we need to push cells in a single row, span cells to the right.
  510. pivotTable.push(unbalancedCell);
  511. }
  512. }
  513. return unbalancedCellDepth;
  514. }
  515. return 0;
  516. };
  517. Pivot.prototype._addFactCellsToPivotTableRow = function _addFactCellsToPivotTableRow(rowEdge, pivotTableRow) {
  518. var _this3 = this;
  519. var columnEdgeIterator = new CrosstabEdgeIterator(this.columnEdge);
  520. var maxLevel = this.getNestingLevels().colNestingLevel - 1;
  521. var isOverallSummary = void 0,
  522. key = void 0,
  523. tuple = void 0,
  524. valueObj = void 0,
  525. rawValue = void 0,
  526. value = void 0,
  527. condValue = void 0,
  528. columnEdge = void 0,
  529. condProjected = void 0;
  530. if (this._conditionalMeasureInfo && this._measuresInfo) {
  531. condProjected = this._measuresInfo.find(function (info) {
  532. return info.label === _this3._conditionalMeasureInfo.label;
  533. });
  534. }
  535. while (columnEdge = columnEdgeIterator.next(undefined, maxLevel)) {
  536. isOverallSummary = rowEdge.isOverallSummary || columnEdge.isOverallSummary;
  537. key = this.getKeyFromDataCells(columnEdge.descendant_or_self, rowEdge.descendant_or_self);
  538. tuple = this.getKeyFromDataCells(columnEdge.descendant_or_self_without_measures, rowEdge.descendant_or_self_without_measures);
  539. valueObj = this.valuesMap[key];
  540. if (valueObj) {
  541. rawValue = valueObj.value;
  542. }
  543. // We need distinguish the null data point values from DSS for formatting purpose.
  544. // If missing by default: data should be undefined
  545. // If explicitly from DSS: data becomes null
  546. value = valueObj ? rawValue : null;
  547. condValue = null;
  548. if (rawValue !== undefined && rawValue !== null) {
  549. // populate with the conditional measure value
  550. if (this._conditionalMeasureInfo) {
  551. // conditional format only applies to the projected conditional measure
  552. // if the conditional measure is not projected, then format the entire crosstab
  553. if (valueObj && (this._conditionalMeasureInfo.label === valueObj.label || !condProjected)) {
  554. condValue = {
  555. value: valueObj.conditionalValue,
  556. formatted: Formatter.formatNull(valueObj.conditionalValue, this._conditionalMeasureInfo.formatSpec)
  557. };
  558. }
  559. }
  560. }
  561. pivotTableRow.push({
  562. rowEdge: rowEdge,
  563. columnEdge: columnEdge,
  564. value: value,
  565. rawValue: rawValue,
  566. key: key,
  567. tuple: tuple,
  568. condValue: condValue,
  569. isOverallSummary: isOverallSummary
  570. });
  571. }
  572. };
  573. Pivot.prototype._addFactCellsToPivotTableRowNewCF = function _addFactCellsToPivotTableRowNewCF(rowEdge, pivotTableRow) {
  574. var columnEdgeIterator = new CrosstabEdgeIterator(this.columnEdge);
  575. var maxLevel = this.getNestingLevels().colNestingLevel - 1;
  576. var isOverallSummary = void 0,
  577. key = void 0,
  578. tuple = void 0,
  579. valueObj = void 0,
  580. rawValue = void 0,
  581. value = void 0,
  582. condValue = void 0,
  583. columnEdge = void 0;
  584. while (columnEdge = columnEdgeIterator.next(undefined, maxLevel)) {
  585. isOverallSummary = rowEdge.isOverallSummary || columnEdge.isOverallSummary;
  586. key = this.getKeyFromDataCells(columnEdge.descendant_or_self, rowEdge.descendant_or_self);
  587. tuple = this.getKeyFromDataCells(columnEdge.descendant_or_self_without_measures, rowEdge.descendant_or_self_without_measures);
  588. valueObj = this.valuesMap[key];
  589. if (valueObj) {
  590. rawValue = valueObj.value;
  591. }
  592. // We need distinguish the null data point values from DSS for formatting purpose.
  593. // If missing by default: data should be undefined
  594. // If explicitly from DSS: data becomes null
  595. value = valueObj ? rawValue : null;
  596. condValue = null;
  597. if (rawValue !== undefined && rawValue !== null) {
  598. // populate with the conditional measure value
  599. if (valueObj) {
  600. var conditionalMeasure = this._conditionalMeasureInfoNew.find(function (condMeasure) {
  601. return condMeasure.label === valueObj.conditionalLabel;
  602. });
  603. if (conditionalMeasure) {
  604. condValue = {
  605. value: valueObj.conditionalValue,
  606. formatted: Formatter.formatNull(valueObj.conditionalValue, conditionalMeasure.formatSpec)
  607. };
  608. }
  609. }
  610. }
  611. pivotTableRow.push({
  612. rowEdge: rowEdge,
  613. columnEdge: columnEdge,
  614. value: value,
  615. rawValue: rawValue,
  616. key: key,
  617. tuple: tuple,
  618. condValue: condValue,
  619. isOverallSummary: isOverallSummary
  620. });
  621. }
  622. };
  623. Pivot.prototype.getKeyFromDataCells = function getKeyFromDataCells(aData, bData) {
  624. var aKeys = [];
  625. if (Array.isArray(aData)) {
  626. for (var i = 0; i < aData.length; i++) {
  627. aKeys.push(aData[i].value ? aData[i].value : aData[i]);
  628. }
  629. }
  630. if (Array.isArray(bData)) {
  631. for (var _i = 0; _i < bData.length; _i++) {
  632. aKeys.push(bData[_i].value ? bData[_i].value : bData[_i]);
  633. }
  634. }
  635. return aKeys.toString();
  636. };
  637. Pivot.prototype._addValuesToMap = function _addValuesToMap(dimensions, measureValue, measureInfo, conditionalValue, conditionalValueInfo) {
  638. if (measureValue === undefined) {
  639. // undefined
  640. return;
  641. }
  642. this.valuesMap[this.getKeyFromDataCells(dimensions)] = {
  643. label: measureInfo ? measureInfo.label : null,
  644. value: measureValue,
  645. conditionalLabel: conditionalValueInfo ? conditionalValueInfo.label : null,
  646. conditionalValue: conditionalValue,
  647. getFormatSpec: function getFormatSpec() {
  648. return measureInfo ? measureInfo.formatSpec : null;
  649. }
  650. };
  651. };
  652. Pivot.prototype._addDimensionToEdge = function _addDimensionToEdge(edge, dimensions, nestingLevel, type, measuresInfo, currentLevel) {
  653. currentLevel = currentLevel || 0;
  654. if (dimensions && dimensions.length > 0) {
  655. var memberKey = dimensions[0].value;
  656. if (!edge[memberKey]) {
  657. edge[memberKey] = {
  658. level: currentLevel,
  659. type: type,
  660. value: dimensions[0]
  661. };
  662. if (dimensions[0].isSummary) {
  663. edge[memberKey].isSummary = dimensions[0].isSummary;
  664. }
  665. var formatSpec = null;
  666. if (type === ROW) {
  667. formatSpec = this._getFormatSpec(this._rowInfo[currentLevel]);
  668. } else if (type === COLUMN) {
  669. formatSpec = this._getFormatSpec(this._columnInfo[currentLevel]);
  670. }
  671. if (formatSpec && formatSpec.type === DATE) {
  672. edge[memberKey].formatSpec = formatSpec;
  673. }
  674. }
  675. var remaining = dimensions.slice(1, dimensions.length);
  676. if (remaining.length > 0) {
  677. if (!edge[memberKey].nestedEdges) {
  678. edge[memberKey].nestedEdges = {};
  679. }
  680. this._addDimensionToEdge(edge[memberKey].nestedEdges, remaining, nestingLevel, type, measuresInfo, currentLevel + 1);
  681. } else if (this._columnEdgeHasMeasure && type === COLUMN) {
  682. //if we get here, this means we are at the innermost level of the nesting, so add the measures if there are any
  683. if (!edge[memberKey].nestedEdges) {
  684. edge[memberKey].nestedEdges = {};
  685. edge[memberKey].hasMeasureChildren = true;
  686. }
  687. this._addMeasureToEdge(edge[memberKey].nestedEdges, measuresInfo, type, currentLevel + 1, false /*bIsSummary*/);
  688. }
  689. } else if (this._columnEdgeHasMeasure && type === COLUMN) {
  690. //if we get here, it means that there are no dimensions
  691. this._addMeasureToEdge(edge, measuresInfo, type, currentLevel, false /*bIsSummary*/);
  692. }
  693. };
  694. Pivot.prototype._getFormatSpec = function _getFormatSpec(info) {
  695. return info.formatSpec && info.formatSpec.formatSpec;
  696. };
  697. Pivot.prototype._addMeasureToEdge = function _addMeasureToEdge(edge, measuresInfo, type, currentLevel, bIsSummary, bIsOverallSummary) {
  698. if (measuresInfo) {
  699. var measure = void 0,
  700. measureLabel = void 0;
  701. for (var i = 0; i < measuresInfo.length; i++) {
  702. measure = measuresInfo[i];
  703. measureLabel = measure.label;
  704. if (!edge[measureLabel]) {
  705. edge[measureLabel] = {
  706. level: currentLevel,
  707. type: type,
  708. isMeasure: true
  709. };
  710. }
  711. if (bIsSummary) {
  712. edge[measureLabel].isSummary = true;
  713. // make sure the measures in the Summary are not sorted
  714. edge[measureLabel].noSort = true;
  715. if (bIsOverallSummary) {
  716. edge[measureLabel].isOverallSummary = true;
  717. }
  718. }
  719. }
  720. }
  721. };
  722. /**
  723. * Not every data item mapped in slot have data return from query result,
  724. * in a situation such as suppression where all values of a measure were suppressed.
  725. * Verify and return a boolean indicating whether there are data returned for the mapped data item in slot
  726. *
  727. * @param {array} resultDataItemList list of data items returned from query result
  728. * @param {object} dataItemFromSlot mapped slot data item to verify
  729. * @return {boolean} true if the dataItem has data returned for turn false
  730. */
  731. Pivot.prototype._measureItemHasData = function _measureItemHasData() {
  732. var resultDataItemList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  733. var dataItemFromSlot = arguments[1];
  734. if (!dataItemFromSlot) {
  735. throw new Error('Invalid dataItem provided');
  736. }
  737. return !!resultDataItemList.find(function (item) {
  738. return item.getId() === dataItemFromSlot.getId();
  739. });
  740. };
  741. return Pivot;
  742. }();
  743. return Pivot;
  744. });
  745. //# sourceMappingURL=Pivot.js.map