DashboardToExploreConverter.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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: Explore
  10. *| (C) Copyright IBM Corp. 2018, 2019
  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 WIDGET_PROPERTIES_WHITELIST = [
  20. // area
  21. 'area.interpolate', 'data.handling',
  22. // bar
  23. 'valueLabels.visible',
  24. //composite v2
  25. 'column.valueLabels.visible', 'lineWithPoints.valueLabels.visible',
  26. // line
  27. 'valueAxis.zeroOrigin', 'valueLabels.format',
  28. // pie
  29. 'label.percentage',
  30. // radial
  31. 'valueLabels.format',
  32. // stacked bar
  33. 'stacked.percent', 'valueLabels.visible',
  34. // summary
  35. 'showItemLabel',
  36. //KPI
  37. 'conditionalFormattingEnabled', 'rangeScale', 'numberOfRanges', 'conditionalFormatting',
  38. // Crosstab
  39. 'fixedRowHeight', 'fixedColumnWidth', 'columnWidths', 'rowHeights'];
  40. var WIDGET_STYLES_BLACKLIST = ['fillColor', 'borderColor'];
  41. var DashboardToExploreConverter = function (_ConverterInterface) {
  42. _inherits(DashboardToExploreConverter, _ConverterInterface);
  43. function DashboardToExploreConverter(definition) {
  44. _classCallCheck(this, DashboardToExploreConverter);
  45. var _this = _possibleConstructorReturn(this, (DashboardToExploreConverter.__proto__ || Object.getPrototypeOf(DashboardToExploreConverter)).call(this, Object.assign({
  46. src: 'DASHBOARD',
  47. target: 'EXPLORE'
  48. }, definition)));
  49. _this.isStylesStripped = false;
  50. return _this;
  51. }
  52. _createClass(DashboardToExploreConverter, [{
  53. key: 'convert',
  54. value: function convert(src, target, spec) {
  55. var _this2 = this;
  56. return new Promise(function (resolve, reject) {
  57. var specJson = JSON.parse(spec);
  58. if (!_this2.validate(specJson)) {
  59. reject(new Error('Invalid incoming spec'));
  60. }
  61. resolve(JSON.stringify(_this2._doConvert(specJson)));
  62. });
  63. }
  64. }, {
  65. key: 'validate',
  66. value: function validate(spec) {
  67. return !!spec;
  68. }
  69. /**
  70. *
  71. * Get the content model properties from board layout spec
  72. * @param {object} spec board layout spec
  73. * @param {object} contentMap
  74. * @example { 'model1': {'conditionalFormatting': {}}, 'model2': {'properties' : {}}}
  75. */
  76. }, {
  77. key: '_getContentFromLayout',
  78. value: function _getContentFromLayout(spec, contentMap) {
  79. var _this3 = this;
  80. if (!spec) {
  81. return;
  82. }
  83. if (spec.content) {
  84. contentMap[spec.id] = spec.content;
  85. }
  86. if (spec.items && spec.items.length > 0) {
  87. spec.items.forEach(function (item) {
  88. return _this3._getContentFromLayout(item, contentMap);
  89. });
  90. }
  91. }
  92. }, {
  93. key: '_getContentFromSpec',
  94. value: function _getContentFromSpec(spec) {
  95. var _this4 = this;
  96. var contentMap = {};
  97. // spec returned from copy is slightly different from regular dashboard spec where layout is an array;
  98. // while in regular dashboard spec, layout is an object.
  99. if (_.isArray(spec.layout)) {
  100. spec.layout.forEach(function (layout) {
  101. _this4._getContentFromLayout(layout, contentMap);
  102. });
  103. } else {
  104. this._getContentFromLayout(spec.layout, contentMap);
  105. }
  106. return contentMap;
  107. }
  108. //convert dashboard fragment spec
  109. }, {
  110. key: '_doConvert',
  111. value: function _doConvert(dashboardSpec) {
  112. var _this5 = this;
  113. //layout
  114. var cards = [];
  115. var widgetMap = {};
  116. var contentMap = this._getContentFromSpec(dashboardSpec);
  117. dashboardSpec.widgets.filter(function (widget) {
  118. return _this5._isSupported(widget);
  119. }).forEach(function (widget) {
  120. var card = _this5._getCard(widget, contentMap);
  121. cards.push(card);
  122. _this5._stripoffWidgetStyles(widget);
  123. delete widget.titleHtml;
  124. delete widget.name;
  125. widget.showTitle = true;
  126. widgetMap[widget.id] = widget;
  127. });
  128. //copy datasources and others
  129. var exploreSpec = null;
  130. if (cards.length > 0) {
  131. exploreSpec = dashboardSpec;
  132. //At this point, for both launch and paste, dashboardSpecHelper.getContents() has already converted the applicable
  133. //pageContext (filter dock) filters to local filters in each widget spec.
  134. //Explore targets don't expose pageContext for single cards so we should not include it in the target spec.
  135. //(note: Ocassionally, dashboard to dashboard paste overrides this behaviour and uses pageContext filters directly.)
  136. exploreSpec.pageContext = [];
  137. exploreSpec.widgets = widgetMap;
  138. exploreSpec.layout = {
  139. id: _.uniqueId('exploreContainer'),
  140. items: cards,
  141. type: 'exploreContainer'
  142. };
  143. //Set a flag indicating that styles have been stripped off,
  144. //so that a toast message can be shown in Explore, once the
  145. //toast is shown, this flag will be removed.
  146. //TODO: This solution involves addtional manipulations on object
  147. //property,so there cound be a better way to show toast which gets
  148. //rid ofthe additional manipulations.
  149. if (this.isStylesStripped) {
  150. exploreSpec.isStylesStripped = true;
  151. this.isStylesStripped = false;
  152. }
  153. }
  154. return exploreSpec;
  155. }
  156. /**
  157. * Strip off styles of the widget
  158. *
  159. * @param {Object} widget - widget object
  160. */
  161. }, {
  162. key: '_stripoffWidgetStyles',
  163. value: function _stripoffWidgetStyles() {
  164. var _this6 = this;
  165. var widget = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  166. // Filter out widget properties
  167. if (widget.hasOwnProperty('properties')) {
  168. var filterProperties = this._filterWidgetProperties(widget['properties']);
  169. if (filterProperties.length < widget['properties'].length) {
  170. if (filterProperties.length > 0) {
  171. widget['properties'] = filterProperties;
  172. } else {
  173. delete widget['properties'];
  174. }
  175. this.isStylesStripped = true;
  176. }
  177. }
  178. // Filter out widget styles
  179. WIDGET_STYLES_BLACKLIST.forEach(function (prop) {
  180. if (widget.hasOwnProperty(prop)) {
  181. delete widget[prop];
  182. if (!_this6.isStylesStripped) {
  183. _this6.isStylesStripped = true;
  184. }
  185. }
  186. });
  187. }
  188. }, {
  189. key: '_filterWidgetProperties',
  190. value: function _filterWidgetProperties(properties) {
  191. return properties.filter(function (prop) {
  192. return WIDGET_PROPERTIES_WHITELIST.indexOf(prop.id) !== -1;
  193. });
  194. }
  195. }, {
  196. key: '_getCard',
  197. value: function _getCard(widget, contentMap) {
  198. return {
  199. id: _.uniqueId('exploreCard'),
  200. type: 'exploreCard',
  201. items: [{
  202. id: widget.id,
  203. type: 'widget',
  204. content: contentMap[widget.id]
  205. }],
  206. isHidden: false,
  207. isFavourite: false
  208. };
  209. }
  210. }, {
  211. key: '_isSupported',
  212. value: function _isSupported(widget) {
  213. var unsupportedVisIds = ['dataPlayer', 'customVis', 'schematic', 'visualizationPreview', 'com.ibm.vis.schematicsPreview'];
  214. return widget.type === 'live' && unsupportedVisIds.find(function (id) {
  215. return id === widget.visId;
  216. }) === undefined;
  217. }
  218. }]);
  219. return DashboardToExploreConverter;
  220. }(ConverterInterface);
  221. return DashboardToExploreConverter;
  222. });
  223. //# sourceMappingURL=DashboardToExploreConverter.js.map