'use strict'; /** * Licensed Materials - Property of IBM * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2017, 2020 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['../../../lib/@waca/core-client/js/core-client/ui/core/Class', '../../../lib/@waca/dashboard-common/dist/utils/ObjectHelper', 'jquery'], function (Class, ObjectHelper, $) { var DashboardController = Class.extend({ init: function init(options) { DashboardController.inherited('init', this, arguments); this.view = options.view; this.glassContext = options.glassContext; this.eventRouter = options.eventRouter; this.services = options.services; this.widgetRegistry = options.widgetRegistry; this.featuresOverride = options.featuresOverride || {}; this.FrozenWidgetRegistry = null; var _ref = options.appSettings || {}, _ref$options = _ref.options; _ref$options = _ref$options === undefined ? {} : _ref$options; var _ref$options$collecti = _ref$options.collections, collections = _ref$options$collecti === undefined ? {} : _ref$options$collecti, _ref$options$config = _ref$options.config, config = _ref$options$config === undefined ? {} : _ref$options$config; this.collectionsConfig = collections; this.config = config; }, createContent: function createContent(boardModel) { var options = { featureLoader: this.featureLoader, dashboardFeatures: this.featureLoader, boardModel: boardModel, type: 'dashboard' }; var contentFactory = this.featureLoader.getFeature('ContentFactory'); this._dashboardContent = contentFactory.createContent(options); return this._dashboardContent.initialize(); }, getFeature: function getFeature(name) { var feature = void 0; if (this.featureLoader) { feature = this.featureLoader.getFeature(name); } if (!feature) { try { feature = this.getDashboardCoreSvc(name); } catch (e) { // Ignore if getDashboardCoreSvc threw a missing feature error. getFeature should never throw a fault. // and won't once we clean up the old service stuff } } return feature; }, setDirty: function setDirty(dirty) { // Sync dirty state to undoredo controller if (this.view.boardController && this.view.boardController.undoRedoController) { this.view.boardController.undoRedoController.setDirty(dirty); } this.view.trigger('change:dirty', { value: dirty }); this.view.eventRouter.trigger('change:dirty', { value: dirty }); }, setServices: function setServices(services) { this.services = services; }, setFeatureLoader: function setFeatureLoader(featureLoader) { this.featureLoader = featureLoader; }, /** * Return the current content view */ getCurrentContentView: function getCurrentContentView() { return this.view; }, /** * Return the canvas API. This method might retrun null if the canvas is not ready yet */ getCanvas: function getCanvas() { // The deprecated canvas contains all the deprecated api + the proper public apis return this.view.getDeprecatedCanvas(); }, /** * Return a promise that will be resolved with the canvas API */ getCanvasWhenReady: function getCanvasWhenReady() { // The deprecated canvas contains all the deprecated api + the proper public apis return this.view.getDeprecatedCanvasWhenReady(); }, isDevInstall: function isDevInstall() { return this.glassContext.isDevInstall(); }, /** * Add a new widget to the canvas or new content to selected widgets * * @param {object} - options containing the widget to be added */ addContentToCanvas: function addContentToCanvas(options) { this.view.boardController.addDataItemsOrAddWidget(options); }, /** * Return the application name */ getApplicationName: function getApplicationName() { return this.glassContext.appController.getCurrentPerspective(); }, /** * Return the localized application name */ getApplicationLabel: function getApplicationLabel() { return this.view.getApplicationLabel(); }, /** * Reload the dashboard using a give json specified * @param {object} JSONSpec An object that represents a dashboard spec * @param {object} extOptions extra options to use when re-initializing BaseBoardView * @returns {Promise} The resolved object is the new dashboardApi instance from the reinitialization of the dashboad from the given spec */ reloadFromJSONSpec: function reloadFromJSONSpec(JSONspec, extOptions) { return this.view.reloadFromJSONSpec(JSONspec, extOptions); }, /** * Return the CDN url */ getCDNUrl: function getCDNUrl() { return this.view.getCDNUrl(); }, /** * Get layout * * @param {string} id - layout id * * @return {Layout} layout * * @deprecated Use getCanvas().getLayout instead */ getLayout: function getLayout(id) { return this.view.boardController.layoutController.getView(id); }, /** * Glassify the options a.k.a - make an options object ready to be passed to a glass dependant component * * @param {object} options to be glassified. * */ prepareGlassOptions: function prepareGlassOptions(options) { options.glassContext = this.glassContext; }, /** * Open a glass application view * * @param {string} name - application anme * @param {object} options - application options. * */ openGlassApplication: function openGlassApplication(name, options) { return this.glassContext.appController.openAppView(name, options); }, /** * Close a glass application view * * @param {string} name - application name * @param {object} id - id of the application being closed. * @param {object} options - application options. * */ closeGlassApplication: function closeGlassApplication(name, id, options) { return this.glassContext.appController.closeAppView(name, id, options); }, /** * Return a collection config specified through the perspective * @param {string} name collection name * @return {object} config value */ getCollectionConfig: function getCollectionConfig(name) { return this.collectionsConfig[name]; }, /** * Return a config specified through the perspective * @param {string} config name * @return {object} config value */ getAppConfig: function getAppConfig(name) { return this.config[name]; }, /** * Return a dashboard service * @param {string} name service name * @return {Promise} - resolved with the service object */ getDashboardSvc: function getDashboardSvc(name) { return this.services.getSvc(name); }, /** * Register a dashboard service * @param {string} name service name * @param {Object} service service object * */ registerDashboardSvc: function registerDashboardSvc(name, service) { return this.services.register(name, service); }, /** * Return a dashboard core service * @param {string} name service name * @return {Object} - Service object */ getDashboardCoreSvc: function getDashboardCoreSvc(name) { return this.services.getSvcSync(name); }, /** * Return a glass service * @param {string} name service name * @return {Promise} - resolved with the service object */ getGlassSvc: function getGlassSvc(name) { return this.glassContext.getSvc(name); }, /** * Return a glass core service * @param {string} name service name * @return {Object} - Service object */ getGlassCoreSvc: function getGlassCoreSvc(name) { return this.glassContext.getCoreSvc(name); }, /** * Show an error message * @param {string} message * @param {string} title */ showErrorMessage: function showErrorMessage(message, title) { return this.glassContext.appController.showErrorMessage(message, title); }, /** * Show a message in the UI * @param {string} message */ showMessage: function showMessage(message) { return this.glassContext.appController.showMessage(message); }, /** * Show a toast message in the UI * @param {string} message * @param {object} options - toast options */ showToast: function showToast(message, options) { return this.glassContext.appController.showToast(message, options); }, /** * Show a glass slideout * @param {object} options - slideout options */ showSlideOut: function showSlideOut(options) { return this.glassContext.appController.showSlideOut(options); }, /** * Show a glass context menu * @param {object} options - context menu options */ showContextMenu: function showContextMenu(options) { return this.glassContext.appController.showContextMenu(options); }, /** * Find a glass plugin * @param {string} name - plugin name * @return {object} glass plugin object */ findGlassPlugin: function findGlassPlugin(name) { return this.glassContext.appController.findPlugin(name); }, /** * Find a glass collection * @param {string} name - plugin name * @return {Promise} promise resolved with the collection */ findGlassCollection: function findGlassCollection(name) { var _this = this; return this.glassContext.appController.findCollection(name).then(function () { var collection = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var featureOverride = _this.featuresOverride[name] || {}; var excludes = featureOverride.excludes || []; return collection.filter(function (item) { return !(excludes.indexOf(item.name) !== -1); }); }); }, /** * Open a glass app view * @param {string} name * @param {object} options */ openAppView: function openAppView(name, options) { return this.glassContext.appController.openAppView(name, options); }, /** * @deprecated Use getCanvas().getSelectedWidgets() instead */ getSelectedWidgets: function getSelectedWidgets() { return this.view.getSelectedWidgets(); }, /** * Set the permissions on the dashboard * @param {object} permissions */ setPermissions: function setPermissions(permissions) { this.view.setPermissions(permissions); }, /** * Checks for write access to the dashboard */ canAuthor: function canAuthor() { return this.view.canAuthor(); }, /** * Destroy the dashboard - clean up */ destroy: function destroy() { DashboardController.inherited('init', this, arguments); if (this.services) { this.services.destroy(); } if (this.featureLoader) { this.featureLoader.destroy(); } if (this._dashboardContent) { this._dashboardContent.destroy(); this._dashboardContent = null; } this.view = null; this.glassContext = null; this.eventRouter = null; this.services = null; this.collectionsConfig = null; this.config = null; this.widgetRegistry = null; this.services = null; this.featureLoader = null; }, /** * Map between API event names and internal event names */ eventMap: { dirty: 'change:dirty' }, /** * Register an event handler for dashboard events * @param {string} eventName Name of the dashboard event * @param {function} handler Event handler to be called when the event occurrs */ on: function on(eventName, callback, context) { if (!eventName) { return; } var name = this.eventMap[eventName] || eventName; return this.eventRouter.on(name, callback, context); }, /** * Unregister an event handler for dashboard events * @param {string} eventName Name of the dashboard event * @param {function} handler Event handler to be called when the event occurrs */ off: function off(eventName, callback, context) { if (!eventName) { return; } var name = this.eventMap[eventName] || eventName; this.eventRouter.off(name, callback, context); }, /** * Trigger a dashboard event * @param {string} eventName Name of the dashboard event * @param {object} payload Event payload */ triggerDashboardEvent: function triggerDashboardEvent(eventName, payload) { if (!eventName) { return; } var name = this.eventMap[eventName] || eventName; this.eventRouter.trigger(name, payload); }, /** * Relink an existing source to a new module. * Widgets that reference this source will reload after the relink is finished. * @param {String} sourceInfo.id Id of the source to relink. This id is passed to the caller in the relink:clicked event * @param {String=} sourceInfo.cognosAssetId Cognos Content Story ID. * @param {String=} sourceInfo.module The module definition * @param {String=} sourceInfo.type The type of the source, only needed if using cognosAssetId and the type if the source is changing. * @param {String=} sourceInfo.name Name of the source, only needed if you wish to change the name. */ relink: function relink() /*sourceInfo*/{ // var dataSourcesSvc = this.services.getSvcSync('dataSources'); // dataSourcesSvc.relink(this.oDataSource, oNewDataSourceObj); }, /** * Change the mode between authoring, consumption and eventGroups * @param {string} mode */ setMode: function setMode(mode) { var promise = void 0; var contentView = this.view; if (contentView) { if (mode === 'eventGroups' && !contentView.isEventGroupViewMode || mode !== 'eventGroups' && contentView.isEventGroupViewMode) { // toggle from/to 'eventGroup' promise = contentView.toggleEventGroupMode(mode); } else if (mode === 'authoring' && !contentView.isAuthoringMode || mode === 'consumption' && contentView.isAuthoringMode) { promise = contentView.toggleMode(); } // Ensure the properties pane is closed in eventGroup or consumption mode if (contentView.isEventGroupViewMode || !contentView.isAuthoringMode) { this.toggleProperties(false); } } return promise || Promise.reject(new Error('Cannot set mode without a content view')); }, getMode: function getMode() { var contentView = this.view; if (contentView) { return contentView.isEventGroupViewMode ? 'eventGroups' : contentView.isAuthoringMode ? 'authoring' : 'consumption'; } }, /** * Get the dashboard instance specification * @return {Object} dashboard spec */ getSpec: function getSpec() { var contentView = this.view; if (contentView && contentView.getContent) { var content = contentView.getContent(); return content.boardSpec; } }, /** * Undo the last action performed on the dashboard */ undo: function undo() { var contentView = this.view; if (contentView) { contentView.doUndo(); } }, /** * Reverse the last undo performed on the dashboard */ redo: function redo() { var contentView = this.view; if (contentView) { contentView.doRedo(); } }, /** * Toggle the properties pane * @param {boolean} [toggle] if true display the properties pane, otherwise hide the properties pane. * @param {Object} [context] Optional context for toggling properties pane */ toggleProperties: function toggleProperties(toggle, context) { var contentView = this.view; if (contentView) { var controller = contentView.boardController; var dashboardAuthoringToolsPane = contentView.dashboardApi.getFeature('InAppSlideoutState'); var propertiesManager = controller.getExtension('propertiesManager'); var selectTab = function selectTab() { propertiesManager.getPropertyControl('tabControl').selectTabByName(context.tabName); }; // in case an invalid toggle state (non-boolean) is given, simply toggle the state if (typeof toggle !== 'boolean') { toggle = !propertiesManager.isSlideoutOpen(); } // Properties pane is always off on event group and consumption mode toggle = contentView.isEventGroupViewMode || !contentView.isAuthoringMode ? false : toggle; context = context || {}; // close the pane when in non authoring (consumption and fullscreen) and event group mode if (contentView.isEventGroupViewMode || !contentView.isAuthoringMode) { dashboardAuthoringToolsPane && dashboardAuthoringToolsPane.close(); } //TODO: remove the following block when the focus mode epic is ready if (propertiesManager.isSlideoutOpen()) { if (!toggle) { propertiesManager.hide(); } else if (context.tabName) { selectTab(); } } else if (toggle && !propertiesManager.isSlideoutOpen()) { propertiesManager.show({ 'context': context, 'onSetFocus': context.tabName ? selectTab : null }); } } }, /** * @returns an object containing dashboard information (isDirty, dashboard id and dashboard search path) */ getDashboardInfo: function getDashboardInfo() { return this.view.getBoardInfo(); }, /** * Select widget * * @param {string} widgetId - widget id to select */ selectWidget: function selectWidget(widgetId) { var _view$boardController = this.view.boardController; _view$boardController = _view$boardController === undefined ? {} : _view$boardController; var layoutController = _view$boardController.layoutController; if (layoutController) { return layoutController.getInteractionController().then(function (controller) { var widgetEl = $('#' + widgetId).get(0); controller.selectionHandler.selectNode(widgetEl); }); } return Promise.reject(new Error('Unable to select widget. Canvas not fully initialized.')); }, deselectAllWidgets: function deselectAllWidgets() { var _view$boardController2 = this.view.boardController; _view$boardController2 = _view$boardController2 === undefined ? {} : _view$boardController2; var layoutController = _view$boardController2.layoutController; if (layoutController) { return layoutController.getInteractionController().then(function (controller) { controller.selectionHandler.deselectAll(); }); } return Promise.resolve(); }, getToolbarActions: function getToolbarActions() { var toolbarItems = this.view.boardController && this.view.boardController.layoutController && this.view.boardController.layoutController.interactionController && this.view.boardController.layoutController.interactionController.toolbarItems; return toolbarItems; }, /** * Waits till all widgets in input list are rendered * * @param {string[]} widgetIds * @param {Promise} number of widgets rendered */ waitTillWidgetsRendered: function waitTillWidgetsRendered(widgetIds) { if (!widgetIds || widgetIds.length === 0) { return Promise.resolve(0); } return this.getCanvasWhenReady().then(function (canvas) { var promises = []; var renderCount = 0; widgetIds.forEach(function (widgetId) { var prom = canvas.getWidgetWhenReady(widgetId); promises.push(prom); }); return Promise.all(promises).then(function (widgetApis) { var renderPromises = []; widgetApis.forEach(function (widgetApi) { var prom2 = widgetApi.whenRenderComplete().then(function () { renderCount++; }); renderPromises.push(prom2); }); return Promise.all(renderPromises).then(function () { return renderCount; }); }); }); }, /** * get live widget registry * * @return a copy of live widget registry */ getWidgetRegistry: function getWidgetRegistry() { if (!this.FrozenWidgetRegistry) { this.FrozenWidgetRegistry = ObjectHelper.deepFreezeObject(JSON.parse(JSON.stringify(this.widgetRegistry))); } return this.FrozenWidgetRegistry; }, /** * Copy the selected widget(s) on the dashboard */ copy: function copy() { var contentView = this.view; if (contentView) { contentView.doCopy(); } }, /** * Paste the selected widget(s) on the dashboard */ paste: function paste() { var contentView = this.view; if (contentView) { return contentView.doPaste(); } return Promise.reject(); }, getContent: function getContent() { return this._dashboardContent && this._dashboardContent.getAPI(); } }); return DashboardController; }); //# sourceMappingURL=DashboardController.js.map