123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: BI Dashboard
- *| (C) Copyright IBM Corp. 2018
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- define(['underscore'], function (_) {
- var ConditionalPaletteUpgradeHelper = function () {
- function ConditionalPaletteUpgradeHelper() {
- _classCallCheck(this, ConditionalPaletteUpgradeHelper);
- }
- /**
- * Perform upgrade
- *
- * @param {object} spec - spec to perform upgrade on
- *
- * @return {Promise} Promise to be resolved when upgrade performed
- */
- ConditionalPaletteUpgradeHelper.prototype.upgrade = function upgrade(spec, dashboardApi) {
- var _this = this;
- return dashboardApi.getGlassSvc('.DashboardTheme').then(function (dashboardThemeService) {
- return dashboardThemeService.getThemeDefinition(spec.theme).then(function (themeDefinition) {
- // Get all the conditional palettes for the theme.
- var conditionalPalettes = themeDefinition.getPalettes('ConditionalPalette');
- for (var widgetId in spec.widgets) {
- var widget = spec.widgets[widgetId];
- _this._updateWidgetConditionalPalette(widget, conditionalPalettes, dashboardApi);
- }
- return spec;
- });
- });
- };
- /**
- * If the widget has a conditional palette, get the palette name by comparing the hex values that are saved in the spec
- */
- ConditionalPaletteUpgradeHelper.prototype._updateWidgetConditionalPalette = function _updateWidgetConditionalPalette(widget, conditionalPalettes, dashboardApi) {
- if (!widget || !widget.conditions || !widget.conditions.palette) {
- return;
- }
- var widgetPaletteColors = widget.conditions.palette.colors;
- // If we already have an Endor spec
- if (!widgetPaletteColors || widgetPaletteColors.length < 1 || !widgetPaletteColors[0].bgcolor) {
- return;
- }
- var paletteColors = widgetPaletteColors.map(function (_ref) {
- var bgcolor = _ref.bgcolor;
- return bgcolor;
- } //pluck bgcolor
- ).filter(function (color) {
- return color && color.length;
- } //filter out invalid colours
- ).map(function (color) {
- return color.toUpperCase();
- } //to allow case-insensitive search
- );
- var isWidgetPaletteAPattern = widgetPaletteColors[0].pattern ? true : false;
- var paletteId = null;
- for (var _iterator = conditionalPalettes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
- var _ref2;
- if (_isArray) {
- if (_i >= _iterator.length) break;
- _ref2 = _iterator[_i++];
- } else {
- _i = _iterator.next();
- if (_i.done) break;
- _ref2 = _i.value;
- }
- var conditionalPalette = _ref2;
- if (isWidgetPaletteAPattern) {
- if (conditionalPalette.fills[0].pattern) {
- paletteId = conditionalPalette.id;
- break;
- }
- } else {
- var foundAllColors = true;
- var conditionalPaletteColours = conditionalPalette.fills.filter(function (fill) {
- return _.isString(fill);
- } //we only attempt to match against strings
- ).map(function (fill) {
- return fill.toUpperCase();
- } //to allow case-insensitive search
- );
- // Since the user could of removed any number of colors from the palette we can't simply do a direct match.
- // Loops through all the colors from the widget spec and see if they're all found in the palette. If they
- // are concider it a match
- for (var _iterator2 = paletteColors, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
- var _ref3;
- if (_isArray2) {
- if (_i2 >= _iterator2.length) break;
- _ref3 = _iterator2[_i2++];
- } else {
- _i2 = _iterator2.next();
- if (_i2.done) break;
- _ref3 = _i2.value;
- }
- var color = _ref3;
- if (conditionalPaletteColours.indexOf(color) === -1) {
- foundAllColors = false;
- break;
- }
- }
- if (foundAllColors) {
- paletteId = conditionalPalette.id;
- break;
- }
- }
- }
- // Should never happen, we should always find a match. But log it as an error if we don't
- if (!paletteId) {
- dashboardApi.getGlassCoreSvc('.Logger').error('Could not find a match for the conditional palette', widget);
- }
- // Make sure we have a properties array
- if (!widget.properties) {
- widget.properties = [];
- }
- widget.properties.push({ id: 'condColorPalette', value: paletteId });
- };
- return ConditionalPaletteUpgradeHelper;
- }();
- return ConditionalPaletteUpgradeHelper;
- });
- //# sourceMappingURL=ConditionalPaletteUpgradeHelper.js.map
|