VisualizationCreator.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2014, 2020
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['./visapi/VisAPI', './visapi/support/VisRenderSupport', './visapi/support/VisConditionsSupport', './visapi/support/VisQuerySupport', '../../visualizations/renderer/VisController', // CAREFUL: This VisController is the View/Interactivity Controller.
  13. '../../visualizations/renderer/Vis2SelectionController', '../../lib/@waca/core-client/js/core-client/ui/core/Class', '../../lib/@waca/dashboard-common/dist/api/Error', './nls/StringResources', 'underscore', './modelapis/modelmanagers/VisModelManagerLeftover'], function (VisAPI, VisRenderSupport, VisConditionsSupport, VisQuerySupport, VisInteractivityController, Vis2SelectionController, Class, APIError, StringResources, _, VisModelManagerLeftover) {
  14. 'use strict';
  15. /**
  16. * INTENT: VisualizationCreator creates the entities and support objects for a live widget visualization.
  17. */
  18. var VisualizationCreator = Class.extend({
  19. // constructor
  20. init: function init(params) {
  21. VisualizationCreator.inherited('init', this, arguments);
  22. this.ownerWidget = params.ownerWidget;
  23. this.widgetModel = params.widgetModel;
  24. this.dashboardApi = params.dashboardAPI;
  25. this.logger = params.logger;
  26. //TODO: MARK=Should investigate what params are actually needed....
  27. this.isPreview = params.initialConfigJSON.isPreview ? params.initialConfigJSON.isPreview : false; //TODO: Check if this changes after init.
  28. this.content = params.content;
  29. //Legacy manager created by the visualization feature
  30. this.visModelManager = params.legacyManagers.visModelManager;
  31. this.dataModel = params.legacyManagers.dataModel;
  32. this.visFilterSupport = params.legacyManagers.visFilterSupport;
  33. this.visPropertySupport = params.legacyManagers.visPropertySupport;
  34. this.displayTypeManager = params.legacyManagers.visDisplayTypeManager;
  35. this.recommender = params.legacyManagers.recommender;
  36. this.pageContext = null;
  37. this.synchronizeData = null;
  38. this.visDefinitions = this.dashboardApi.getFeature('VisDefinitions.internal');
  39. this.visualizationFeature = this.content.getFeature('Visualization');
  40. this.contentFeatureLoader = params.contentFeatureLoader;
  41. },
  42. destroy: function destroy() {
  43. if (this.visAPI) {
  44. try {
  45. this.visAPI.destroy();
  46. } catch (error) {
  47. // Do nothing in destroy.
  48. } finally {
  49. this.visAPI = null;
  50. }
  51. }
  52. this.visModelManager = null;
  53. this.visFilterSupport = null;
  54. this.visConditionsSupport = null;
  55. this.visPropertySupport = null;
  56. this.displayTypeManager = null;
  57. this.recommender = null;
  58. this.visDefinitions = null;
  59. this.visualizationFeature = null;
  60. this.contentFeatureLoader = null;
  61. this.pageContext = null;
  62. this.synchronizeData = null;
  63. this.content = null;
  64. this.ownerWidget = null;
  65. this.widgetModel = null;
  66. this.dashboardApi = null;
  67. this.logger = null;
  68. if (this.visRenderSupport) {
  69. this.visRenderSupport.remove();
  70. this.visRenderSupport = null;
  71. }
  72. VisualizationCreator.inherited('destroy', this, arguments);
  73. },
  74. setPageContext: function setPageContext(pageContext) {
  75. this.pageContext = pageContext;
  76. },
  77. setSynchronizeData: function setSynchronizeData(synchronizeData) {
  78. this.synchronizeData = synchronizeData;
  79. },
  80. _validateVisDefinition: function _validateVisDefinition(visId) {
  81. var visDefinition = this.visDefinitions.getById(visId);
  82. var visDefinitionReady = void 0;
  83. if (visDefinition.getState().getError()) {
  84. // attempt to refresh the visualization definition if it had previous failed
  85. visDefinitionReady = visDefinition.refresh();
  86. } else {
  87. visDefinitionReady = Promise.resolve();
  88. }
  89. return visDefinitionReady;
  90. },
  91. createVisualization: function createVisualization() {
  92. var _this = this;
  93. if (!this.visualizationFeature.getDataSource()) {
  94. this.createEmptyVis();
  95. return this.ownerWidget.readyDfd.promise;
  96. } else {
  97. return this._validateVisDefinition(this.widgetModel.visId).then(function () {
  98. _this._initializeModelManagers();
  99. // TODO: make a better fix for this, the widget waiting for the canvas to be ready sounds like an anti-pattern
  100. // the synchronize data should be revisited
  101. _this.dashboardApi.getCanvasWhenReady().then(function () {
  102. return _this.synchronizeData.loadBrushedContext(_this.pageContext, _this.visualizationFeature, _this.visAPI).then(function () {
  103. _this.ownerWidget.readyDfd.resolve();
  104. });
  105. });
  106. }).catch(function (error) {
  107. _this._showLoadingVisualizationNotFound(_this.widgetModel.visId, error);
  108. throw error;
  109. });
  110. }
  111. },
  112. _showLoadingVisualizationNotFound: function _showLoadingVisualizationNotFound(visId, error) {
  113. var hasParams = error instanceof APIError && error.getParams();
  114. var errorMessage = hasParams ? error.getParams().errorInfo.errorMessage : StringResources.get('dwErrorLoadingVisualizationNotFound');
  115. var params = {
  116. errorInfo: {
  117. id: visId || StringResources.get('emptyVisualization'),
  118. errorCode: hasParams ? error.getParams().errorInfo.errorCode : null,
  119. errorMessage: errorMessage
  120. }
  121. };
  122. this.showError(errorMessage, params);
  123. },
  124. createVisAPI: function createVisAPI(args) {
  125. if (!this.visAPI) {
  126. this.visAPI = new VisAPI(args);
  127. } else {
  128. this.visAPI.reIntialize(args);
  129. }
  130. return this.visAPI;
  131. },
  132. /**
  133. * create an empty visualization 'drop items here'.
  134. */
  135. createEmptyVis: function createEmptyVis() {
  136. try {
  137. this._initializeModelManagers();
  138. this.ownerWidget.readyDfd.resolve();
  139. } catch (error) {
  140. this._showLoadingVisualizationNotFound(this.widgetModel && this.widgetModel.visId, error);
  141. }
  142. },
  143. /**
  144. * Initialize all vis model managers and support objects for both create from data and create from viz cases.
  145. */
  146. //archetype isn't used, but don't want to change API
  147. _initializeModelManagers: function _initializeModelManagers() {
  148. //When dataset is known.
  149. this.createVisConditionSupportDeprecatedFeature();
  150. this.visQuerySupport = new VisQuerySupport({
  151. visModelManager: this.visModelManager,
  152. ownerWidget: this.ownerWidget,
  153. propertySupport: this.visPropertySupport,
  154. pageContext: this.pageContext,
  155. logger: this.logger,
  156. visAPI: this.visAPI
  157. });
  158. this.visRenderSupport = new VisRenderSupport({
  159. visQuerySupport: this.visQuerySupport,
  160. content: this.content,
  161. visModelManager: this.visModelManager,
  162. ownerWidget: this.ownerWidget,
  163. pageContext: this.pageContext,
  164. synchronizeData: this.synchronizeData,
  165. dashboardApi: this.dashboardApi
  166. });
  167. this.visModelManagerLEFTOVER = new VisModelManagerLeftover({
  168. content: this.content,
  169. widgetModel: this.widgetModel,
  170. ownerWidget: this.ownerWidget,
  171. dashboardApi: this.dashboardApi
  172. });
  173. this.visAPI = this.createVisAPI({
  174. visModelManager: this.visModelManager,
  175. visRenderSupport: this.visRenderSupport,
  176. visFilterSupport: this.visFilterSupport,
  177. visConditionsSupport: this.visConditionsSupport,
  178. visPropertySupport: this.visPropertySupport,
  179. visQuerySupport: this.visQuerySupport,
  180. ownerWidget: this.ownerWidget,
  181. visModelManagerLEFTOVER: this.visModelManagerLEFTOVER
  182. });
  183. this.ownerWidget.setVisModelManager(this.visModelManager, this.visAPI);
  184. this.visAPI.setDataMembersTEMPORARILY({
  185. ownerWidget: this.ownerWidget,
  186. pendingFilters: this.visModelManager.pendingFilters,
  187. localFilters: this.visModelManager.localFilters
  188. });
  189. this.createVisInteractivityControllerDeprecatedFeature();
  190. this.visQuerySupport.initialize(this.visAPI, this.content);
  191. this.visRenderSupport.initialize();
  192. this.visConditionsSupport.initialize(this.visAPI);
  193. },
  194. showError: function showError(msg, params, type) {
  195. var state = this.content.getFeature('state.internal');
  196. if (state) {
  197. var error = new APIError({ 'msg': msg, 'params': params }, { 'type': type });
  198. state.setError(error);
  199. }
  200. },
  201. /**
  202. * Called when altering the module used by this visualization. As it affects a wide variety of objects, it's better to
  203. * simply re-initalize the existing visualization's visAPI instance to a completely new instances of it's models and their supporting classes
  204. *
  205. */
  206. recreateVisualization: function recreateVisualization() {
  207. return this.createVisualization();
  208. },
  209. getDisplayTypeManager: function getDisplayTypeManager() {
  210. return this.displayTypeManager;
  211. },
  212. /**
  213. * @returns the current module object.
  214. */
  215. getModule: function getModule() {
  216. return this.visModelManager.getModule();
  217. },
  218. _handleGetDataSetError: function _handleGetDataSetError(error, sourceId) {
  219. this.showError('dwErrorMissingDataset', {
  220. 'datasetName': error.sourceInfo ? error.sourceInfo.name : sourceId
  221. });
  222. },
  223. getVisModelManager: function getVisModelManager() {
  224. return this.visModelManager;
  225. },
  226. createVisConditionSupportDeprecatedFeature: function createVisConditionSupportDeprecatedFeature() {
  227. if (!this.visConditionsSupport) {
  228. this.visConditionsSupport = new VisConditionsSupport({
  229. visModelManager: this.visModelManager,
  230. widgetModel: this.widgetModel,
  231. pageContext: this.pageContext,
  232. dashboardApi: this.dashboardApi,
  233. visualization: this.visualizationFeature,
  234. dataModel: this.dataModel
  235. });
  236. this.contentFeatureLoader.registerDeprecatedFeature(this.content.getId(), 'VisConditionsSupport.deprecated', this.visConditionsSupport);
  237. }
  238. },
  239. createVisInteractivityControllerDeprecatedFeature: function createVisInteractivityControllerDeprecatedFeature() {
  240. if (!this.visSelectionController) {
  241. this.visSelectionController = new Vis2SelectionController({
  242. visAPI: this.visAPI,
  243. visModel: this.visAPI, //TODO: Deprecate this!
  244. ownerWidget: this.ownerWidget,
  245. pageContext: this.pageContext,
  246. synchronizeData: this.synchronizeData,
  247. dashboardApi: this.dashboardApi
  248. });
  249. this.contentFeatureLoader.registerDeprecatedFeature(this.content.getId(), 'SelectionController.deprecated', this.visSelectionController);
  250. }
  251. if (!this.visInteractivityController) {
  252. this.visInteractivityController = new VisInteractivityController({
  253. visAPI: this.visAPI,
  254. visModel: this.visAPI, //TODO: Deprecate this!
  255. ownerWidget: this.ownerWidget,
  256. pageContext: this.pageContext,
  257. synchronizeData: this.synchronizeData,
  258. dashboardApi: this.dashboardApi,
  259. selectionController: this.visSelectionController
  260. });
  261. this.contentFeatureLoader.registerDeprecatedFeature(this.content.getId(), 'InteractivityController.deprecated', this.visInteractivityController);
  262. }
  263. }
  264. });
  265. return VisualizationCreator;
  266. });
  267. //# sourceMappingURL=VisualizationCreator.js.map