KPI_VCF_Upgrade.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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', '../../../app/util/DeepClone'], function (_, UpgradeBase, WidgetUpgradeUtils, VCF_RuleConstants) {
  17. /**
  18. * Upgrade kpi widgets to the new version based on the KPI widget ConbditionalFormatting
  19. **/
  20. var DefaultPaletteLength = 5;
  21. var KPIVisId = 'kpi';
  22. var SummaryVisId = 'summary';
  23. var rangeRuleTemplate = {
  24. 'ruleType': VCF_RuleConstants.RULE_TYPES.RANGE,
  25. 'startCondition': {
  26. 'type': VCF_RuleConstants.RULE_OPERATORS.greaterThanOrEqualTo,
  27. 'value': undefined
  28. },
  29. 'endCondition': {
  30. 'type': VCF_RuleConstants.RULE_OPERATORS.lessThan,
  31. 'value': undefined
  32. },
  33. 'ruleStyle': {
  34. 'textColor': undefined,
  35. 'shape': undefined
  36. },
  37. 'operator': VCF_RuleConstants.RangeOperators.and
  38. };
  39. var minThresholdRuleTemplate = {
  40. 'ruleType': VCF_RuleConstants.RULE_TYPES.THRESHOLD,
  41. 'startCondition': {
  42. 'type': VCF_RuleConstants.RULE_OPERATORS.lessThan,
  43. 'value': undefined
  44. },
  45. 'ruleStyle': {
  46. 'textColor': 'color4',
  47. 'shape': undefined
  48. },
  49. 'operator': VCF_RuleConstants.RangeOperators.none
  50. };
  51. var maxThresholdRuleTemplate = {
  52. 'ruleType': VCF_RuleConstants.RULE_TYPES.THRESHOLD,
  53. 'startCondition': {
  54. 'type': VCF_RuleConstants.RULE_OPERATORS.greaterThanOrEqualTo,
  55. 'value': undefined
  56. },
  57. 'ruleStyle': {
  58. 'textColor': 'color0',
  59. 'shape': undefined
  60. },
  61. 'operator': VCF_RuleConstants.RangeOperators.none
  62. };
  63. var equalThresholdRuleTemplate = {
  64. 'ruleType': VCF_RuleConstants.RULE_TYPES.THRESHOLD,
  65. 'startCondition': {
  66. 'type': VCF_RuleConstants.RULE_OPERATORS.equalTo,
  67. 'value': undefined
  68. },
  69. 'ruleStyle': {
  70. 'textColor': 'color2',
  71. 'shape': undefined
  72. },
  73. 'operator': VCF_RuleConstants.RangeOperators.none
  74. };
  75. var KPI_VCF_Upgrade = function (_UpgradeBase) {
  76. _inherits(KPI_VCF_Upgrade, _UpgradeBase);
  77. function KPI_VCF_Upgrade() {
  78. _classCallCheck(this, KPI_VCF_Upgrade);
  79. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  80. _this.VERSION = 1805;
  81. return _this;
  82. }
  83. /**
  84. * Perform upgrade
  85. *
  86. * @param {object} spec - spec to perform upgrade on
  87. *
  88. * @return {Promise} Promise to be resolved when upgrade performed
  89. */
  90. KPI_VCF_Upgrade.prototype.up = function up(spec) {
  91. var _this2 = this;
  92. if (!WidgetUpgradeUtils.specHasWidgets(spec)) {
  93. return Promise.resolve(spec);
  94. }
  95. // Upgrade all the KPI widgets
  96. var promisesOfUpgrading = [];
  97. Object.values(spec.widgets).forEach(function (widgetModel) {
  98. if (widgetModel && widgetModel.visId) {
  99. var widgetVisId = widgetModel.visId;
  100. var widgetUISpecs = _this2._getObjects(spec.layout, 'id', widgetModel.id);
  101. var widgetUISpec = widgetUISpecs.length > 0 ? widgetUISpecs[0] : undefined;
  102. if (widgetVisId === KPIVisId && widgetUISpec) {
  103. var legacyCFSpec = widgetUISpec.content ? widgetUISpec.content.conditionalFormatting : undefined;
  104. var promise = _this2._getTargetConditionalFormat(widgetModel, legacyCFSpec).then(function (newCFSpec) {
  105. if (newCFSpec) {
  106. if (legacyCFSpec && legacyCFSpec.conditionalFormats) {
  107. legacyCFSpec.conditionalFormats.push(newCFSpec);
  108. } else {
  109. widgetUISpec.content = {
  110. conditionalFormatting: {
  111. conditionalFormats: [newCFSpec]
  112. }
  113. };
  114. }
  115. }
  116. });
  117. promisesOfUpgrading.push(promise);
  118. } else if (widgetVisId === SummaryVisId && widgetUISpec) {
  119. if (widgetUISpec.content.conditionalFormatting) {
  120. widgetUISpec.content.conditionalFormatting = undefined;
  121. }
  122. }
  123. }
  124. });
  125. return Promise.all(promisesOfUpgrading).then(function () {
  126. return spec;
  127. });
  128. };
  129. // Downgrade is not available in CA
  130. KPI_VCF_Upgrade.prototype.down = function down(spec) {
  131. return Promise.resolve(spec);
  132. };
  133. KPI_VCF_Upgrade.prototype._getObjects = function _getObjects(obj, key, val) {
  134. var objects = [];
  135. for (var i in obj) {
  136. if (!obj.hasOwnProperty(i)) continue;
  137. if (_typeof(obj[i]) == 'object') {
  138. objects = objects.concat(this._getObjects(obj[i], key, val));
  139. } else if (i == key && obj[i] == val) {
  140. objects.push(obj);
  141. }
  142. }
  143. return objects;
  144. };
  145. KPI_VCF_Upgrade.prototype._getSlotName = function _getSlotName(slots, mappingId) {
  146. var slotName = '';
  147. var foundSlot = _.find(slots, function (slot) {
  148. return slot.dataItems[0] === mappingId;
  149. });
  150. if (foundSlot) {
  151. slotName = foundSlot.name;
  152. }
  153. return slotName;
  154. };
  155. KPI_VCF_Upgrade.prototype._getTargetConditionalFormat = function _getTargetConditionalFormat(widgetModel, legacyCFSpec) {
  156. var _this3 = this;
  157. var resultPromise = Promise.resolve();
  158. if (legacyCFSpec) {
  159. // Default conditionalFormat
  160. var newCFSpec = {
  161. 'dataItemId': undefined,
  162. 'paletteId': undefined,
  163. 'backgroundOpacity': 0.5,
  164. 'scale': undefined,
  165. 'rules': []
  166. };
  167. var dataItems = [];
  168. if (widgetModel.data && widgetModel.data.dataViews && widgetModel.data.dataViews.length > 0) {
  169. widgetModel.data.dataViews.forEach(function (dataView) {
  170. dataItems.push.apply(dataItems, dataView.dataItems);
  171. });
  172. }
  173. dataItems.forEach(function (dataItem) {
  174. var slotName = _this3._getSlotName(widgetModel.slotmapping.slots, dataItem.id);
  175. if (slotName === 'actual') {
  176. newCFSpec.dataItemId = dataItem.itemId;
  177. } else if (slotName === 'goal') {
  178. newCFSpec.colorBy = dataItem.itemId;
  179. }
  180. });
  181. newCFSpec.paletteId = legacyCFSpec.paletteId;
  182. newCFSpec.scale = legacyCFSpec.rangeScale;
  183. if (legacyCFSpec.enabled) {
  184. var paletteReversed = !legacyCFSpec.paletteReversed; // the value of `paletteReversed` in the legacy is just the opposite of the actual UI of the product. So, flip it to get the correct value.
  185. var ranges = legacyCFSpec.ranges;
  186. var rangesLength = ranges ? ranges.length : 0;
  187. var rule = null;
  188. resultPromise = this.data.dashboardApi.getFeature('Palette').getPalette(newCFSpec.paletteId).then(function (paletteInfo) {
  189. // we'll have palette info when this palette is a custom palette and existing on this server
  190. return paletteInfo.content.fills.length;
  191. }, function () {
  192. return DefaultPaletteLength;
  193. }).then(function (paletteLength) {
  194. var colorIndexMap = _this3._buildIndexMap(paletteLength, rangesLength);
  195. if (paletteReversed) {
  196. colorIndexMap.reverse();
  197. }
  198. var defaultFillColors = _.range(paletteLength).map(function (index) {
  199. return 'color' + index;
  200. });
  201. for (var i = 0; i < rangesLength; i++) {
  202. var legacyRange = ranges[i];
  203. if (legacyRange.startValue === null) {
  204. rule = _.deepClone(minThresholdRuleTemplate);
  205. rule.startCondition.value = legacyRange.endValue;
  206. } else if (legacyRange.endValue === null) {
  207. rule = _.deepClone(maxThresholdRuleTemplate);
  208. rule.startCondition.value = legacyRange.startValue;
  209. } else if (legacyRange.startValue === legacyRange.endValue) {
  210. rule = _.deepClone(equalThresholdRuleTemplate);
  211. rule.startCondition.value = legacyRange.startValue;
  212. } else {
  213. rule = _.deepClone(rangeRuleTemplate);
  214. rule.startCondition.value = legacyRange.startValue;
  215. rule.endCondition.value = legacyRange.endValue;
  216. }
  217. if (legacyRange.shape) {
  218. rule.ruleStyle.shape = legacyRange.shape;
  219. }
  220. if (legacyRange.userSetColorId) {
  221. rule.ruleStyle.textColor = legacyRange.userSetColorId;
  222. } else if (legacyRange.defaultColorId) {
  223. rule.ruleStyle.textColor = legacyRange.defaultColorId;
  224. } else {
  225. rule.ruleStyle.textColor = defaultFillColors[colorIndexMap[i]];
  226. }
  227. var systemColorRegex = /^color[0-9]+/;
  228. if (systemColorRegex.test(rule.ruleStyle.textColor) && !(defaultFillColors.indexOf(rule.ruleStyle.textColor) !== -1)) {
  229. rule.ruleStyle.textColor = '';
  230. }
  231. newCFSpec.rules.push(rule);
  232. }
  233. return newCFSpec;
  234. });
  235. } else {
  236. return Promise.resolve(newCFSpec);
  237. }
  238. }
  239. return resultPromise;
  240. };
  241. /**
  242. * @description this method is a copy of _buildIndexMap from js/features/widget/conditionalFormatting/api/impl/DeprecatedConditionalFormatting.js
  243. * @param {int} paletteLength the length of a palette
  244. * @param {*} rangesLength the number of ranges
  245. */
  246. KPI_VCF_Upgrade.prototype._buildIndexMap = function _buildIndexMap(paletteLength, rangesLength) {
  247. if (paletteLength <= rangesLength) {
  248. return _.range(paletteLength);
  249. }
  250. if (paletteLength === DefaultPaletteLength) {
  251. var fiveColorPalleteMaps = [[4], // one color
  252. [0, 4], // two colors
  253. [0, 2, 4], // three colors
  254. [0, 1, 2, 4], // four colors
  255. [0, 1, 2, 3, 4] // five colors
  256. ];
  257. var colorIndexMap = fiveColorPalleteMaps[rangesLength - 1];
  258. return colorIndexMap;
  259. }
  260. if (rangesLength === 1) {
  261. return [Math.floor(paletteLength / 2)];
  262. }
  263. var step = (paletteLength - 1) / rangesLength;
  264. var indexMap = [0];
  265. for (var i = 2; i < rangesLength; i++) {
  266. indexMap.push(Math.floor(step * i));
  267. }
  268. indexMap.push(paletteLength - 1);
  269. return indexMap;
  270. };
  271. return KPI_VCF_Upgrade;
  272. }(UpgradeBase);
  273. return new KPI_VCF_Upgrade();
  274. });
  275. //# sourceMappingURL=KPI_VCF_Upgrade.js.map