'use strict'; 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; }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: BI Dashboard *| (C) Copyright IBM Corp. 2020 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['underscore', '../../../lib/@waca/upgrades/UpgradeBase', './utils/WidgetUpgradeUtils', './utils/VCF_RuleConstants', '../../../app/util/DeepClone'], function (_, UpgradeBase, WidgetUpgradeUtils, VCF_RuleConstants) { /** * Upgrade kpi widgets to the new version based on the KPI widget ConbditionalFormatting **/ var DefaultPaletteLength = 5; var KPIVisId = 'kpi'; var SummaryVisId = 'summary'; var rangeRuleTemplate = { 'ruleType': VCF_RuleConstants.RULE_TYPES.RANGE, 'startCondition': { 'type': VCF_RuleConstants.RULE_OPERATORS.greaterThanOrEqualTo, 'value': undefined }, 'endCondition': { 'type': VCF_RuleConstants.RULE_OPERATORS.lessThan, 'value': undefined }, 'ruleStyle': { 'textColor': undefined, 'shape': undefined }, 'operator': VCF_RuleConstants.RangeOperators.and }; var minThresholdRuleTemplate = { 'ruleType': VCF_RuleConstants.RULE_TYPES.THRESHOLD, 'startCondition': { 'type': VCF_RuleConstants.RULE_OPERATORS.lessThan, 'value': undefined }, 'ruleStyle': { 'textColor': 'color4', 'shape': undefined }, 'operator': VCF_RuleConstants.RangeOperators.none }; var maxThresholdRuleTemplate = { 'ruleType': VCF_RuleConstants.RULE_TYPES.THRESHOLD, 'startCondition': { 'type': VCF_RuleConstants.RULE_OPERATORS.greaterThanOrEqualTo, 'value': undefined }, 'ruleStyle': { 'textColor': 'color0', 'shape': undefined }, 'operator': VCF_RuleConstants.RangeOperators.none }; var equalThresholdRuleTemplate = { 'ruleType': VCF_RuleConstants.RULE_TYPES.THRESHOLD, 'startCondition': { 'type': VCF_RuleConstants.RULE_OPERATORS.equalTo, 'value': undefined }, 'ruleStyle': { 'textColor': 'color2', 'shape': undefined }, 'operator': VCF_RuleConstants.RangeOperators.none }; var KPI_VCF_Upgrade = function (_UpgradeBase) { _inherits(KPI_VCF_Upgrade, _UpgradeBase); function KPI_VCF_Upgrade() { _classCallCheck(this, KPI_VCF_Upgrade); var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this)); _this.VERSION = 1805; return _this; } /** * Perform upgrade * * @param {object} spec - spec to perform upgrade on * * @return {Promise} Promise to be resolved when upgrade performed */ KPI_VCF_Upgrade.prototype.up = function up(spec) { var _this2 = this; if (!WidgetUpgradeUtils.specHasWidgets(spec)) { return Promise.resolve(spec); } // Upgrade all the KPI widgets var promisesOfUpgrading = []; Object.values(spec.widgets).forEach(function (widgetModel) { if (widgetModel && widgetModel.visId) { var widgetVisId = widgetModel.visId; var widgetUISpecs = _this2._getObjects(spec.layout, 'id', widgetModel.id); var widgetUISpec = widgetUISpecs.length > 0 ? widgetUISpecs[0] : undefined; if (widgetVisId === KPIVisId && widgetUISpec) { var legacyCFSpec = widgetUISpec.content ? widgetUISpec.content.conditionalFormatting : undefined; var promise = _this2._getTargetConditionalFormat(widgetModel, legacyCFSpec).then(function (newCFSpec) { if (newCFSpec) { if (legacyCFSpec && legacyCFSpec.conditionalFormats) { legacyCFSpec.conditionalFormats.push(newCFSpec); } else { widgetUISpec.content = { conditionalFormatting: { conditionalFormats: [newCFSpec] } }; } } }); promisesOfUpgrading.push(promise); } else if (widgetVisId === SummaryVisId && widgetUISpec) { if (widgetUISpec.content.conditionalFormatting) { widgetUISpec.content.conditionalFormatting = undefined; } } } }); return Promise.all(promisesOfUpgrading).then(function () { return spec; }); }; // Downgrade is not available in CA KPI_VCF_Upgrade.prototype.down = function down(spec) { return Promise.resolve(spec); }; KPI_VCF_Upgrade.prototype._getObjects = function _getObjects(obj, key, val) { var objects = []; for (var i in obj) { if (!obj.hasOwnProperty(i)) continue; if (_typeof(obj[i]) == 'object') { objects = objects.concat(this._getObjects(obj[i], key, val)); } else if (i == key && obj[i] == val) { objects.push(obj); } } return objects; }; KPI_VCF_Upgrade.prototype._getSlotName = function _getSlotName(slots, mappingId) { var slotName = ''; var foundSlot = _.find(slots, function (slot) { return slot.dataItems[0] === mappingId; }); if (foundSlot) { slotName = foundSlot.name; } return slotName; }; KPI_VCF_Upgrade.prototype._getTargetConditionalFormat = function _getTargetConditionalFormat(widgetModel, legacyCFSpec) { var _this3 = this; var resultPromise = Promise.resolve(); if (legacyCFSpec) { // Default conditionalFormat var newCFSpec = { 'dataItemId': undefined, 'paletteId': undefined, 'backgroundOpacity': 0.5, 'scale': undefined, 'rules': [] }; var dataItems = []; if (widgetModel.data && widgetModel.data.dataViews && widgetModel.data.dataViews.length > 0) { widgetModel.data.dataViews.forEach(function (dataView) { dataItems.push.apply(dataItems, dataView.dataItems); }); } dataItems.forEach(function (dataItem) { var slotName = _this3._getSlotName(widgetModel.slotmapping.slots, dataItem.id); if (slotName === 'actual') { newCFSpec.dataItemId = dataItem.itemId; } else if (slotName === 'goal') { newCFSpec.colorBy = dataItem.itemId; } }); newCFSpec.paletteId = legacyCFSpec.paletteId; newCFSpec.scale = legacyCFSpec.rangeScale; if (legacyCFSpec.enabled) { 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. var ranges = legacyCFSpec.ranges; var rangesLength = ranges ? ranges.length : 0; var rule = null; resultPromise = this.data.dashboardApi.getFeature('Palette').getPalette(newCFSpec.paletteId).then(function (paletteInfo) { // we'll have palette info when this palette is a custom palette and existing on this server return paletteInfo.content.fills.length; }, function () { return DefaultPaletteLength; }).then(function (paletteLength) { var colorIndexMap = _this3._buildIndexMap(paletteLength, rangesLength); if (paletteReversed) { colorIndexMap.reverse(); } var defaultFillColors = _.range(paletteLength).map(function (index) { return 'color' + index; }); for (var i = 0; i < rangesLength; i++) { var legacyRange = ranges[i]; if (legacyRange.startValue === null) { rule = _.deepClone(minThresholdRuleTemplate); rule.startCondition.value = legacyRange.endValue; } else if (legacyRange.endValue === null) { rule = _.deepClone(maxThresholdRuleTemplate); rule.startCondition.value = legacyRange.startValue; } else if (legacyRange.startValue === legacyRange.endValue) { rule = _.deepClone(equalThresholdRuleTemplate); rule.startCondition.value = legacyRange.startValue; } else { rule = _.deepClone(rangeRuleTemplate); rule.startCondition.value = legacyRange.startValue; rule.endCondition.value = legacyRange.endValue; } if (legacyRange.shape) { rule.ruleStyle.shape = legacyRange.shape; } if (legacyRange.userSetColorId) { rule.ruleStyle.textColor = legacyRange.userSetColorId; } else if (legacyRange.defaultColorId) { rule.ruleStyle.textColor = legacyRange.defaultColorId; } else { rule.ruleStyle.textColor = defaultFillColors[colorIndexMap[i]]; } var systemColorRegex = /^color[0-9]+/; if (systemColorRegex.test(rule.ruleStyle.textColor) && !(defaultFillColors.indexOf(rule.ruleStyle.textColor) !== -1)) { rule.ruleStyle.textColor = ''; } newCFSpec.rules.push(rule); } return newCFSpec; }); } else { return Promise.resolve(newCFSpec); } } return resultPromise; }; /** * @description this method is a copy of _buildIndexMap from js/features/widget/conditionalFormatting/api/impl/DeprecatedConditionalFormatting.js * @param {int} paletteLength the length of a palette * @param {*} rangesLength the number of ranges */ KPI_VCF_Upgrade.prototype._buildIndexMap = function _buildIndexMap(paletteLength, rangesLength) { if (paletteLength <= rangesLength) { return _.range(paletteLength); } if (paletteLength === DefaultPaletteLength) { var fiveColorPalleteMaps = [[4], // one color [0, 4], // two colors [0, 2, 4], // three colors [0, 1, 2, 4], // four colors [0, 1, 2, 3, 4] // five colors ]; var colorIndexMap = fiveColorPalleteMaps[rangesLength - 1]; return colorIndexMap; } if (rangesLength === 1) { return [Math.floor(paletteLength / 2)]; } var step = (paletteLength - 1) / rangesLength; var indexMap = [0]; for (var i = 2; i < rangesLength; i++) { indexMap.push(Math.floor(step * i)); } indexMap.push(paletteLength - 1); return indexMap; }; return KPI_VCF_Upgrade; }(UpgradeBase); return new KPI_VCF_Upgrade(); }); //# sourceMappingURL=KPI_VCF_Upgrade.js.map