ExploreToDashboardConverter.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  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: Conversion service
  10. *| (C) Copyright IBM Corp. 2018, 2021
  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', './ConversionService'], function (_, ConversionService) {
  17. var conversionServiceInstance = new ConversionService();
  18. var ConverterInterface = conversionServiceInstance.getInterface();
  19. var ExploreToDashboardConverter = function (_ConverterInterface) {
  20. _inherits(ExploreToDashboardConverter, _ConverterInterface);
  21. function ExploreToDashboardConverter(definition) {
  22. _classCallCheck(this, ExploreToDashboardConverter);
  23. return _possibleConstructorReturn(this, (ExploreToDashboardConverter.__proto__ || Object.getPrototypeOf(ExploreToDashboardConverter)).call(this, Object.assign({
  24. src: 'EXPLORE',
  25. target: 'DASHBOARD'
  26. }, definition)));
  27. }
  28. _createClass(ExploreToDashboardConverter, [{
  29. key: 'convert',
  30. value: function convert(src, target, spec) {
  31. var _this2 = this;
  32. return new Promise(function (resolve, reject) {
  33. var specJson = JSON.parse(spec);
  34. if (!_this2.validate(specJson)) {
  35. reject(new Error('Invalid incoming spec'));
  36. }
  37. _this2.filterOutEmptyWidgets(specJson);
  38. resolve(JSON.stringify(_this2._doConvert(specJson)));
  39. });
  40. }
  41. }, {
  42. key: 'validate',
  43. value: function validate(spec) {
  44. return !_.isEmpty(spec) && !_.isEmpty(this._getFilteredWidgets(spec));
  45. }
  46. }, {
  47. key: 'filterOutEmptyWidgets',
  48. value: function filterOutEmptyWidgets(spec) {
  49. var filteredWidgets = this._getFilteredWidgets(spec);
  50. var filteredLayoutItems = this._getFilteredLayoutItems(spec.layout.items, filteredWidgets);
  51. spec.layout.items = filteredLayoutItems;
  52. spec.widgets = filteredWidgets;
  53. }
  54. }, {
  55. key: '_getFilteredWidgets',
  56. value: function _getFilteredWidgets() {
  57. var spec = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  58. var filteredWidgets = {};
  59. var widgets = [];
  60. if (spec.widgets) {
  61. widgets = Object.values(spec.widgets);
  62. } else if (spec.layout && spec.layout.items) {
  63. widgets = this._findWidgets(spec.layout.items);
  64. }
  65. widgets.forEach(function (widget) {
  66. if (widget.type === 'live') {
  67. filteredWidgets[widget.id] = widget;
  68. }
  69. });
  70. return filteredWidgets;
  71. }
  72. }, {
  73. key: '_findWidgets',
  74. value: function _findWidgets() {
  75. var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  76. var widgets = [];
  77. for (var i = 0; i < items.length; i++) {
  78. var item = items[i];
  79. if (item.items) {
  80. widgets = widgets.concat(this._findWidgets(item.items));
  81. } else if (item.features) {
  82. widgets.push(item.features['Models_internal']);
  83. }
  84. }
  85. return widgets;
  86. }
  87. }, {
  88. key: '_getFilteredLayoutItems',
  89. value: function _getFilteredLayoutItems(layoutItems, filteredWidgets) {
  90. return layoutItems.filter(function (item) {
  91. if (item.subType === 'compare') {
  92. var firstWidgetId = item.items[0].id;
  93. var secondWidgetId = item.items[1].id;
  94. var hasLiveWidgets = filteredWidgets[firstWidgetId] || filteredWidgets[secondWidgetId];
  95. // Should only keep live widgets in comparison card layouts
  96. item.items = item.items.filter(function (widget) {
  97. return filteredWidgets[widget.id];
  98. });
  99. return hasLiveWidgets;
  100. } else {
  101. var widgetId = item.items[0].id;
  102. return filteredWidgets[widgetId];
  103. }
  104. });
  105. }
  106. }, {
  107. key: '_doConvert',
  108. value: function _doConvert(exploreSpec) {
  109. var fragmentSpec = {
  110. dataSources: exploreSpec.dataSources,
  111. widgets: this._cleanWidgets(_.values(exploreSpec.widgets))
  112. };
  113. var convertedCards = this._convertCards(exploreSpec.layout.items);
  114. fragmentSpec.layout = convertedCards.layout;
  115. fragmentSpec.widgetToCardMap = convertedCards.widgetToCardMap;
  116. return fragmentSpec;
  117. }
  118. }, {
  119. key: '_cleanWidgets',
  120. value: function _cleanWidgets(widgets) {
  121. return widgets.map(function (widget) {
  122. return _.omit(widget, 'thumbnailId');
  123. });
  124. }
  125. }, {
  126. key: '_convertCards',
  127. value: function _convertCards(cards) {
  128. var _this3 = this;
  129. return cards.reduce(function (acc, card) {
  130. return _this3._convertCard(acc, card);
  131. }, {
  132. layout: [],
  133. widgetToCardMap: {}
  134. });
  135. }
  136. }, {
  137. key: '_convertCard',
  138. value: function _convertCard(acc, card) {
  139. var widgets = this._getWidgets(card); //in order
  140. if (widgets.length === 1) {
  141. acc.layout.push(this._getGroupedItems(widgets)[0]);
  142. acc.widgetToCardMap[widgets[0].id] = card.id;
  143. } else {
  144. var groupId = _.uniqueId('group');
  145. acc.layout.push({
  146. id: groupId,
  147. type: 'group',
  148. relatedLayouts: '',
  149. items: this._getGroupedItems(widgets),
  150. style: {
  151. top: '0%',
  152. left: '0%',
  153. height: '50%',
  154. width: '50%'
  155. }
  156. });
  157. acc.widgetToCardMap[groupId] = card.id;
  158. }
  159. return acc;
  160. }
  161. }, {
  162. key: '_getWidgets',
  163. value: function _getWidgets(card) {
  164. var widgets = [];
  165. card.items.forEach(function (item) {
  166. if (item.type === 'widget') {
  167. widgets.push(item);
  168. }
  169. });
  170. return widgets;
  171. }
  172. }, {
  173. key: '_trimPercent',
  174. value: function _trimPercent(percent) {
  175. return +percent.toFixed(2);
  176. }
  177. }, {
  178. key: '_getGroupedItems',
  179. value: function _getGroupedItems(widgets) {
  180. var _this4 = this;
  181. var percentage = widgets.length > 1 ? 100 : 50;
  182. var width = this._trimPercent(1 / widgets.length * percentage) + '%';
  183. return widgets.map(function (widget, index) {
  184. var widgetSpec = {
  185. id: widget.id,
  186. type: 'widget',
  187. relatedLayouts: '',
  188. style: {
  189. top: '0%',
  190. left: _this4._trimPercent(index / widgets.length * 100) + '%',
  191. height: percentage + '%',
  192. width: width
  193. },
  194. content: widget.content
  195. };
  196. if (widget.properties) {
  197. //explore widget properties should be persisted after conversion.please find defect explore-2966
  198. widgetSpec.properties = widget.properties;
  199. }
  200. return widgetSpec;
  201. });
  202. }
  203. }]);
  204. return ExploreToDashboardConverter;
  205. }(ConverterInterface);
  206. return ExploreToDashboardConverter;
  207. });
  208. //# sourceMappingURL=ExploreToDashboardConverter.js.map