123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- 'use strict';
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
- 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: BI Dashboard
- *| (C) Copyright IBM Corp. 2020
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- define(['underscore', '../../../lib/@waca/upgrades/UpgradeBase', './utils/WidgetUpgradeUtils', './utils/VCF_RuleConstants', 'dashboard-analytics/dataSources/services/DataSourcesService', '../../../lib/@waca/dashboard-common/dist/core/UniqueHashIdBuilder', '../../../app/nls/StringResources', '../../../app/util/DeepClone'], function (_, UpgradeBase, WidgetUpgradeUtils, VCF_RuleConstants, DataSourcesService, UniqueHashIdBuilder, StringResources) {
- /**
- * Upgrade Crosstab/Table widgets to the new version based on the Crosstab/Table ConditionalFormatting feature
- **/
- var defaultPaletteLength = 5;
- var CrosstabTable_VCF_Upgrade = function (_UpgradeBase) {
- _inherits(CrosstabTable_VCF_Upgrade, _UpgradeBase);
- function CrosstabTable_VCF_Upgrade() {
- _classCallCheck(this, CrosstabTable_VCF_Upgrade);
- var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));
- _this.VERSION = 1807;
- return _this;
- }
- /**
- * Perform upgrade
- *
- * @param {object} spec - spec to perform upgrade on
- *
- * @return {Promise} Promise to be resolved when upgrade performed
- */
- CrosstabTable_VCF_Upgrade.prototype.up = function up(spec) {
- var _this2 = this;
- return Promise.resolve().then(function () {
- if (!WidgetUpgradeUtils.specHasWidgets(spec)) {
- return spec;
- }
- var dataSourcesClass = _this2._DataSourcesServices || DataSourcesService;
- _this2.dataSourcesService = new dataSourcesClass({
- features: {
- API: _this2.data.dashboardApi,
- Logger: _this2.data.logger
- }
- });
- _this2.dataSourcesService.initializeSourcesCollection(spec);
- // Upgrade all the Crosstab widgets
- var upgradeVisIds = ['crosstab', 'JQGrid'];
- var upgradeSlotNames = ['values', 'grid_cols'];
- var promises = [];
- var unsupportedCFs = [];
- _.each(spec.widgets, function (widget) {
- if (widget && widget.visId && upgradeVisIds.indexOf(widget.visId) !== -1) {
- var dataItems = widget.data && widget.data.dataViews && widget.data.dataViews.length ? widget.data.dataViews[0].dataItems : [];
- // Find color heat dataItem
- var heatSlot = _this2._getSlot(widget.slotmapping.slots, ['heat']);
- var heatId = heatSlot && heatSlot.dataItems[0];
- var heatDataItem = dataItems.find(function (dataItem) {
- return dataItem.id === heatId;
- });
- if (heatDataItem) {
- var widgetUISpecs = _this2._getObjects(spec.layout, 'id', widget.id);
- var widgetUISpec = widgetUISpecs.length > 0 ? widgetUISpecs[0] : undefined;
- var conditionalFormats = [];
- if (widgetUISpec) {
- widgetUISpec.content = widgetUISpec.content || {};
- widgetUISpec.content.conditionalFormatting = {
- conditionalFormats: conditionalFormats
- };
- }
- var useNewHeatId = false;
- var getDataItemTypes = void 0;
- var upgradeSlotContainsHeat = false;
- var upgradeSlotDataItems = [];
- if (widget.visId === 'JQGrid') {
- getDataItemTypes = _this2._getDataItemTypes(widget);
- } else {
- getDataItemTypes = Promise.resolve();
- }
- promises.push(getDataItemTypes.then(function (dataItemTypes) {
- var upgradeSlot = _this2._getSlot(widget.slotmapping.slots, upgradeSlotNames);
- if (upgradeSlot) {
- upgradeSlot.dataItems.forEach(function (id) {
- dataItems.forEach(function (dataItem) {
- if (dataItem.id == id) {
- upgradeSlotDataItems.push(dataItem);
- if (dataItem.itemId === heatDataItem.itemId) {
- upgradeSlotContainsHeat = true;
- }
- }
- });
- });
- }
- var firstFactInUpgradeSlotIdx = dataItemTypes && upgradeSlotDataItems.findIndex(function (dataItem) {
- return dataItemTypes[dataItem.itemId] === 'fact';
- });
- var upgradeCFPromises = [];
- upgradeSlotDataItems.forEach(function (dataItem, idx) {
- if (useNewHeatId) {
- //We need a 1-1 relationship
- heatDataItem = _this2._cloneDataItemWithNewId(heatDataItem);
- heatSlot.dataItems.push(heatDataItem.id);
- dataItems.push(heatDataItem);
- }
- useNewHeatId = true;
- var currentType = dataItemTypes && dataItemTypes[dataItem.itemId];
- var upgradeCFPromise = Promise.resolve();
- if (widget.visId === 'crosstab' || currentType === 'fact') {
- upgradeCFPromise = _this2._createConditionFormat(widget, conditionalFormats, dataItem, heatDataItem, upgradeSlotContainsHeat);
- } else if (upgradeSlotContainsHeat) {
- if (dataItem.itemId === heatDataItem.itemId) {
- unsupportedCFs.push({
- widget: widget.visId,
- dataItem: dataItem
- });
- }
- } else {
- var nextIdx = idx + 1;
- if (firstFactInUpgradeSlotIdx === -1 || firstFactInUpgradeSlotIdx === nextIdx) {
- //Either there are no fact columns or It's a non grouped attribute. (Groups aren't colored)
- unsupportedCFs.push({
- widget: widget.id,
- dataItem: dataItem
- });
- }
- }
- upgradeCFPromises.push(upgradeCFPromise);
- });
- return Promise.all(upgradeCFPromises);
- }));
- }
- }
- });
- return Promise.all(promises).then(function () {
- if (unsupportedCFs.length) {
- var msg = StringResources.get('unsupportedConditionalFormattingEncountered');
- var logger = _this2.data.logger;
- if (logger) {
- logger.warn(msg, unsupportedCFs);
- }
- _this2._showUnsupportedCFMessage(msg);
- }
- return spec;
- });
- });
- };
- CrosstabTable_VCF_Upgrade.prototype._showUnsupportedCFMessage = function _showUnsupportedCFMessage(msg) {
- this.data.dashboardApi.showToast(msg, {
- type: 'warning',
- preventDuplicates: true
- });
- };
- CrosstabTable_VCF_Upgrade.prototype._createConditionFormat = function _createConditionFormat(widget, conditionalFormats, dataItem, heatDataItem, upgradeSlotContainsHeat) {
- var conditionalFormat = {
- 'dataItemId': dataItem.itemId,
- 'paletteId': 'condColorPalette0',
- 'heatId': heatDataItem.id,
- 'scale': VCF_RuleConstants.RULE_SCALES.NUMERIC,
- 'rules': [],
- 'backgroundOpacity': 100
- };
- var colors = widget.conditions && widget.conditions.palette ? widget.conditions.palette.colors : [];
- var promise = void 0;
- if (!upgradeSlotContainsHeat || heatDataItem.itemId === dataItem.itemId) {
- promise = this._updateTargetConditionalFormat(widget, colors, conditionalFormat);
- } else {
- promise = Promise.resolve();
- }
- return promise.then(function () {
- conditionalFormats.push(conditionalFormat);
- });
- };
- CrosstabTable_VCF_Upgrade.prototype._cloneDataItemWithNewId = function _cloneDataItemWithNewId(dataItem) {
- var clonedItem = JSON.parse(JSON.stringify(dataItem));
- clonedItem.id = UniqueHashIdBuilder.createUniqueHashId(clonedItem.id + _.uniqueId(), []);
- return clonedItem;
- };
- // Downgrade is not available in CA
- CrosstabTable_VCF_Upgrade.prototype.down = function down(spec) {
- return Promise.resolve(spec);
- };
- CrosstabTable_VCF_Upgrade.prototype._getObjects = function _getObjects(obj, key, val) {
- var objects = [];
- for (var i in obj) {
- if (!obj.hasOwnProperty(i)) continue;
- if (_typeof(obj[i]) == 'object') {
- objects = objects.concat(this._getObjects(obj[i], key, val));
- } else if (i == key && obj[i] == val) {
- objects.push(obj);
- }
- }
- return objects;
- };
- CrosstabTable_VCF_Upgrade.prototype._getSlot = function _getSlot(slots, slotNamesFilter) {
- return _.find(slots, function (slot) {
- return slotNamesFilter.indexOf(slot.name) !== -1;
- });
- };
- CrosstabTable_VCF_Upgrade.prototype._updateTargetConditionalFormat = function _updateTargetConditionalFormat(widget, colors, conditionalFormat) {
- var _this3 = this;
- if (_.isEmpty(colors)) {
- return Promise.resolve();
- }
- var condColorPaletteProperty = this._getObjects(widget.properties, 'id', 'condColorPalette');
- if (condColorPaletteProperty && condColorPaletteProperty.length) {
- conditionalFormat.paletteId = condColorPaletteProperty[0].value;
- }
- var condColorOrderProperty = this._getObjects(widget.properties, 'id', 'condColorOrder');
- var isPaletteReversed = condColorOrderProperty && condColorOrderProperty.length ? condColorOrderProperty[0].value === 'DarkerForLowerValue' : false;
- var rule = null;
- var rangeRuleTemplate = {
- 'ruleType': VCF_RuleConstants.RULE_TYPES.RANGE,
- 'startCondition': {
- 'type': VCF_RuleConstants.RULE_OPERATORS.greaterThanOrEqualTo,
- 'value': undefined
- },
- 'ruleStyle': {
- 'fillColor': '',
- 'textColor': '',
- 'shape': ''
- },
- 'operator': VCF_RuleConstants.RangeOperators.and,
- 'endCondition': {
- 'type': VCF_RuleConstants.RULE_OPERATORS.lessThan,
- 'value': undefined
- }
- };
- return this.data.dashboardApi.getFeature('Palette').getPalette(conditionalFormat.paletteId).then(function (condColorPalette) {
- var paletteColors = condColorPalette.content.fills;
- return paletteColors.length;
- }, function () {
- return defaultPaletteLength;
- }).then(function (paletteLength) {
- var rangesLength = colors.length - 1;
- var colorIndexMap = _this3._buildIndexMap(paletteLength, rangesLength);
- if (isPaletteReversed) {
- colorIndexMap.reverse();
- }
- var defaultFillColors = _.range(paletteLength).map(function (index) {
- return 'color' + index;
- });
- for (var i = 0; i < rangesLength; i++) {
- rule = _.deepClone(rangeRuleTemplate);
- rule.startCondition.value = colors[i].value;
- rule.endCondition.value = colors[i + 1].value;
- if (i < defaultFillColors.length) {
- rule.ruleStyle.fillColor = defaultFillColors[colorIndexMap[i]];
- } else {
- rule.ruleStyle.fillColor = defaultFillColors[colorIndexMap[i % defaultFillColors.length]];
- }
- conditionalFormat.rules.push(rule);
- }
- });
- };
- /**
- * @description this method is a copy of _buildIndexMap from js/features/widget/conditionalFormatting/api/impl/DeprecatedConditionalFormatting.js
- * @param {int} paletteLength the length of a palette
- * @param {*} rangesLength the number of ranges
- */
- CrosstabTable_VCF_Upgrade.prototype._buildIndexMap = function _buildIndexMap(paletteLength, rangesLength) {
- if (paletteLength <= rangesLength) {
- return _.range(paletteLength);
- }
- if (paletteLength === defaultPaletteLength) {
- var fiveColorPalleteMaps = [[4], // one color
- [0, 4], // two colors
- [0, 2, 4], // three colors
- [0, 1, 2, 4], // four colors
- [0, 1, 2, 3, 4] // five colors
- ];
- var colorIndexMap = fiveColorPalleteMaps[rangesLength - 1];
- return colorIndexMap;
- }
- if (rangesLength === 1) {
- return [Math.floor(paletteLength / 2)];
- }
- var step = (paletteLength - 1) / rangesLength;
- var indexMap = [0];
- for (var i = 2; i < rangesLength; i++) {
- indexMap.push(Math.floor(step * i));
- }
- indexMap.push(paletteLength - 1);
- return indexMap;
- };
- CrosstabTable_VCF_Upgrade.prototype._getDataItemTypes = function _getDataItemTypes(widget) {
- var dataTypes = {};
- var dataViews = widget.data && widget.data.dataViews;
- var modelRef = dataViews && dataViews[0] ? dataViews[0].modelRef : null;
- var promise = !modelRef || !dataViews ? Promise.resolve(null) : this._getModule(modelRef);
- return promise.then(function (module) {
- if (module) {
- _.each(dataViews, function (dataView) {
- var dataItems = dataView.dataItems;
- if (dataItems) {
- _.each(dataItems, function (dataItem) {
- // get metadata with column id
- var mdc = module.getMetadataColumn(dataItem.itemId);
- dataTypes[dataItem.itemId] = mdc.getType();
- });
- }
- });
- }
- return dataTypes;
- });
- };
- CrosstabTable_VCF_Upgrade.prototype._getModule = function _getModule(modelRef) {
- return this.dataSourcesService.getModule(modelRef);
- };
- return CrosstabTable_VCF_Upgrade;
- }(UpgradeBase);
- return new CrosstabTable_VCF_Upgrade();
- });
- //# sourceMappingURL=CrosstabTable_VCF_Upgrade.js.map
|