CompositeChartV2Upgrade.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  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/core-client/js/core-client/utils/UniqueId', '../../../lib/@waca/upgrades/UpgradeBase', './utils/WidgetUpgradeUtils'], function (_, UniqueId, UpgradeBase, WidgetUpgradeUtils) {
  17. var CHART_ID = 'com.ibm.vis.rave2bundlecomposite';
  18. // The id map with all boolean properties that are needed to change type
  19. var PROPERTY_TYPE_UPGRADE_MAP = {
  20. 'lines.smooth': {
  21. newPropId: 'lineWithPoints.interpolate',
  22. trueValue: 'cardinal',
  23. falseValue: 'linear',
  24. defaultValue: 'linear'
  25. },
  26. 'markers.visible': {
  27. newPropId: 'lineWithPoints.display',
  28. trueValue: 'line_points',
  29. falseValue: 'line',
  30. defaultValue: 'line_points'
  31. }
  32. };
  33. // The id map with all properties that are needed to change name
  34. var PROPERTY_NAME_UPGRADE_MAP = {
  35. 'gridLines.visible': ['valueAxis.gridLines.visible', 'itemAxis.gridLines.visible'],
  36. 'valueAxis.title.visible': ['valueAxis.column.title.visible', 'valueAxis.line.title.visible'],
  37. 'valueLabels.visible': ['column.valueLabels.visible', 'lineWithPoints.valueLabels.visible'],
  38. 'valueLabels.color': ['column.valueLabels.color', 'lineWithPoints.valueLabels.color'],
  39. 'valueLabels.font': ['column.valueLabels.font', 'lineWithPoints.valueLabels.font'],
  40. 'valueAxis.ticks.labels.visible': ['valueAxis.column.ticks.labels.visible', 'valueAxis.line.ticks.labels.visible'],
  41. 'valueAxis.ticks.labels.color': ['valueAxis.column.ticks.labels.color', 'valueAxis.line.ticks.labels.color'],
  42. 'valueAxis.ticks.labels.font': ['valueAxis.column.ticks.labels.font', 'valueAxis.line.ticks.labels.font'],
  43. 'valueAxis.ticks.visible': ['valueAxis.column.ticks.visible', 'valueAxis.line.ticks.visible'],
  44. 'valueAxis.ticks.color': ['valueAxis.column.ticks.color', 'valueAxis.line.ticks.color'],
  45. 'colorPalette': ['colorPalette_column', 'colorPalette_line'],
  46. 'defaultPaletteIndex': ['colorPalette_column_defaultIndex'],
  47. 'compositeLineColor': ['colorPalette_line_defaultIndex']
  48. };
  49. var CompositeChartV2Upgrade = function (_UpgradeBase) {
  50. _inherits(CompositeChartV2Upgrade, _UpgradeBase);
  51. function CompositeChartV2Upgrade() {
  52. _classCallCheck(this, CompositeChartV2Upgrade);
  53. var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
  54. _this.VERSION = 1803;
  55. return _this;
  56. }
  57. /**
  58. * Perform upgrade
  59. *
  60. * @param {object} spec - spec to perform upgrade on
  61. *
  62. * @return {Promise} Promise to be resolved when upgrade performed
  63. */
  64. CompositeChartV2Upgrade.prototype.up = function up(spec) {
  65. var _this2 = this;
  66. if (!spec) {
  67. return Promise.resolve(spec);
  68. }
  69. if (!WidgetUpgradeUtils.specHasWidgets(spec)) {
  70. return Promise.resolve(spec);
  71. }
  72. _.each(spec.widgets, function (model) {
  73. if (WidgetUpgradeUtils.isLiveWidgetModel(model)) {
  74. if (!model.properties) {
  75. model.properties = [];
  76. }
  77. if (model.visId === CHART_ID) {
  78. Object.keys(PROPERTY_TYPE_UPGRADE_MAP).forEach(function (oldPropId) {
  79. return _this2._upgradeBooleanProperty(_extends({ model: model, oldPropId: oldPropId }, PROPERTY_TYPE_UPGRADE_MAP[oldPropId]));
  80. });
  81. Object.keys(PROPERTY_NAME_UPGRADE_MAP).forEach(function (oldPropId) {
  82. return _this2._upgradePropertyName(model, oldPropId, PROPERTY_NAME_UPGRADE_MAP[oldPropId]);
  83. });
  84. _this2._splitDataView(model);
  85. }
  86. }
  87. });
  88. return Promise.resolve(spec);
  89. };
  90. // Downgrades are not available in CA
  91. CompositeChartV2Upgrade.prototype.down = function down(spec) {
  92. return Promise.resolve(spec);
  93. };
  94. /**
  95. * Upgrade a boolean type property to a non-boolean type property.
  96. *
  97. * @param {object} options.model - model to perform upgrade on
  98. * @param {string} options.oldPropId -the deprecated prop id
  99. * @param {string} options.newPropId - the new prop id
  100. * @param {any} options.trueValue - value to set for the new prop when the deprecated value is set to true
  101. * @param {any} options.falseValue - value to set for the new prop when the deprecated value is set to false
  102. * @param {any} options.defaultValue - value to set for the new prop when the deprecated value is undefined
  103. */
  104. CompositeChartV2Upgrade.prototype._upgradeBooleanProperty = function _upgradeBooleanProperty(options) {
  105. var model = options.model,
  106. oldPropId = options.oldPropId,
  107. newPropId = options.newPropId,
  108. trueValue = options.trueValue,
  109. falseValue = options.falseValue,
  110. defaultValue = options.defaultValue;
  111. var deprecatedProp = WidgetUpgradeUtils.findProperty(oldPropId, model.properties);
  112. if (!deprecatedProp) {
  113. return;
  114. }
  115. model.properties = _.filter(model.properties, function (prop) {
  116. return prop.id !== oldPropId;
  117. });
  118. if (deprecatedProp.value && defaultValue !== trueValue) {
  119. model.properties.push({
  120. id: newPropId,
  121. value: trueValue
  122. });
  123. } else if (!deprecatedProp.value && defaultValue !== falseValue) {
  124. model.properties.push({
  125. id: newPropId,
  126. value: falseValue
  127. });
  128. }
  129. };
  130. /**
  131. * Upgrade a old property id to a new one(s) with the same property value.
  132. *
  133. * @param {object} model - model to perform upgrade on
  134. * @param {string} oldPropId -the deprecated prop id
  135. * @param {string} newPropIds - the list of new prop ids
  136. */
  137. CompositeChartV2Upgrade.prototype._upgradePropertyName = function _upgradePropertyName(model, oldId, newIds) {
  138. var deprecatedProp = WidgetUpgradeUtils.findProperty(oldId, model.properties);
  139. if (deprecatedProp && !_.isUndefined(deprecatedProp.value)) {
  140. model.properties = _.filter(model.properties, function (prop) {
  141. return prop.id !== oldId;
  142. });
  143. newIds.forEach(function (id) {
  144. model.properties.push({
  145. id: id,
  146. value: deprecatedProp.value
  147. });
  148. });
  149. }
  150. };
  151. CompositeChartV2Upgrade.prototype._splitDataView = function _splitDataView(model) {
  152. if (!model.data || _.isEmpty(model.data.dataViews)) {
  153. return;
  154. }
  155. var dataViews = model.data && model.data.dataViews;
  156. var columnDataView = dataViews[0];
  157. var slots = model.slotmapping && model.slotmapping.slots;
  158. var lineValueSlot = slots.find(function (slot) {
  159. return slot.name === 'lineValue';
  160. });
  161. slots.forEach(function (slot) {
  162. delete slot.layerId;
  163. });
  164. if (lineValueSlot) {
  165. var lineValueDataItemId = lineValueSlot.dataItems[0];
  166. var lineValueDataItemIndex = columnDataView.dataItems.findIndex(function (dataItem) {
  167. return dataItem.id === lineValueDataItemId;
  168. });
  169. var lineValueDataItem = columnDataView.dataItems.splice(lineValueDataItemIndex, 1);
  170. dataViews.push({
  171. modelRef: columnDataView.modelRef,
  172. dataItems: lineValueDataItem,
  173. id: UniqueId.get('model')
  174. });
  175. }
  176. };
  177. return CompositeChartV2Upgrade;
  178. }(UpgradeBase);
  179. return new CompositeChartV2Upgrade();
  180. });
  181. //# sourceMappingURL=CompositeChartV2Upgrade.js.map