SummaryKPIUpgrade.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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; }
  4. 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; }
  5. /*
  6. *+------------------------------------------------------------------------+
  7. *| Licensed Materials - Property of IBM
  8. *| IBM Cognos Products: BI Dashboard
  9. *| (C) Copyright IBM Corp. 2019
  10. *|
  11. *| US Government Users Restricted Rights - Use, duplication or disclosure
  12. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  13. *+------------------------------------------------------------------------+
  14. */
  15. define(['underscore', '../../../lib/@waca/upgrades/UpgradeBase', './utils/WidgetUpgradeUtils', '../../../features/dashboard/colors/api/impl/Colors'], function (_, UpgradeBase, WidgetUpgradeUtils, Colors) {
  16. /**
  17. * Upgrade summary widgets to the new version based on the KPI widget
  18. **/
  19. var SummaryKPIUpgrade = function (_UpgradeBase) {
  20. _inherits(SummaryKPIUpgrade, _UpgradeBase);
  21. function SummaryKPIUpgrade() {
  22. _classCallCheck(this, SummaryKPIUpgrade);
  23. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  24. _this.VERSION = 1401;
  25. return _this;
  26. }
  27. /**
  28. * Perform upgrade
  29. *
  30. * @param {object} spec - spec to perform upgrade on
  31. *
  32. * @return {Promise} Promise to be resolved when upgrade performed
  33. */
  34. SummaryKPIUpgrade.prototype.up = function up(spec) {
  35. var _this2 = this;
  36. var dashboard = this.data.dashboardApi;
  37. var dashboardThemeFeature = dashboard.getFeature('DashboardTheme');
  38. var paletteFeature = dashboard.getFeature('Palette');
  39. this._colorsService = new Colors({ features: { API: this.data.dashboardApi, DashboardTheme: dashboardThemeFeature, Palette: paletteFeature } });
  40. if (!WidgetUpgradeUtils.specHasWidgets(spec)) {
  41. return Promise.resolve(spec);
  42. }
  43. var themeName = spec.theme || 'defaultTheme';
  44. return this._colorsService.initializeColorsFeatureForUpgrade(themeName).then(function () {
  45. var widgetPromises = [];
  46. _.each(spec.widgets, function (model) {
  47. if (model && WidgetUpgradeUtils.isLiveWidgetModel(model) && model.visId === 'summary') {
  48. widgetPromises.push(_this2._summaryWidgetUpgrade(model, spec));
  49. }
  50. });
  51. return Promise.all(widgetPromises).then(function () {
  52. return spec;
  53. });
  54. });
  55. };
  56. // Downgrades are not available in CA
  57. SummaryKPIUpgrade.prototype.down = function down(spec) {
  58. return Promise.resolve(spec);
  59. };
  60. SummaryKPIUpgrade.prototype._summaryWidgetUpgrade = function _summaryWidgetUpgrade(model, spec) {
  61. var _this3 = this;
  62. //Upgrade the slot mapping. The slot has changed from values to actual.
  63. if (!_.isEmpty(model.slotmapping)) {
  64. model.slotmapping.slots[0].name = 'actual';
  65. }
  66. model.enableSlotDefinitionFormat = false;
  67. var propertyMap = {};
  68. if (model.properties) {
  69. model.properties = model.properties.filter(function (property) {
  70. propertyMap[property.id] = property.value;
  71. return property.id !== 'summaryValueColor' && property.id !== 'summaryLabelColor';
  72. });
  73. }
  74. var paletteId = propertyMap.colorPalette || this._colorsService.getDefaultPaletteName('ColorPalette');
  75. var colorIndex = propertyMap.valueColor || 0;
  76. return this._colorsService.getHexColorFromPalette({
  77. paletteId: paletteId,
  78. colorIndex: colorIndex,
  79. defaultIfNotFound: true,
  80. addMissingBaseColors: false
  81. }).then(function (paletteHexColor) {
  82. if (!model.properties) {
  83. model.properties = [];
  84. }
  85. var color = paletteHexColor.toUpperCase();
  86. _this3._addCustomColor(spec, color);
  87. model.properties.push({
  88. id: 'elementColor',
  89. value: 'customColor' + color.substring(1)
  90. });
  91. });
  92. };
  93. SummaryKPIUpgrade.prototype._addCustomColor = function _addCustomColor(spec, color) {
  94. if (!spec.properties) {
  95. spec.properties = {};
  96. }
  97. if (!spec.properties.customColors) {
  98. spec.properties['customColors'] = {
  99. 'colors': [color]
  100. };
  101. } else {
  102. var colorList = spec.properties.customColors.colors;
  103. if (colorList.indexOf(color) === -1) {
  104. colorList.push(color);
  105. }
  106. }
  107. };
  108. return SummaryKPIUpgrade;
  109. }(UpgradeBase);
  110. return new SummaryKPIUpgrade();
  111. });
  112. //# sourceMappingURL=SummaryKPIUpgrade.js.map