ConditionalPaletteUpgradeHelper.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /*
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: BI Dashboard
  7. *| (C) Copyright IBM Corp. 2018
  8. *|
  9. *| US Government Users Restricted Rights - Use, duplication or disclosure
  10. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  11. *+------------------------------------------------------------------------+
  12. */
  13. define(['underscore'], function (_) {
  14. var ConditionalPaletteUpgradeHelper = function () {
  15. function ConditionalPaletteUpgradeHelper() {
  16. _classCallCheck(this, ConditionalPaletteUpgradeHelper);
  17. }
  18. /**
  19. * Perform upgrade
  20. *
  21. * @param {object} spec - spec to perform upgrade on
  22. *
  23. * @return {Promise} Promise to be resolved when upgrade performed
  24. */
  25. ConditionalPaletteUpgradeHelper.prototype.upgrade = function upgrade(spec, dashboardApi) {
  26. var _this = this;
  27. return dashboardApi.getGlassSvc('.DashboardTheme').then(function (dashboardThemeService) {
  28. return dashboardThemeService.getThemeDefinition(spec.theme).then(function (themeDefinition) {
  29. // Get all the conditional palettes for the theme.
  30. var conditionalPalettes = themeDefinition.getPalettes('ConditionalPalette');
  31. for (var widgetId in spec.widgets) {
  32. var widget = spec.widgets[widgetId];
  33. _this._updateWidgetConditionalPalette(widget, conditionalPalettes, dashboardApi);
  34. }
  35. return spec;
  36. });
  37. });
  38. };
  39. /**
  40. * If the widget has a conditional palette, get the palette name by comparing the hex values that are saved in the spec
  41. */
  42. ConditionalPaletteUpgradeHelper.prototype._updateWidgetConditionalPalette = function _updateWidgetConditionalPalette(widget, conditionalPalettes, dashboardApi) {
  43. if (!widget || !widget.conditions || !widget.conditions.palette) {
  44. return;
  45. }
  46. var widgetPaletteColors = widget.conditions.palette.colors;
  47. // If we already have an Endor spec
  48. if (!widgetPaletteColors || widgetPaletteColors.length < 1 || !widgetPaletteColors[0].bgcolor) {
  49. return;
  50. }
  51. var paletteColors = widgetPaletteColors.map(function (_ref) {
  52. var bgcolor = _ref.bgcolor;
  53. return bgcolor;
  54. } //pluck bgcolor
  55. ).filter(function (color) {
  56. return color && color.length;
  57. } //filter out invalid colours
  58. ).map(function (color) {
  59. return color.toUpperCase();
  60. } //to allow case-insensitive search
  61. );
  62. var isWidgetPaletteAPattern = widgetPaletteColors[0].pattern ? true : false;
  63. var paletteId = null;
  64. for (var _iterator = conditionalPalettes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  65. var _ref2;
  66. if (_isArray) {
  67. if (_i >= _iterator.length) break;
  68. _ref2 = _iterator[_i++];
  69. } else {
  70. _i = _iterator.next();
  71. if (_i.done) break;
  72. _ref2 = _i.value;
  73. }
  74. var conditionalPalette = _ref2;
  75. if (isWidgetPaletteAPattern) {
  76. if (conditionalPalette.fills[0].pattern) {
  77. paletteId = conditionalPalette.id;
  78. break;
  79. }
  80. } else {
  81. var foundAllColors = true;
  82. var conditionalPaletteColours = conditionalPalette.fills.filter(function (fill) {
  83. return _.isString(fill);
  84. } //we only attempt to match against strings
  85. ).map(function (fill) {
  86. return fill.toUpperCase();
  87. } //to allow case-insensitive search
  88. );
  89. // Since the user could of removed any number of colors from the palette we can't simply do a direct match.
  90. // Loops through all the colors from the widget spec and see if they're all found in the palette. If they
  91. // are concider it a match
  92. for (var _iterator2 = paletteColors, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  93. var _ref3;
  94. if (_isArray2) {
  95. if (_i2 >= _iterator2.length) break;
  96. _ref3 = _iterator2[_i2++];
  97. } else {
  98. _i2 = _iterator2.next();
  99. if (_i2.done) break;
  100. _ref3 = _i2.value;
  101. }
  102. var color = _ref3;
  103. if (conditionalPaletteColours.indexOf(color) === -1) {
  104. foundAllColors = false;
  105. break;
  106. }
  107. }
  108. if (foundAllColors) {
  109. paletteId = conditionalPalette.id;
  110. break;
  111. }
  112. }
  113. }
  114. // Should never happen, we should always find a match. But log it as an error if we don't
  115. if (!paletteId) {
  116. dashboardApi.getGlassCoreSvc('.Logger').error('Could not find a match for the conditional palette', widget);
  117. }
  118. // Make sure we have a properties array
  119. if (!widget.properties) {
  120. widget.properties = [];
  121. }
  122. widget.properties.push({ id: 'condColorPalette', value: paletteId });
  123. };
  124. return ConditionalPaletteUpgradeHelper;
  125. }();
  126. return ConditionalPaletteUpgradeHelper;
  127. });
  128. //# sourceMappingURL=ConditionalPaletteUpgradeHelper.js.map