DashboardTheme.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. 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; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Business Analytics (C) Copyright IBM Corp. 2020
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. 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) {
  10. var SYSTEM_THEME_COLLECTION_ID = 'com.ibm.bi.dashboard.dashboardTheme.system';
  11. /**
  12. * @class
  13. * @implements DashboardThemeAPI
  14. * @hideconstructor
  15. * @classdesc default implementation of the DashboardThemeAPI
  16. */
  17. var DashboardTheme = function () {
  18. function DashboardTheme() {
  19. _classCallCheck(this, DashboardTheme);
  20. this._themes = {};
  21. this._getThemeDefinition = null;
  22. this._themeProvider = this;
  23. this._api = APIFactory.createAPI(this, [DashboardThemeAPI]);
  24. }
  25. /**
  26. * it is required in order to be defined as a dashboard feature
  27. * @returns {Object} instance of the API implementation
  28. */
  29. DashboardTheme.prototype.getAPI = function getAPI() {
  30. return this._api;
  31. };
  32. DashboardTheme.prototype.initialize = function initialize(glassContext) {
  33. var _this = this;
  34. this._logger = glassContext.getCoreSvc('.Logger');
  35. return glassContext.appController.findCollection(SYSTEM_THEME_COLLECTION_ID).then(function (collection) {
  36. if (Array.isArray(collection) && collection.length > 0) {
  37. var themeProviderModule = collection[0].class;
  38. return ClassFactory.loadModule(themeProviderModule).then(function (ThemeProvider) {
  39. _this._themeProvider = APIFactory.createAPI(new ThemeProvider(), [ThemeProviderAPI]);
  40. }).catch(function (error) {
  41. return _this._logger.error(error);
  42. });
  43. }
  44. });
  45. };
  46. DashboardTheme.prototype.loadThemeDefinition = function loadThemeDefinition(themeName) {
  47. return ClassFactory.loadModule('text!dashboard-core/js/lib/@waca/dashboard-common/dist/themes/' + themeName + '.json').then(JSON.parse.bind(JSON));
  48. };
  49. DashboardTheme.prototype.getThemeDefinition = function getThemeDefinition() {
  50. var _this2 = this;
  51. var themeName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'defaultTheme';
  52. if (this._themes[themeName]) {
  53. this._themeDefinition = this._themes[themeName];
  54. return Promise.resolve(this._themes[themeName]);
  55. }
  56. return this._themeProvider.loadThemeDefinition(themeName).catch(this._handleLoadingError.bind(this, themeName)).then(this._processThemeDefinition.bind(this, themeName)).then(function (themeDefinition) {
  57. _this2._setThemeDefinition(themeName, themeDefinition);
  58. return _this2._themeDefinition;
  59. });
  60. };
  61. DashboardTheme.prototype._processThemeDefinition = function _processThemeDefinition(themeName, contributedThemeDefinition) {
  62. if (this._themeProvider !== this) {
  63. return this.loadThemeDefinition(themeName).then(this._mergeDeep.bind(this, contributedThemeDefinition));
  64. }
  65. return contributedThemeDefinition;
  66. };
  67. DashboardTheme.prototype._handleLoadingError = function _handleLoadingError(themeName, error) {
  68. this._logger.error(error);
  69. if (this === this._themeProvider) {
  70. throw error;
  71. }
  72. return this.loadThemeDefinition(themeName);
  73. };
  74. DashboardTheme.prototype._isObject = function _isObject(item) {
  75. return item && (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && !Array.isArray(item);
  76. };
  77. /**
  78. * Merges the contributed theme definition with the default loaded theme definition to fill in missing attributes
  79. * Also discards attributes of the contributed theme def if it doesn't match the type with the default theme def
  80. * and keeps the attribute from the default theme def
  81. * @param {Object} source - the contributed theme definition
  82. * @param {Object} target - the default theme that's loaded to fill in missing attributes
  83. * @return {Object} the merged object from source and target
  84. */
  85. DashboardTheme.prototype._mergeDeep = function _mergeDeep(source, target) {
  86. var _this3 = this;
  87. var output = Object.assign({}, target);
  88. if (this._isObject(target) && this._isObject(source)) {
  89. Object.keys(source).forEach(function (key) {
  90. if (!source[key]) {
  91. _this3._logger.info('No value corresponding to the key: ' + key);
  92. } else {
  93. if (!target[key]) {
  94. var _Object$assign;
  95. Object.assign(output, (_Object$assign = {}, _Object$assign[key] = source[key], _Object$assign));
  96. } else {
  97. if (_typeof(source[key]) !== _typeof(target[key])) {
  98. _this3._logger.warn('Contributed theme attribute: ' + key + ' is not of the right type');
  99. } else {
  100. if (_this3._isObject(source[key])) {
  101. output[key] = _this3._mergeDeep(source[key], target[key]);
  102. } else {
  103. var _Object$assign2;
  104. Object.assign(output, (_Object$assign2 = {}, _Object$assign2[key] = source[key], _Object$assign2));
  105. }
  106. }
  107. }
  108. }
  109. });
  110. }
  111. return output;
  112. };
  113. DashboardTheme.prototype._setThemeDefinition = function _setThemeDefinition(themeName, themeDefinition) {
  114. this._themes[themeName] = APIFactory.createAPI(new ThemeDefinition(themeDefinition), [ThemeDefinitionAPI]);
  115. this._themeDefinition = this._themes[themeName];
  116. };
  117. return DashboardTheme;
  118. }();
  119. return DashboardTheme;
  120. });
  121. //# sourceMappingURL=DashboardTheme.js.map