'use strict'; /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Dashboard *| (C) Copyright IBM Corp. 2014, 2020 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['./visapi/VisAPI', './visapi/support/VisRenderSupport', './visapi/support/VisConditionsSupport', './visapi/support/VisQuerySupport', '../../visualizations/renderer/VisController', // CAREFUL: This VisController is the View/Interactivity Controller. '../../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) { 'use strict'; /** * INTENT: VisualizationCreator creates the entities and support objects for a live widget visualization. */ var VisualizationCreator = Class.extend({ // constructor init: function init(params) { VisualizationCreator.inherited('init', this, arguments); this.ownerWidget = params.ownerWidget; this.widgetModel = params.widgetModel; this.dashboardApi = params.dashboardAPI; this.logger = params.logger; //TODO: MARK=Should investigate what params are actually needed.... this.isPreview = params.initialConfigJSON.isPreview ? params.initialConfigJSON.isPreview : false; //TODO: Check if this changes after init. this.content = params.content; //Legacy manager created by the visualization feature this.visModelManager = params.legacyManagers.visModelManager; this.dataModel = params.legacyManagers.dataModel; this.visFilterSupport = params.legacyManagers.visFilterSupport; this.visPropertySupport = params.legacyManagers.visPropertySupport; this.displayTypeManager = params.legacyManagers.visDisplayTypeManager; this.recommender = params.legacyManagers.recommender; this.pageContext = null; this.synchronizeData = null; this.visDefinitions = this.dashboardApi.getFeature('VisDefinitions.internal'); this.visualizationFeature = this.content.getFeature('Visualization'); this.contentFeatureLoader = params.contentFeatureLoader; }, destroy: function destroy() { if (this.visAPI) { try { this.visAPI.destroy(); } catch (error) { // Do nothing in destroy. } finally { this.visAPI = null; } } this.visModelManager = null; this.visFilterSupport = null; this.visConditionsSupport = null; this.visPropertySupport = null; this.displayTypeManager = null; this.recommender = null; this.visDefinitions = null; this.visualizationFeature = null; this.contentFeatureLoader = null; this.pageContext = null; this.synchronizeData = null; this.content = null; this.ownerWidget = null; this.widgetModel = null; this.dashboardApi = null; this.logger = null; if (this.visRenderSupport) { this.visRenderSupport.remove(); this.visRenderSupport = null; } VisualizationCreator.inherited('destroy', this, arguments); }, setPageContext: function setPageContext(pageContext) { this.pageContext = pageContext; }, setSynchronizeData: function setSynchronizeData(synchronizeData) { this.synchronizeData = synchronizeData; }, _validateVisDefinition: function _validateVisDefinition(visId) { var visDefinition = this.visDefinitions.getById(visId); var visDefinitionReady = void 0; if (visDefinition.getState().getError()) { // attempt to refresh the visualization definition if it had previous failed visDefinitionReady = visDefinition.refresh(); } else { visDefinitionReady = Promise.resolve(); } return visDefinitionReady; }, createVisualization: function createVisualization() { var _this = this; if (!this.visualizationFeature.getDataSource()) { this.createEmptyVis(); return this.ownerWidget.readyDfd.promise; } else { return this._validateVisDefinition(this.widgetModel.visId).then(function () { _this._initializeModelManagers(); // TODO: make a better fix for this, the widget waiting for the canvas to be ready sounds like an anti-pattern // the synchronize data should be revisited _this.dashboardApi.getCanvasWhenReady().then(function () { return _this.synchronizeData.loadBrushedContext(_this.pageContext, _this.visualizationFeature, _this.visAPI).then(function () { _this.ownerWidget.readyDfd.resolve(); }); }); }).catch(function (error) { _this._showLoadingVisualizationNotFound(_this.widgetModel.visId, error); throw error; }); } }, _showLoadingVisualizationNotFound: function _showLoadingVisualizationNotFound(visId, error) { var hasParams = error instanceof APIError && error.getParams(); var errorMessage = hasParams ? error.getParams().errorInfo.errorMessage : StringResources.get('dwErrorLoadingVisualizationNotFound'); var params = { errorInfo: { id: visId || StringResources.get('emptyVisualization'), errorCode: hasParams ? error.getParams().errorInfo.errorCode : null, errorMessage: errorMessage } }; this.showError(errorMessage, params); }, createVisAPI: function createVisAPI(args) { if (!this.visAPI) { this.visAPI = new VisAPI(args); } else { this.visAPI.reIntialize(args); } return this.visAPI; }, /** * create an empty visualization 'drop items here'. */ createEmptyVis: function createEmptyVis() { try { this._initializeModelManagers(); this.ownerWidget.readyDfd.resolve(); } catch (error) { this._showLoadingVisualizationNotFound(this.widgetModel && this.widgetModel.visId, error); } }, /** * Initialize all vis model managers and support objects for both create from data and create from viz cases. */ //archetype isn't used, but don't want to change API _initializeModelManagers: function _initializeModelManagers() { //When dataset is known. this.createVisConditionSupportDeprecatedFeature(); this.visQuerySupport = new VisQuerySupport({ visModelManager: this.visModelManager, ownerWidget: this.ownerWidget, propertySupport: this.visPropertySupport, pageContext: this.pageContext, logger: this.logger, visAPI: this.visAPI }); this.visRenderSupport = new VisRenderSupport({ visQuerySupport: this.visQuerySupport, content: this.content, visModelManager: this.visModelManager, ownerWidget: this.ownerWidget, pageContext: this.pageContext, synchronizeData: this.synchronizeData, dashboardApi: this.dashboardApi }); this.visModelManagerLEFTOVER = new VisModelManagerLeftover({ content: this.content, widgetModel: this.widgetModel, ownerWidget: this.ownerWidget, dashboardApi: this.dashboardApi }); this.visAPI = this.createVisAPI({ visModelManager: this.visModelManager, visRenderSupport: this.visRenderSupport, visFilterSupport: this.visFilterSupport, visConditionsSupport: this.visConditionsSupport, visPropertySupport: this.visPropertySupport, visQuerySupport: this.visQuerySupport, ownerWidget: this.ownerWidget, visModelManagerLEFTOVER: this.visModelManagerLEFTOVER }); this.ownerWidget.setVisModelManager(this.visModelManager, this.visAPI); this.visAPI.setDataMembersTEMPORARILY({ ownerWidget: this.ownerWidget, pendingFilters: this.visModelManager.pendingFilters, localFilters: this.visModelManager.localFilters }); this.createVisInteractivityControllerDeprecatedFeature(); this.visQuerySupport.initialize(this.visAPI, this.content); this.visRenderSupport.initialize(); this.visConditionsSupport.initialize(this.visAPI); }, showError: function showError(msg, params, type) { var state = this.content.getFeature('state.internal'); if (state) { var error = new APIError({ 'msg': msg, 'params': params }, { 'type': type }); state.setError(error); } }, /** * Called when altering the module used by this visualization. As it affects a wide variety of objects, it's better to * simply re-initalize the existing visualization's visAPI instance to a completely new instances of it's models and their supporting classes * */ recreateVisualization: function recreateVisualization() { return this.createVisualization(); }, getDisplayTypeManager: function getDisplayTypeManager() { return this.displayTypeManager; }, /** * @returns the current module object. */ getModule: function getModule() { return this.visModelManager.getModule(); }, _handleGetDataSetError: function _handleGetDataSetError(error, sourceId) { this.showError('dwErrorMissingDataset', { 'datasetName': error.sourceInfo ? error.sourceInfo.name : sourceId }); }, getVisModelManager: function getVisModelManager() { return this.visModelManager; }, createVisConditionSupportDeprecatedFeature: function createVisConditionSupportDeprecatedFeature() { if (!this.visConditionsSupport) { this.visConditionsSupport = new VisConditionsSupport({ visModelManager: this.visModelManager, widgetModel: this.widgetModel, pageContext: this.pageContext, dashboardApi: this.dashboardApi, visualization: this.visualizationFeature, dataModel: this.dataModel }); this.contentFeatureLoader.registerDeprecatedFeature(this.content.getId(), 'VisConditionsSupport.deprecated', this.visConditionsSupport); } }, createVisInteractivityControllerDeprecatedFeature: function createVisInteractivityControllerDeprecatedFeature() { if (!this.visSelectionController) { this.visSelectionController = new Vis2SelectionController({ visAPI: this.visAPI, visModel: this.visAPI, //TODO: Deprecate this! ownerWidget: this.ownerWidget, pageContext: this.pageContext, synchronizeData: this.synchronizeData, dashboardApi: this.dashboardApi }); this.contentFeatureLoader.registerDeprecatedFeature(this.content.getId(), 'SelectionController.deprecated', this.visSelectionController); } if (!this.visInteractivityController) { this.visInteractivityController = new VisInteractivityController({ visAPI: this.visAPI, visModel: this.visAPI, //TODO: Deprecate this! ownerWidget: this.ownerWidget, pageContext: this.pageContext, synchronizeData: this.synchronizeData, dashboardApi: this.dashboardApi, selectionController: this.visSelectionController }); this.contentFeatureLoader.registerDeprecatedFeature(this.content.getId(), 'InteractivityController.deprecated', this.visInteractivityController); } } }); return VisualizationCreator; }); //# sourceMappingURL=VisualizationCreator.js.map