CrosstabTable_VCF_Upgrade.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. /*
  7. *+------------------------------------------------------------------------+
  8. *| Licensed Materials - Property of IBM
  9. *| IBM Cognos Products: BI Dashboard
  10. *| (C) Copyright IBM Corp. 2020
  11. *|
  12. *| US Government Users Restricted Rights - Use, duplication or disclosure
  13. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  14. *+------------------------------------------------------------------------+
  15. */
  16. define(['underscore', '../../../lib/@waca/upgrades/UpgradeBase', './utils/WidgetUpgradeUtils', './utils/VCF_RuleConstants', 'dashboard-analytics/dataSources/services/DataSourcesService', '../../../lib/@waca/dashboard-common/dist/core/UniqueHashIdBuilder', '../../../app/nls/StringResources', '../../../app/util/DeepClone'], function (_, UpgradeBase, WidgetUpgradeUtils, VCF_RuleConstants, DataSourcesService, UniqueHashIdBuilder, StringResources) {
  17. /**
  18. * Upgrade Crosstab/Table widgets to the new version based on the Crosstab/Table ConditionalFormatting feature
  19. **/
  20. var defaultPaletteLength = 5;
  21. var CrosstabTable_VCF_Upgrade = function (_UpgradeBase) {
  22. _inherits(CrosstabTable_VCF_Upgrade, _UpgradeBase);
  23. function CrosstabTable_VCF_Upgrade() {
  24. _classCallCheck(this, CrosstabTable_VCF_Upgrade);
  25. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  26. _this.VERSION = 1807;
  27. return _this;
  28. }
  29. /**
  30. * Perform upgrade
  31. *
  32. * @param {object} spec - spec to perform upgrade on
  33. *
  34. * @return {Promise} Promise to be resolved when upgrade performed
  35. */
  36. CrosstabTable_VCF_Upgrade.prototype.up = function up(spec) {
  37. var _this2 = this;
  38. return Promise.resolve().then(function () {
  39. if (!WidgetUpgradeUtils.specHasWidgets(spec)) {
  40. return spec;
  41. }
  42. var dataSourcesClass = _this2._DataSourcesServices || DataSourcesService;
  43. _this2.dataSourcesService = new dataSourcesClass({
  44. features: {
  45. API: _this2.data.dashboardApi,
  46. Logger: _this2.data.logger
  47. }
  48. });
  49. _this2.dataSourcesService.initializeSourcesCollection(spec);
  50. // Upgrade all the Crosstab widgets
  51. var upgradeVisIds = ['crosstab', 'JQGrid'];
  52. var upgradeSlotNames = ['values', 'grid_cols'];
  53. var promises = [];
  54. var unsupportedCFs = [];
  55. _.each(spec.widgets, function (widget) {
  56. if (widget && widget.visId && upgradeVisIds.indexOf(widget.visId) !== -1) {
  57. var dataItems = widget.data && widget.data.dataViews && widget.data.dataViews.length ? widget.data.dataViews[0].dataItems : [];
  58. // Find color heat dataItem
  59. var heatSlot = _this2._getSlot(widget.slotmapping.slots, ['heat']);
  60. var heatId = heatSlot && heatSlot.dataItems[0];
  61. var heatDataItem = dataItems.find(function (dataItem) {
  62. return dataItem.id === heatId;
  63. });
  64. if (heatDataItem) {
  65. var widgetUISpecs = _this2._getObjects(spec.layout, 'id', widget.id);
  66. var widgetUISpec = widgetUISpecs.length > 0 ? widgetUISpecs[0] : undefined;
  67. var conditionalFormats = [];
  68. if (widgetUISpec) {
  69. widgetUISpec.content = widgetUISpec.content || {};
  70. widgetUISpec.content.conditionalFormatting = {
  71. conditionalFormats: conditionalFormats
  72. };
  73. }
  74. var useNewHeatId = false;
  75. var getDataItemTypes = void 0;
  76. var upgradeSlotContainsHeat = false;
  77. var upgradeSlotDataItems = [];
  78. if (widget.visId === 'JQGrid') {
  79. getDataItemTypes = _this2._getDataItemTypes(widget);
  80. } else {
  81. getDataItemTypes = Promise.resolve();
  82. }
  83. promises.push(getDataItemTypes.then(function (dataItemTypes) {
  84. var upgradeSlot = _this2._getSlot(widget.slotmapping.slots, upgradeSlotNames);
  85. if (upgradeSlot) {
  86. upgradeSlot.dataItems.forEach(function (id) {
  87. dataItems.forEach(function (dataItem) {
  88. if (dataItem.id == id) {
  89. upgradeSlotDataItems.push(dataItem);
  90. if (dataItem.itemId === heatDataItem.itemId) {
  91. upgradeSlotContainsHeat = true;
  92. }
  93. }
  94. });
  95. });
  96. }
  97. var firstFactInUpgradeSlotIdx = dataItemTypes && upgradeSlotDataItems.findIndex(function (dataItem) {
  98. return dataItemTypes[dataItem.itemId] === 'fact';
  99. });
  100. var upgradeCFPromises = [];
  101. upgradeSlotDataItems.forEach(function (dataItem, idx) {
  102. if (useNewHeatId) {
  103. //We need a 1-1 relationship
  104. heatDataItem = _this2._cloneDataItemWithNewId(heatDataItem);
  105. heatSlot.dataItems.push(heatDataItem.id);
  106. dataItems.push(heatDataItem);
  107. }
  108. useNewHeatId = true;
  109. var currentType = dataItemTypes && dataItemTypes[dataItem.itemId];
  110. var upgradeCFPromise = Promise.resolve();
  111. if (widget.visId === 'crosstab' || currentType === 'fact') {
  112. upgradeCFPromise = _this2._createConditionFormat(widget, conditionalFormats, dataItem, heatDataItem, upgradeSlotContainsHeat);
  113. } else if (upgradeSlotContainsHeat) {
  114. if (dataItem.itemId === heatDataItem.itemId) {
  115. unsupportedCFs.push({
  116. widget: widget.visId,
  117. dataItem: dataItem
  118. });
  119. }
  120. } else {
  121. var nextIdx = idx + 1;
  122. if (firstFactInUpgradeSlotIdx === -1 || firstFactInUpgradeSlotIdx === nextIdx) {
  123. //Either there are no fact columns or It's a non grouped attribute. (Groups aren't colored)
  124. unsupportedCFs.push({
  125. widget: widget.id,
  126. dataItem: dataItem
  127. });
  128. }
  129. }
  130. upgradeCFPromises.push(upgradeCFPromise);
  131. });
  132. return Promise.all(upgradeCFPromises);
  133. }));
  134. }
  135. }
  136. });
  137. return Promise.all(promises).then(function () {
  138. if (unsupportedCFs.length) {
  139. var msg = StringResources.get('unsupportedConditionalFormattingEncountered');
  140. var logger = _this2.data.logger;
  141. if (logger) {
  142. logger.warn(msg, unsupportedCFs);
  143. }
  144. _this2._showUnsupportedCFMessage(msg);
  145. }
  146. return spec;
  147. });
  148. });
  149. };
  150. CrosstabTable_VCF_Upgrade.prototype._showUnsupportedCFMessage = function _showUnsupportedCFMessage(msg) {
  151. this.data.dashboardApi.showToast(msg, {
  152. type: 'warning',
  153. preventDuplicates: true
  154. });
  155. };
  156. CrosstabTable_VCF_Upgrade.prototype._createConditionFormat = function _createConditionFormat(widget, conditionalFormats, dataItem, heatDataItem, upgradeSlotContainsHeat) {
  157. var conditionalFormat = {
  158. 'dataItemId': dataItem.itemId,
  159. 'paletteId': 'condColorPalette0',
  160. 'heatId': heatDataItem.id,
  161. 'scale': VCF_RuleConstants.RULE_SCALES.NUMERIC,
  162. 'rules': [],
  163. 'backgroundOpacity': 100
  164. };
  165. var colors = widget.conditions && widget.conditions.palette ? widget.conditions.palette.colors : [];
  166. var promise = void 0;
  167. if (!upgradeSlotContainsHeat || heatDataItem.itemId === dataItem.itemId) {
  168. promise = this._updateTargetConditionalFormat(widget, colors, conditionalFormat);
  169. } else {
  170. promise = Promise.resolve();
  171. }
  172. return promise.then(function () {
  173. conditionalFormats.push(conditionalFormat);
  174. });
  175. };
  176. CrosstabTable_VCF_Upgrade.prototype._cloneDataItemWithNewId = function _cloneDataItemWithNewId(dataItem) {
  177. var clonedItem = JSON.parse(JSON.stringify(dataItem));
  178. clonedItem.id = UniqueHashIdBuilder.createUniqueHashId(clonedItem.id + _.uniqueId(), []);
  179. return clonedItem;
  180. };
  181. // Downgrade is not available in CA
  182. CrosstabTable_VCF_Upgrade.prototype.down = function down(spec) {
  183. return Promise.resolve(spec);
  184. };
  185. CrosstabTable_VCF_Upgrade.prototype._getObjects = function _getObjects(obj, key, val) {
  186. var objects = [];
  187. for (var i in obj) {
  188. if (!obj.hasOwnProperty(i)) continue;
  189. if (_typeof(obj[i]) == 'object') {
  190. objects = objects.concat(this._getObjects(obj[i], key, val));
  191. } else if (i == key && obj[i] == val) {
  192. objects.push(obj);
  193. }
  194. }
  195. return objects;
  196. };
  197. CrosstabTable_VCF_Upgrade.prototype._getSlot = function _getSlot(slots, slotNamesFilter) {
  198. return _.find(slots, function (slot) {
  199. return slotNamesFilter.indexOf(slot.name) !== -1;
  200. });
  201. };
  202. CrosstabTable_VCF_Upgrade.prototype._updateTargetConditionalFormat = function _updateTargetConditionalFormat(widget, colors, conditionalFormat) {
  203. var _this3 = this;
  204. if (_.isEmpty(colors)) {
  205. return Promise.resolve();
  206. }
  207. var condColorPaletteProperty = this._getObjects(widget.properties, 'id', 'condColorPalette');
  208. if (condColorPaletteProperty && condColorPaletteProperty.length) {
  209. conditionalFormat.paletteId = condColorPaletteProperty[0].value;
  210. }
  211. var condColorOrderProperty = this._getObjects(widget.properties, 'id', 'condColorOrder');
  212. var isPaletteReversed = condColorOrderProperty && condColorOrderProperty.length ? condColorOrderProperty[0].value === 'DarkerForLowerValue' : false;
  213. var rule = null;
  214. var rangeRuleTemplate = {
  215. 'ruleType': VCF_RuleConstants.RULE_TYPES.RANGE,
  216. 'startCondition': {
  217. 'type': VCF_RuleConstants.RULE_OPERATORS.greaterThanOrEqualTo,
  218. 'value': undefined
  219. },
  220. 'ruleStyle': {
  221. 'fillColor': '',
  222. 'textColor': '',
  223. 'shape': ''
  224. },
  225. 'operator': VCF_RuleConstants.RangeOperators.and,
  226. 'endCondition': {
  227. 'type': VCF_RuleConstants.RULE_OPERATORS.lessThan,
  228. 'value': undefined
  229. }
  230. };
  231. return this.data.dashboardApi.getFeature('Palette').getPalette(conditionalFormat.paletteId).then(function (condColorPalette) {
  232. var paletteColors = condColorPalette.content.fills;
  233. return paletteColors.length;
  234. }, function () {
  235. return defaultPaletteLength;
  236. }).then(function (paletteLength) {
  237. var rangesLength = colors.length - 1;
  238. var colorIndexMap = _this3._buildIndexMap(paletteLength, rangesLength);
  239. if (isPaletteReversed) {
  240. colorIndexMap.reverse();
  241. }
  242. var defaultFillColors = _.range(paletteLength).map(function (index) {
  243. return 'color' + index;
  244. });
  245. for (var i = 0; i < rangesLength; i++) {
  246. rule = _.deepClone(rangeRuleTemplate);
  247. rule.startCondition.value = colors[i].value;
  248. rule.endCondition.value = colors[i + 1].value;
  249. if (i < defaultFillColors.length) {
  250. rule.ruleStyle.fillColor = defaultFillColors[colorIndexMap[i]];
  251. } else {
  252. rule.ruleStyle.fillColor = defaultFillColors[colorIndexMap[i % defaultFillColors.length]];
  253. }
  254. conditionalFormat.rules.push(rule);
  255. }
  256. });
  257. };
  258. /**
  259. * @description this method is a copy of _buildIndexMap from js/features/widget/conditionalFormatting/api/impl/DeprecatedConditionalFormatting.js
  260. * @param {int} paletteLength the length of a palette
  261. * @param {*} rangesLength the number of ranges
  262. */
  263. CrosstabTable_VCF_Upgrade.prototype._buildIndexMap = function _buildIndexMap(paletteLength, rangesLength) {
  264. if (paletteLength <= rangesLength) {
  265. return _.range(paletteLength);
  266. }
  267. if (paletteLength === defaultPaletteLength) {
  268. var fiveColorPalleteMaps = [[4], // one color
  269. [0, 4], // two colors
  270. [0, 2, 4], // three colors
  271. [0, 1, 2, 4], // four colors
  272. [0, 1, 2, 3, 4] // five colors
  273. ];
  274. var colorIndexMap = fiveColorPalleteMaps[rangesLength - 1];
  275. return colorIndexMap;
  276. }
  277. if (rangesLength === 1) {
  278. return [Math.floor(paletteLength / 2)];
  279. }
  280. var step = (paletteLength - 1) / rangesLength;
  281. var indexMap = [0];
  282. for (var i = 2; i < rangesLength; i++) {
  283. indexMap.push(Math.floor(step * i));
  284. }
  285. indexMap.push(paletteLength - 1);
  286. return indexMap;
  287. };
  288. CrosstabTable_VCF_Upgrade.prototype._getDataItemTypes = function _getDataItemTypes(widget) {
  289. var dataTypes = {};
  290. var dataViews = widget.data && widget.data.dataViews;
  291. var modelRef = dataViews && dataViews[0] ? dataViews[0].modelRef : null;
  292. var promise = !modelRef || !dataViews ? Promise.resolve(null) : this._getModule(modelRef);
  293. return promise.then(function (module) {
  294. if (module) {
  295. _.each(dataViews, function (dataView) {
  296. var dataItems = dataView.dataItems;
  297. if (dataItems) {
  298. _.each(dataItems, function (dataItem) {
  299. // get metadata with column id
  300. var mdc = module.getMetadataColumn(dataItem.itemId);
  301. dataTypes[dataItem.itemId] = mdc.getType();
  302. });
  303. }
  304. });
  305. }
  306. return dataTypes;
  307. });
  308. };
  309. CrosstabTable_VCF_Upgrade.prototype._getModule = function _getModule(modelRef) {
  310. return this.dataSourcesService.getModule(modelRef);
  311. };
  312. return CrosstabTable_VCF_Upgrade;
  313. }(UpgradeBase);
  314. return new CrosstabTable_VCF_Upgrade();
  315. });
  316. //# sourceMappingURL=CrosstabTable_VCF_Upgrade.js.map