SharedSlotVisQueryModifier.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore', '../../../lib/@waca/dashboard-common/dist/core/APIFactory', './api/QueryModifierAPI', './DataQueryUtils'], function (_, APIFactory, QueryModifierAPI) {
  9. var SharedSlotVisQueryModifier = function () {
  10. function SharedSlotVisQueryModifier(_ref) {
  11. var content = _ref.content,
  12. features = _ref.features;
  13. _classCallCheck(this, SharedSlotVisQueryModifier);
  14. this.content = content;
  15. features.DataQueryExecution.registerQueryModifier(this.getAPI());
  16. }
  17. SharedSlotVisQueryModifier.prototype.getAPI = function getAPI() {
  18. if (!this.api) {
  19. this.api = APIFactory.createAPI(this, [QueryModifierAPI]);
  20. }
  21. return this.api;
  22. };
  23. SharedSlotVisQueryModifier.prototype.getType = function getType() {
  24. return 'main';
  25. };
  26. SharedSlotVisQueryModifier.prototype.modifyQuerySpecList = function modifyQuerySpecList(querySpecList) {
  27. var visualization = this.content.getFeature('Visualization');
  28. var mappedSlots = visualization.getSlots().getMappedSlotList();
  29. if (this._hasSharedSlot(mappedSlots)) {
  30. this._addExtraDataItems(querySpecList, mappedSlots);
  31. }
  32. return querySpecList;
  33. };
  34. SharedSlotVisQueryModifier.prototype._hasSharedSlot = function _hasSharedSlot(mappedSlots) {
  35. var sharedSlots = mappedSlots.filter(function (slot) {
  36. return slot.getDefinition().getDatasetIdList().length > 1;
  37. });
  38. return sharedSlots.length > 0;
  39. };
  40. SharedSlotVisQueryModifier.prototype._getDataItemMap = function _getDataItemMap(querySpecList) {
  41. var result = {};
  42. querySpecList.forEach(function (querySpec) {
  43. querySpec.spec.dataItems.forEach(function (dataItem) {
  44. if (!result[dataItem.id]) {
  45. result[dataItem.id] = dataItem;
  46. }
  47. });
  48. });
  49. return result;
  50. };
  51. SharedSlotVisQueryModifier.prototype._getNestIdList = function _getNestIdList(querySpec) {
  52. var result = [];
  53. querySpec.dataItems.forEach(function (dataItem) {
  54. if (dataItem.nest) {
  55. result.push.apply(result, dataItem.nest);
  56. }
  57. });
  58. return result;
  59. };
  60. /**
  61. * Get the extra data items from other datasets
  62. * We only need to copy data items with type fact
  63. *
  64. * @param {*} mappedSlots
  65. * @param {*} datasetId
  66. * @returns
  67. * @memberof SharedSlotVisQueryModifier
  68. */
  69. SharedSlotVisQueryModifier.prototype._getExtraDataItemIdList = function _getExtraDataItemIdList(querySpec, mappedSlots) {
  70. var extraDataItemIdList = [];
  71. var projections = querySpec.spec.projections;
  72. var nestIdList = this._getNestIdList(querySpec.spec);
  73. var excludedIdList = [].concat(projections, nestIdList);
  74. mappedSlots.forEach(function (slot) {
  75. slot.getDataItemList().forEach(function (dataItem) {
  76. var id = dataItem.getId();
  77. if (dataItem.getType() === 'fact' && !(excludedIdList.indexOf(id) !== -1)) {
  78. extraDataItemIdList.push(id);
  79. }
  80. });
  81. });
  82. return extraDataItemIdList;
  83. };
  84. SharedSlotVisQueryModifier.prototype._addExtraDataItems = function _addExtraDataItems(querySpecList, mappedSlots) {
  85. var _this = this;
  86. var dataItemMap = this._getDataItemMap(querySpecList);
  87. querySpecList.forEach(function (querySpec) {
  88. var extraDataItems = [];
  89. var extraDataItemIdList = _this._getExtraDataItemIdList(querySpec, mappedSlots);
  90. extraDataItemIdList.forEach(function (id) {
  91. var dataItem = dataItemMap[id];
  92. if (_this._isValidDataItem(dataItem)) {
  93. /*
  94. When sorting by a fact in one of the Bar/line queries, we need to introduce that
  95. fact item and sort spec into the other query as a context item.
  96. The query layer will use that item to produce a sorted result.
  97. */
  98. extraDataItems.push(dataItem);
  99. }
  100. });
  101. if (extraDataItems.length) {
  102. var _querySpec$spec$dataI;
  103. (_querySpec$spec$dataI = querySpec.spec.dataItems).push.apply(_querySpec$spec$dataI, extraDataItems);
  104. }
  105. _this._modifyMultiEdgeSort(querySpec);
  106. });
  107. };
  108. /**
  109. *
  110. * Return true is the data item has sorting spec in selection
  111. * @memberof SharedSlotVisQueryModifier
  112. */
  113. SharedSlotVisQueryModifier.prototype._isValidDataItem = function _isValidDataItem(dataItem) {
  114. if (!dataItem.selection) {
  115. return false;
  116. }
  117. var sortingSpec = dataItem.selection.filter(function (value) {
  118. return Object.keys(value).indexOf('sort') !== -1;
  119. });
  120. return sortingSpec.length > 0;
  121. };
  122. /**
  123. *
  124. * Turn off the multiEdgeSort(There is only one edge which is the shared slot)
  125. * @param {*} querySpec query spec object
  126. * @memberof SharedSlotVisQueryModifier
  127. */
  128. SharedSlotVisQueryModifier.prototype._modifyMultiEdgeSort = function _modifyMultiEdgeSort(querySpec) {
  129. if (querySpec.spec.queryHints) {
  130. querySpec.spec.queryHints.multiEdgeSort = false;
  131. }
  132. };
  133. return SharedSlotVisQueryModifier;
  134. }();
  135. return SharedSlotVisQueryModifier;
  136. });
  137. //# sourceMappingURL=SharedSlotVisQueryModifier.js.map