'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"); } } /** * Licensed Materials - Property of IBM * IBM Business Analytics (C) Copyright IBM Corp. 2020 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', './DashboardThemeAPI', '../../../lib/@waca/core-client/js/core-client/utils/ClassFactory', '../../../dashboard/data/ThemeDefinition', '../../../dashboard/data/ThemeDefinitionAPI', './ThemeProviderAPI'], function (APIFactory, DashboardThemeAPI, ClassFactory, ThemeDefinition, ThemeDefinitionAPI, ThemeProviderAPI) { var SYSTEM_THEME_COLLECTION_ID = 'com.ibm.bi.dashboard.dashboardTheme.system'; /** * @class * @implements DashboardThemeAPI * @hideconstructor * @classdesc default implementation of the DashboardThemeAPI */ var DashboardTheme = function () { function DashboardTheme() { _classCallCheck(this, DashboardTheme); this._themes = {}; this._getThemeDefinition = null; this._themeProvider = this; this._api = APIFactory.createAPI(this, [DashboardThemeAPI]); } /** * it is required in order to be defined as a dashboard feature * @returns {Object} instance of the API implementation */ DashboardTheme.prototype.getAPI = function getAPI() { return this._api; }; DashboardTheme.prototype.initialize = function initialize(glassContext) { var _this = this; this._logger = glassContext.getCoreSvc('.Logger'); return glassContext.appController.findCollection(SYSTEM_THEME_COLLECTION_ID).then(function (collection) { if (Array.isArray(collection) && collection.length > 0) { var themeProviderModule = collection[0].class; return ClassFactory.loadModule(themeProviderModule).then(function (ThemeProvider) { _this._themeProvider = APIFactory.createAPI(new ThemeProvider(), [ThemeProviderAPI]); }).catch(function (error) { return _this._logger.error(error); }); } }); }; DashboardTheme.prototype.loadThemeDefinition = function loadThemeDefinition(themeName) { return ClassFactory.loadModule('text!dashboard-core/js/lib/@waca/dashboard-common/dist/themes/' + themeName + '.json').then(JSON.parse.bind(JSON)); }; DashboardTheme.prototype.getThemeDefinition = function getThemeDefinition() { var _this2 = this; var themeName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'defaultTheme'; if (this._themes[themeName]) { this._themeDefinition = this._themes[themeName]; return Promise.resolve(this._themes[themeName]); } return this._themeProvider.loadThemeDefinition(themeName).catch(this._handleLoadingError.bind(this, themeName)).then(this._processThemeDefinition.bind(this, themeName)).then(function (themeDefinition) { _this2._setThemeDefinition(themeName, themeDefinition); return _this2._themeDefinition; }); }; DashboardTheme.prototype._processThemeDefinition = function _processThemeDefinition(themeName, contributedThemeDefinition) { if (this._themeProvider !== this) { return this.loadThemeDefinition(themeName).then(this._mergeDeep.bind(this, contributedThemeDefinition)); } return contributedThemeDefinition; }; DashboardTheme.prototype._handleLoadingError = function _handleLoadingError(themeName, error) { this._logger.error(error); if (this === this._themeProvider) { throw error; } return this.loadThemeDefinition(themeName); }; DashboardTheme.prototype._isObject = function _isObject(item) { return item && (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && !Array.isArray(item); }; /** * Merges the contributed theme definition with the default loaded theme definition to fill in missing attributes * Also discards attributes of the contributed theme def if it doesn't match the type with the default theme def * and keeps the attribute from the default theme def * @param {Object} source - the contributed theme definition * @param {Object} target - the default theme that's loaded to fill in missing attributes * @return {Object} the merged object from source and target */ DashboardTheme.prototype._mergeDeep = function _mergeDeep(source, target) { var _this3 = this; var output = Object.assign({}, target); if (this._isObject(target) && this._isObject(source)) { Object.keys(source).forEach(function (key) { if (!source[key]) { _this3._logger.info('No value corresponding to the key: ' + key); } else { if (!target[key]) { var _Object$assign; Object.assign(output, (_Object$assign = {}, _Object$assign[key] = source[key], _Object$assign)); } else { if (_typeof(source[key]) !== _typeof(target[key])) { _this3._logger.warn('Contributed theme attribute: ' + key + ' is not of the right type'); } else { if (_this3._isObject(source[key])) { output[key] = _this3._mergeDeep(source[key], target[key]); } else { var _Object$assign2; Object.assign(output, (_Object$assign2 = {}, _Object$assign2[key] = source[key], _Object$assign2)); } } } } }); } return output; }; DashboardTheme.prototype._setThemeDefinition = function _setThemeDefinition(themeName, themeDefinition) { this._themes[themeName] = APIFactory.createAPI(new ThemeDefinition(themeDefinition), [ThemeDefinitionAPI]); this._themeDefinition = this._themes[themeName]; }; return DashboardTheme; }(); return DashboardTheme; }); //# sourceMappingURL=DashboardTheme.js.map