VisDefinitions.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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; }
  4. 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; }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Business Analytics (C) Copyright IBM Corp. 2018, 2020
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. /**
  11. * @class VisDefinitionsAPI
  12. * @hideconstructor
  13. *
  14. * @classdesc
  15. * Represents the API to access the visualization definitions that are registered in the dashboard application
  16. */
  17. define(['../../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../VisDefinitionsAPI', '../InternalVisDefinitionsAPI', '../VisDefinitionsAPISpec', '../../../../../api/VisDefinitionAPI', '../../../../../api/impl/VisDefinition', '../../../../../visualizations/definitions/VisDefinitionManager', '../../../../../widgets/livewidget/util/VisUtil'], function (APIFactory, VisDefinitionsAPI, InternalVisDefinitionsAPI, VisDefinitionsAPISpec, VisDefinitionAPI, VisDefinitionImpl, VisDefinitionManager, VisUtil) {
  18. var VisDefinitions = function (_VisDefinitionsAPISpe) {
  19. _inherits(VisDefinitions, _VisDefinitionsAPISpe);
  20. function VisDefinitions(options) {
  21. _classCallCheck(this, VisDefinitions);
  22. var _this = _possibleConstructorReturn(this, _VisDefinitionsAPISpe.call(this));
  23. _this.dashboard = options.features.API;
  24. // TODO : VisDefinitionManager should go away and the implementation should belong to this feature implementation
  25. _this.visDefinitionManager = new VisDefinitionManager({
  26. dashboardApi: _this.dashboard
  27. });
  28. _this.internalAPI = APIFactory.createAPI(_this, [VisDefinitionsAPI, InternalVisDefinitionsAPI]);
  29. _this.api = APIFactory.createAPI(_this, [VisDefinitionsAPI]);
  30. _this._definitionAPIMap = null;
  31. _this._definitionsJSON = null;
  32. _this._modifiers = [];
  33. return _this;
  34. }
  35. VisDefinitions.prototype.getAPI = function getAPI(type) {
  36. if (type === 'internal') {
  37. return this.internalAPI;
  38. } else {
  39. return this.api;
  40. }
  41. };
  42. VisDefinitions.prototype.destroy = function destroy() {
  43. if (this._definitionAPIMap) {
  44. for (var id in this._definitionAPIMap.implById) {
  45. this._definitionAPIMap.implById[id].destroy();
  46. }
  47. }
  48. this._definitionsJSON = null;
  49. this._definitionAPIMap = null;
  50. this.dashboard = null;
  51. this.visDefinitionManager = null;
  52. this.internalAPI = null;
  53. this.api = null;
  54. this._modifiers = null;
  55. };
  56. VisDefinitions.prototype.initialize = function initialize() {
  57. var _this2 = this;
  58. if (this._definitionAPIMap) {
  59. return Promise.resolve(this._definitionAPIMap);
  60. } else {
  61. return this.visDefinitionManager.getAll(this.dashboard).then(function (definitions) {
  62. _this2._definitionsJSON = definitions;
  63. return _this2._createDefinitionAPIMap(definitions);
  64. });
  65. }
  66. };
  67. VisDefinitions.prototype._createDefinitionAPIMap = function _createDefinitionAPIMap() {
  68. var _this3 = this;
  69. var definitions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  70. var conditionalFormattingFeature = !this.dashboard.getGlassCoreSvc('.FeatureChecker').checkValue('dashboard', 'xtabcondFormat', 'disabled');
  71. this._definitionAPIMap = definitions.jsonDefinitions.reduce(function (map, definition) {
  72. definition = _this3.modifyDefinition(definition);
  73. var id = definition.id;
  74. if (conditionalFormattingFeature && (id === 'crosstab' || id === 'JQGrid')) {
  75. definition = JSON.parse(JSON.stringify(definition));
  76. definition.dataSlots.forEach(function (slot) {
  77. if (slot.id === 'heat') {
  78. slot.hidden = true;
  79. }
  80. });
  81. }
  82. var impl = new VisDefinitionImpl(definition, _this3.visDefinitionManager);
  83. var definitionAPI = APIFactory.createAPI(impl, [VisDefinitionAPI]);
  84. _this3._addToDefinitionMap(map, impl, definitionAPI);
  85. return map;
  86. }, { implById: {}, byId: {}, byType: {}, list: [] });
  87. return this._definitionAPIMap;
  88. };
  89. VisDefinitions.prototype._addToDefinitionMap = function _addToDefinitionMap(definitionAPIMap, definitionImpl, definitionApi) {
  90. var id = definitionApi.getId();
  91. definitionAPIMap.implById[id] = definitionImpl;
  92. definitionAPIMap.byId[id] = definitionApi;
  93. definitionAPIMap.byType[definitionApi.getType() || id] = definitionApi;
  94. definitionAPIMap.list.push(definitionApi);
  95. // ensure the events are propagated to the parent
  96. APIFactory.setParentChildRelation(this, definitionImpl);
  97. };
  98. VisDefinitions.prototype.getList = function getList() {
  99. var list = this._definitionAPIMap && this._definitionAPIMap.list || [];
  100. /**
  101. * livewidget-cleanup [todo] need to revisit here:
  102. * should we always filter out the definition with error state or just for empty definition ?
  103. * currently, we only set error for empty definition..
  104. */
  105. return list.filter(function (definition) {
  106. return !definition.getState().getError();
  107. });
  108. };
  109. VisDefinitions.prototype.loadById = function loadById(id) {
  110. return this.visDefinitionManager.get(id);
  111. };
  112. VisDefinitions.prototype.getById = function getById(id) {
  113. var visDefinition = this._definitionAPIMap && this._definitionAPIMap.byId[id];
  114. if (!visDefinition) {
  115. var visDefinitionImpl = new VisDefinitionImpl({ id: id, type: id }, this.visDefinitionManager);
  116. visDefinition = visDefinitionImpl.getAPI();
  117. this._addToDefinitionMap(this._definitionAPIMap, visDefinitionImpl, visDefinition);
  118. visDefinitionImpl.setError(VisUtil.createVisDefinitionLoadingError(id));
  119. }
  120. return visDefinition;
  121. };
  122. VisDefinitions.prototype.getByType = function getByType(type) {
  123. return this._definitionAPIMap && this._definitionAPIMap.byType[type];
  124. };
  125. VisDefinitions.prototype.refresh = function refresh() {
  126. var _this4 = this;
  127. return this.visDefinitionManager.refreshProviders(this.dashboard).then(function (visDefinitions) {
  128. _this4._definitionsJSON = visDefinitions;
  129. _this4._createDefinitionAPIMap(visDefinitions);
  130. });
  131. };
  132. // Internal API
  133. VisDefinitions.prototype.getAll = function getAll() {
  134. return this.visDefinitionManager.getAll(this.dashboard);
  135. };
  136. // Internal API
  137. VisDefinitions.prototype.getRawDefinition = function getRawDefinition(id) {
  138. var definitionImpl = this._definitionAPIMap.implById[id];
  139. var error = definitionImpl && definitionImpl.getState().getError();
  140. if (!error && !definitionImpl) {
  141. error = VisUtil.createVisDefinitionLoadingError(id);
  142. }
  143. if (error) {
  144. throw error;
  145. }
  146. return this._definitionAPIMap.implById[id].visSpec;
  147. };
  148. VisDefinitions.prototype.modifyDefinition = function modifyDefinition(definition) {
  149. if (this._modifiers.length > 0) {
  150. // The definition object may be frozen
  151. definition = JSON.parse(JSON.stringify(definition));
  152. this._modifiers.forEach(function (modifier) {
  153. try {
  154. modifier.modifyDefinition(definition);
  155. } catch (e) {
  156. // a bad extension.. ignore it.
  157. }
  158. });
  159. }
  160. return definition;
  161. };
  162. VisDefinitions.prototype.registerModifier = function registerModifier(modifier) {
  163. if (this._modifiers.indexOf(modifier) === -1 && typeof modifier.modifyDefinition === 'function') {
  164. this._modifiers.push(modifier);
  165. if (this._definitionsJSON) {
  166. this._createDefinitionAPIMap(this._definitionsJSON);
  167. }
  168. }
  169. };
  170. VisDefinitions.prototype.deregisterModifier = function deregisterModifier(modifier) {
  171. var idx = this._modifiers.indexOf(modifier);
  172. if (idx !== -1) {
  173. this._modifiers.splice(idx, 1);
  174. if (this._definitionsJSON) {
  175. this._createDefinitionAPIMap(this._definitionsJSON);
  176. }
  177. }
  178. };
  179. return VisDefinitions;
  180. }(VisDefinitionsAPISpec);
  181. return VisDefinitions;
  182. });
  183. //# sourceMappingURL=VisDefinitions.js.map