123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- 'use strict';
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *|
- *| IBM Cognos Products: dashboard-core
- *|
- *| (C) Copyright IBM Corp. 2015, 2020
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- /*
- * !!WARNING!! !!WARNING!! !!WARNING!!
- *
- * This file is used/inherited in Storytelling.
- * Please make sure changes in here do not break Storytelling or at least let us know.
- */
- define(['underscore', '../../../lib/@waca/core-client/js/core-client/ui/core/Class', '../../../app/util/ErrorUtils', '../../../app/util/GlassUtil', '../../../dashboard/glass/util/SaveActionUtil'], function (_, BaseClass, ErrorUtils, GlassUtil, SaveActionUtil) {
- var saveResponseFields = ['status', 'name', 'saveRequest', 'assetId', 'url', 'savedSpec', 'cmSpec'];
- var ActionHandler = BaseClass.extend({
- getContentView: function getContentView(context) {
- return context.glassContext.appController.currentAppView.currentContentView;
- },
- getDashboardView: function getDashboardView(context) {
- return this.getContentView(context);
- },
- getStringResourcesService: function getStringResourcesService(context) {
- return this.getContentView(context).services.getSvcSync('.StringResources');
- },
- _getDashboardApi: function _getDashboardApi(context) {
- return this.getContentView(context).getDashboardApi();
- },
- _getContextContent: function _getContextContent(context) {
- return this.getContentView(context).getContent();
- },
- /**
- * Handle the save button click. If the dashboard has an id, then we will save the dashboard. otherwise we will do a save as
- */
- onSelectItem: function onSelectItem(context) {
- var id = this.getContentView(context).getBoardId();
- var saveMode = context.target.itemId === 'com.ibm.bi.dashboard.save';
- if (id && saveMode && this.getContentView(context).canAuthor()) {
- this.save(context, true, true /*bShowToast*/);
- } else {
- this.saveAs(context, id);
- }
- },
- isItemVisible: function isItemVisible(context) {
- // explore and dashboard shares the save button, but dashboard capability should not affect explore user to save exploration
- // TODO: clean this up once explore has their own button contribution
- var appViewType = this.getContentView(context).getDashboardApi().getType().toUpperCase();
- var saveMode = context.target.itemId === 'com.ibm.bi.dashboard.save';
- if (appViewType === 'EXPLORE') {
- return saveMode ? this.getContentView(context).canAuthor() : true;
- }
- return saveMode ? this.getContentView(context).canAuthor() && ErrorUtils.hasCapability(context.glassContext, 'canAuthorDashboard') : ErrorUtils.hasCapability(context.glassContext, 'canAuthorDashboard');
- },
- /**
- * Hook to override require calls for the unit test.
- */
- _require: function _require(modules, callback) {
- return require(modules, callback);
- },
- /**
- * Prompt the user for a location and do a save as.
- * @param {Object} context of selection (passed during onSelect)
- * @param {string} id The Board Id
- */
- saveAs: function saveAs(context, id) {
- var _this = this;
- var dashboardView = this.getDashboardView(context);
- var defaultName = dashboardView.boardModel.name;
- return this._getAncestors(context, id).then(function (ancestors) {
- return context.glassContext.getSvc('.ContentDialogFactory').then(function (dialogFactory) {
- _this._saveAsDialog = dialogFactory.createSaveAsDialog({
- glassContext: context.glassContext,
- defaultFileName: defaultName,
- service: {
- context: context,
- save: _this.onSaveAs.bind(_this)
- },
- ancestors: ancestors
- });
- _this._saveAsDialog.open();
- });
- });
- },
- _getAncestors: function _getAncestors(context, id) {
- return this._getContentService(context).then(function (contentService) {
- return contentService.getAncestors({ id: id });
- });
- },
- onSaveAs: function onSaveAs(service, selection, name, overwrite) {
- var context = service.context;
- return this._getSaveFeature(context).saveAs(name, selection.url, overwrite, saveResponseFields).then(this._onSaveAsSuccess.bind(this, context), this._onSaveAsError.bind(this, context)).then(this._invokeLifeCycleHandlers.bind(this, 'saveas.after', context)).finally(this._saveAsDialog.hide.bind(this._saveAsDialog));
- },
- _onSaveAsError: function _onSaveAsError(context, errorObject) {
- var errorCode = ErrorUtils.getErrorCode(errorObject.saveRequest);
- if (errorCode === 'cmDuplicateName') {
- var error = {
- 'isDuplicate': true
- };
- return Promise.reject(error);
- } else if (errorCode.lastIndexOf('cmAddWithReplaceFailed', 0) === 0 || errorCode.lastIndexOf('cmInvalidReference', 0) === 0) {
- // error codes: cmAddWithReplaceFailed, cmAddWithReplaceFailedBadClass, cmInvalidReference2
- error = {
- 'isReplaceFailed': true
- };
- return Promise.reject(error);
- } else {
- ErrorUtils.showError(context.glassContext, errorObject.saveRequest, errorObject.cmSpec);
- return Promise.resolve();
- }
- },
- _onSaveAsSuccess: function _onSaveAsSuccess(context) {
- var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- var contentView = this.getContentView(context);
- contentView.setSavedInstance(params.savedSpec);
- // Call Content Service GET to trigger MRU
- return this._getContentService(context).then(function (oContentService) {
- return oContentService.onSaveAsSuccess({ url: params.url });
- }).done(function (dbInfo) {
- var dashboard = this._getDashboardApi(context);
- if (dbInfo) {
- if (dbInfo.id) {
- contentView.boardModel.set({ id: dbInfo.id }, { silent: true });
- }
- if (dbInfo.permissions) {
- contentView.setPermissions(dbInfo.permissions);
- }
- }
- // Update the name in the dashboard model
- contentView.boardModel.set({
- name: params.name
- }, {
- silent: true
- });
- var segment = dashboard.getFeature('segment');
- var segmentType = { type: 'Updated Object' };
- segment.track('savedAs', function () {
- return segmentType;
- });
- // Clear dirty flag in the UI
- dashboard.getFeature('DashboardState').setDirty(false);
- contentView.boardController.undoRedoController.registerSaveAction();
- // Update current cached app view to prevent Defect 235654
- context.glassContext.appController.updateCurrentCachedAppView();
- var appConfig = this._getAppConfig(context);
- if (appConfig && appConfig.saveThumbnail) {
- this._saveThumbnail(context, dbInfo && dbInfo.id);
- }
- var stringResources = this.getStringResourcesService(context);
- context.glassContext.appController.showToast(stringResources.get('dashboard_save_success'));
- return this._invokeSaveServiceHandlers(context, {
- saveAs: true,
- stringResources: stringResources,
- context: context,
- spec: dashboard.getFeature('Serializer').toJSON()
- });
- }.bind(this));
- },
- /**
- * Get glass service .DashboardContent
- * @private
- * @async
- * @return {Promise<Object>}
- */
- _getContentService: function _getContentService(context) {
- return context.glassContext.getSvc('.DashboardContent');
- },
- save: function save(context, bInvokeSaveServiceHandlers, bShowToast) {
- var _this2 = this;
- var dashboardView = this.getDashboardView(context);
- var dashboard = dashboardView.getDashboardApi();
- var id = dashboardView.getBoardId();
- var boardModel = dashboardView.boardModel;
- var boardSpec = dashboard.getFeature('Serializer').toJSON();
- var _saveSpec = null;
- SaveActionUtil.cleanUnusedCustomColors(dashboard, boardSpec, boardModel);
- return this._getSaveFeature(context).save(saveResponseFields).then(function (_ref) {
- var savedSpec = _ref.savedSpec;
- if (savedSpec) {
- _saveSpec = savedSpec;
- }
- return _this2._invokeLifeCycleHandlers('save.after', context);
- }).then(function () {
- var stringResources = _this2.getStringResourcesService(context);
- var segment = dashboard.getFeature('segment');
- var segmentType = { type: 'Updated Object' };
- segment.track('saved', function () {
- return segmentType;
- });
- // Clear dirty flag in the UI
- dashboard.getFeature('DashboardState').setDirty(false);
- dashboardView.boardController.undoRedoController.registerSaveAction();
- dashboardView.setSavedInstance(_saveSpec);
- var appConfig = _this2._getAppConfig(context);
- if (appConfig && appConfig.saveThumbnail) {
- _this2._saveThumbnail(context, id);
- }
- if (bShowToast) {
- context.glassContext.appController.showToast(stringResources.get('dashboard_save_success'));
- }
- if (bInvokeSaveServiceHandlers) {
- _this2._invokeSaveServiceHandlers(context, {
- saveAs: false,
- stringResources: stringResources,
- context: context,
- spec: boardSpec
- });
- }
- }).catch(function (request) {
- var code = ErrorUtils.showError(context.glassContext, request);
- // The object is deleted.. try a save as
- if (code === 'cmEmptySelection') {
- _this2.saveAs(context);
- }
- });
- },
- _invokeLifeCycleHandlers: function _invokeLifeCycleHandlers(name, context) {
- var dashboardApi = this.getDashboardView(context).getDashboardApi();
- var lifeCycle = dashboardApi.getFeature('LifeCycle');
- return lifeCycle.invokeHandlers(name).catch(function (error) {
- var logger = dashboardApi.getGlassCoreSvc('.Logger');
- logger.error('[SaveActionHandler] Life cycle handlers error', error);
- });
- },
- /**
- * Loop over the services that are in the saveServices collection to call their 'onDashboardSave' method
- */
- _invokeSaveServiceHandlers: function _invokeSaveServiceHandlers(context, options) {
- var dashboard = this._getDashboardApi(context);
- return this._getSaveServiceExtensionCollection(context.glassContext, dashboard).then(function (collection) {
- var promises = [];
- if (collection) {
- // provide a call that will update the dashboard spec in CM if needed
- options.resaveDashboardSpecCallback = this.save.bind(this, context, false, false /*bShowToast*/);
- collection.forEach(function (extension) {
- var saveService = dashboard.getFeature(extension.name);
- if (saveService && saveService.onDashboardSave) {
- promises.push(saveService.onDashboardSave(options));
- }
- });
- }
- return Promise.all(promises);
- }.bind(this));
- },
- _getSaveServiceExtensionCollection: function _getSaveServiceExtensionCollection(glassContext, dashboard) {
- var saveServices = dashboard.getCollectionConfig('saveServices');
- if (saveServices) {
- return Promise.all([glassContext.appController.findCollection(saveServices.id)]).then(function (extensions) {
- return extensions[0];
- });
- }
- return Promise.resolve();
- },
- _getAppConfig: function _getAppConfig(context) {
- var content = this._getContextContent(context);
- return content && content.options && content.options.config;
- },
- _saveThumbnail: function _saveThumbnail(context, id) {
- var _this3 = this;
- return this._createThumbnail(context).then(function (thumbnailDataUri) {
- return _this3._getContentService(context).then(function (contentService) {
- return contentService.saveThumbnail({ thumbnailDataUri: thumbnailDataUri, id: id });
- });
- });
- },
- _createThumbnail: function _createThumbnail(context) {
- var dashboardView = this.getDashboardView(context);
- var thumbnailSvc = dashboardView.getDashboardApi().getFeature('.Thumbnail');
- return thumbnailSvc && thumbnailSvc.generateImage(dashboardView.el);
- },
- _getSaveFeature: function _getSaveFeature(context) {
- return this._getDashboardApi(context).getFeature('Save');
- }
- });
- return ActionHandler;
- });
- //# sourceMappingURL=SaveActionHandler.js.map
|