EdgeIterator.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2016, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define([], function () {
  9. 'use strict';
  10. return function () {
  11. function EdgeIterator(edgeInfo) {
  12. var leafnode_index_no_summary = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  13. _classCallCheck(this, EdgeIterator);
  14. this.edge = edgeInfo.edge;
  15. this.keys = edgeInfo.sortedKeys;
  16. this.currentKeyIndex = 0;
  17. this.subIterator = null;
  18. this.numberOfLeafNodes = 0;
  19. //this is the index of the leaf nodes over the entire edge not including the summary
  20. this.leafnode_index_no_summary = leafnode_index_no_summary;
  21. }
  22. EdgeIterator.prototype.next = function next(ancestors, maxLevel) {
  23. var next = null;
  24. if (this._previous && !this._previous.isSummary) {
  25. ++this.leafnode_index_no_summary;
  26. }
  27. if (this.currentKeyIndex < this.keys.length) {
  28. //an array of descendants including self
  29. var _self = [];
  30. var key = this.keys[this.currentKeyIndex];
  31. var currentEdge = this.edge[key];
  32. // Summary key doesn't have value.
  33. var edgeItem = currentEdge.value !== undefined ? currentEdge.value : key;
  34. _self.push(edgeItem);
  35. if (!this.subIterator && currentEdge.nestedEdges) {
  36. this._first = null;
  37. this.ancestor_or_self = !ancestors || ancestors.length === 0 ? [edgeItem] : ancestors.concat(edgeItem);
  38. this.numberOfLeafNodes = 0;
  39. this.subIterator = new this.EdgeIterator(currentEdge.nestedEdges, this.leafnode_index_no_summary);
  40. }
  41. if (this.subIterator) {
  42. var subCategory = this.subIterator.next(this.ancestor_or_self, maxLevel);
  43. if (subCategory) {
  44. if (subCategory.hasLeafNode || subCategory.isLeafNode) {
  45. //this is number of leaf nodes so far in the traversal
  46. this.numberOfLeafNodes++;
  47. }
  48. next = this.createNodeInfo(_self, edgeItem, currentEdge, subCategory, maxLevel);
  49. } else {
  50. // Move to the next index
  51. this.currentKeyIndex++;
  52. this.subIterator = null;
  53. this._first.noOfLeafNodes = this._previous.index + 1;
  54. this._first.first = true;
  55. next = this.next(ancestors, maxLevel);
  56. }
  57. } else {
  58. //leaf node info
  59. next = this.createLeafNodeInfo(_self, edgeItem, currentEdge, ancestors, maxLevel);
  60. this.currentKeyIndex++;
  61. }
  62. }
  63. if (!this._first) {
  64. this._first = next;
  65. }
  66. this._previous = next;
  67. return next;
  68. };
  69. EdgeIterator.prototype.createLeafNodeInfo = function createLeafNodeInfo() /* descendant_or_self, value, currentEdge, ancestors, maxLevel */{
  70. throw new Error('EdgeIterator.createLeafNodeInfo is not implemented');
  71. };
  72. EdgeIterator.prototype.createNodeInfo = function createNodeInfo() /* descendant_or_self, value, currentEdge, childNode, maxLevel */{
  73. throw new Error('EdgeIterator.createNodeInfo is not implemented');
  74. };
  75. /**
  76. * This function rollup the 'Summary' of a descendants if a descendants are summaries and the current nodeInfo itself is also a summary
  77. *
  78. * @param {object} nodeInfo = the node to rollup summary of descendents
  79. * @param {string} value - the current nodeInfo summary value
  80. * @param {number} maxLevel - the maximum nesting level
  81. * @param {object} [childNode] - the immediate children of the nodeInfo
  82. *
  83. * @return {boolean} return true if rollup summaries else return false
  84. */
  85. EdgeIterator.prototype.rollupSummariesOfDescendents = function rollupSummariesOfDescendents(nodeInfo, value, maxLevel, childNode) {
  86. var currentNestLevel = nodeInfo.nestLevel + (childNode ? childNode.descendant_or_self.length : 0);
  87. var rollup = nodeInfo.isSummary && !nodeInfo.isMeasure && Number.isInteger(maxLevel) && currentNestLevel < maxLevel; //there are gaps between currentNestLevel && maxLevel
  88. if (rollup) {
  89. var iterateMaxLevel = maxLevel - (childNode ? childNode.descendant_or_self.length : 0);
  90. for (var i = nodeInfo.nestLevel; i < iterateMaxLevel; i++) {
  91. nodeInfo.descendant_or_self.push(value);
  92. }
  93. }
  94. return rollup;
  95. };
  96. return EdgeIterator;
  97. }();
  98. });
  99. //# sourceMappingURL=EdgeIterator.js.map