'use strict'; 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; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Explore *| (C) Copyright IBM Corp. 2018, 2019 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['underscore', './ConversionService'], function (_, ConversionService) { var conversionServiceInstance = new ConversionService(); var ConverterInterface = conversionServiceInstance.getInterface(); var WIDGET_PROPERTIES_WHITELIST = [ // area 'area.interpolate', 'data.handling', // bar 'valueLabels.visible', //composite v2 'column.valueLabels.visible', 'lineWithPoints.valueLabels.visible', // line 'valueAxis.zeroOrigin', 'valueLabels.format', // pie 'label.percentage', // radial 'valueLabels.format', // stacked bar 'stacked.percent', 'valueLabels.visible', // summary 'showItemLabel', //KPI 'conditionalFormattingEnabled', 'rangeScale', 'numberOfRanges', 'conditionalFormatting', // Crosstab 'fixedRowHeight', 'fixedColumnWidth', 'columnWidths', 'rowHeights']; var WIDGET_STYLES_BLACKLIST = ['fillColor', 'borderColor']; var DashboardToExploreConverter = function (_ConverterInterface) { _inherits(DashboardToExploreConverter, _ConverterInterface); function DashboardToExploreConverter(definition) { _classCallCheck(this, DashboardToExploreConverter); var _this = _possibleConstructorReturn(this, (DashboardToExploreConverter.__proto__ || Object.getPrototypeOf(DashboardToExploreConverter)).call(this, Object.assign({ src: 'DASHBOARD', target: 'EXPLORE' }, definition))); _this.isStylesStripped = false; return _this; } _createClass(DashboardToExploreConverter, [{ key: 'convert', value: function convert(src, target, spec) { var _this2 = this; return new Promise(function (resolve, reject) { var specJson = JSON.parse(spec); if (!_this2.validate(specJson)) { reject(new Error('Invalid incoming spec')); } resolve(JSON.stringify(_this2._doConvert(specJson))); }); } }, { key: 'validate', value: function validate(spec) { return !!spec; } /** * * Get the content model properties from board layout spec * @param {object} spec board layout spec * @param {object} contentMap * @example { 'model1': {'conditionalFormatting': {}}, 'model2': {'properties' : {}}} */ }, { key: '_getContentFromLayout', value: function _getContentFromLayout(spec, contentMap) { var _this3 = this; if (!spec) { return; } if (spec.content) { contentMap[spec.id] = spec.content; } if (spec.items && spec.items.length > 0) { spec.items.forEach(function (item) { return _this3._getContentFromLayout(item, contentMap); }); } } }, { key: '_getContentFromSpec', value: function _getContentFromSpec(spec) { var _this4 = this; var contentMap = {}; // spec returned from copy is slightly different from regular dashboard spec where layout is an array; // while in regular dashboard spec, layout is an object. if (_.isArray(spec.layout)) { spec.layout.forEach(function (layout) { _this4._getContentFromLayout(layout, contentMap); }); } else { this._getContentFromLayout(spec.layout, contentMap); } return contentMap; } //convert dashboard fragment spec }, { key: '_doConvert', value: function _doConvert(dashboardSpec) { var _this5 = this; //layout var cards = []; var widgetMap = {}; var contentMap = this._getContentFromSpec(dashboardSpec); dashboardSpec.widgets.filter(function (widget) { return _this5._isSupported(widget); }).forEach(function (widget) { var card = _this5._getCard(widget, contentMap); cards.push(card); _this5._stripoffWidgetStyles(widget); delete widget.titleHtml; delete widget.name; widget.showTitle = true; widgetMap[widget.id] = widget; }); //copy datasources and others var exploreSpec = null; if (cards.length > 0) { exploreSpec = dashboardSpec; //At this point, for both launch and paste, dashboardSpecHelper.getContents() has already converted the applicable //pageContext (filter dock) filters to local filters in each widget spec. //Explore targets don't expose pageContext for single cards so we should not include it in the target spec. //(note: Ocassionally, dashboard to dashboard paste overrides this behaviour and uses pageContext filters directly.) exploreSpec.pageContext = []; exploreSpec.widgets = widgetMap; exploreSpec.layout = { id: _.uniqueId('exploreContainer'), items: cards, type: 'exploreContainer' }; //Set a flag indicating that styles have been stripped off, //so that a toast message can be shown in Explore, once the //toast is shown, this flag will be removed. //TODO: This solution involves addtional manipulations on object //property,so there cound be a better way to show toast which gets //rid ofthe additional manipulations. if (this.isStylesStripped) { exploreSpec.isStylesStripped = true; this.isStylesStripped = false; } } return exploreSpec; } /** * Strip off styles of the widget * * @param {Object} widget - widget object */ }, { key: '_stripoffWidgetStyles', value: function _stripoffWidgetStyles() { var _this6 = this; var widget = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; // Filter out widget properties if (widget.hasOwnProperty('properties')) { var filterProperties = this._filterWidgetProperties(widget['properties']); if (filterProperties.length < widget['properties'].length) { if (filterProperties.length > 0) { widget['properties'] = filterProperties; } else { delete widget['properties']; } this.isStylesStripped = true; } } // Filter out widget styles WIDGET_STYLES_BLACKLIST.forEach(function (prop) { if (widget.hasOwnProperty(prop)) { delete widget[prop]; if (!_this6.isStylesStripped) { _this6.isStylesStripped = true; } } }); } }, { key: '_filterWidgetProperties', value: function _filterWidgetProperties(properties) { return properties.filter(function (prop) { return WIDGET_PROPERTIES_WHITELIST.indexOf(prop.id) !== -1; }); } }, { key: '_getCard', value: function _getCard(widget, contentMap) { return { id: _.uniqueId('exploreCard'), type: 'exploreCard', items: [{ id: widget.id, type: 'widget', content: contentMap[widget.id] }], isHidden: false, isFavourite: false }; } }, { key: '_isSupported', value: function _isSupported(widget) { var unsupportedVisIds = ['dataPlayer', 'customVis', 'schematic', 'visualizationPreview', 'com.ibm.vis.schematicsPreview']; return widget.type === 'live' && unsupportedVisIds.find(function (id) { return id === widget.visId; }) === undefined; } }]); return DashboardToExploreConverter; }(ConverterInterface); return DashboardToExploreConverter; }); //# sourceMappingURL=DashboardToExploreConverter.js.map