Source: api/sharing/ShareController.js

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: Collaboration
 * (C) Copyright IBM Corp. 2015, 2019
 *
 * US Government Users Restricted Rights - Use, duplication or disclosure
 * restricted by GSA ADP Schedule Contract with IBM Corp.
 */

define([
	'underscore',
	'../../lib/@waca/core-client/js/core-client/ui/core/Class',
	'../../messaging/Connectors',
	'./ShareableItems',
	'../../nls/StringResources',
	'../../utils/AssetTypeUtil'
], function (_, Class, Connectors, ShareableItems, StringResources, AssetTypeUtil) {

	var PERSPECTIVE_SUBTITLE_MAP = {
		'dashboard': function (view) {
			var tabName = view.boardController && view.boardController.layoutController && view.boardController.layoutController.getCurrentSubViewTitle && view.boardController.layoutController.getCurrentSubViewTitle();
			return tabName && StringResources.get('subview_title_tab', { tabName: tabName });
		},
		'story': function (view) {
			var sceneInfo = view.storyPaneController && view.storyPaneController.getCurrentSceneInfo && view.storyPaneController.getCurrentSceneInfo();
			var sceneTitle = sceneInfo && sceneInfo.title;
			return sceneTitle && StringResources.get('subview_title_scene', { sceneTitle: sceneTitle });
		},
		'explore': function (view) {
			const layoutModel = view.exploreContainerLayoutModel;
			if (layoutModel.exploreVM.isEmptyCard()) {
				return StringResources.get('cardless_exploration', { title: view.getTitle() });
			} else {
				const cardId = layoutModel.exploreVM.getDisplayedCard();
				const cardTitle = layoutModel.findModel(cardId).getTitle();
				return cardTitle ? StringResources.get('subview_title_explore', { cardTitle }) : StringResources.get('cardless_exploration', { title: view.getTitle() });
			}
		}
	};

	var PERSPECTIVE_SUBTITLE_OBJ_MAP = {
		'dashboard': function (view) {
			var tabName = view.boardController && view.boardController.layoutController && view.boardController.layoutController.getCurrentSubViewTitle && view.boardController.layoutController.getCurrentSubViewTitle();
			return tabName && {
				type: removeQuotationMarks(StringResources.get('subview_title_tab', { tabName: '' })),
				name: tabName
			};
		},
		'story': function (view) {
			var sceneInfo = view.storyPaneController && view.storyPaneController.getCurrentSceneInfo && view.storyPaneController.getCurrentSceneInfo();
			var sceneTitle = sceneInfo && sceneInfo.title;
			return sceneTitle && {
				type: removeQuotationMarks(StringResources.get('subview_title_scene', { sceneTitle: '' })),
				name: (typeof sceneTitle === 'string') ? sceneTitle : sceneTitle.translationTable.Default
			};
		},
		'explore': function (view) {
			const layoutModel = view.exploreContainerLayoutModel;
			if (layoutModel.exploreVM.isEmptyCard()) {
				return null;
			} else {
				const cardId = layoutModel.exploreVM.getDisplayedCard();
				const cardTitle = layoutModel.findModel(cardId).getTitle();
				return cardTitle ? {
					type: removeQuotationMarks(StringResources.get('subview_title_explore', { cardTitle: '' })),
					name: cardTitle
				} : null;
			}
		}
	};

	function removeQuotationMarks (str) {
		return str.replace(/[\u201C\u201D"]/g, '').trim();
	}

	var ShareController = Class.extend( /** @lends ShareController */ {

		/**
		 * @desc Constructor for ShareController.
		 * @constructs ShareController
		 * @extends Class
		 * @public
		 * @param {object} options
		 * @param {object} options.slideout The share panel slideout
		 * @param {object} options.glassContext The glass context
		 * @param {object} options.logger The logger service
		 * @param {function} options.errorHandler Function to call when an error occurs
		 */
		init: function (options) {
			ShareController.inherited('init', this, arguments);
			this.slideout = options.slideout;
			this.glassContext = options.glassContext;
			this._shareableItems = new ShareableItems(options);
			this._connectors = new Connectors(options);
			this.perspective = (this.glassContext && typeof this.glassContext.getCurrentContentView === 'function') ?
				this.glassContext.getCurrentContentView().perspective : '';
		},

		/**
		 * @instance
		 * @returns {promise}
		 */
		getConnectors: function () {
			return this._connectors.discover();
		},

		/**
		 * @instance
		 * @returns perspective
		 */
		getPerspective: function () {
			return this.perspective;
		},

		/**
		 * @instance
		 * @returns {promise}
		 */
		getScreenshot: function (context) {
			return this._shareableItems.getScreenshot(context);
		},

		/**
		 * @instance
		 * @returns {promise}
		 */
		canCaptureImage: function(context) {
			return this._shareableItems.canCaptureImage(context);
		},

		/**
		 * @instance
		 * @returns {promise}
		 */
		getLink: function (context) {
			return this._shareableItems.getLink(context);
		},

		/**
		 * @instance
		 * @returns {promise}
		 */
		close: function () {
			return Promise.resolve();
		},

		/**
		 * Gets localized strings for asset properties such as type and title.
		 * @return {{assetTitle: string, assetSubTitle: string, assetType: string, assetSubTitleObj: object}}
		 */
		getAssetStrings: function () {
			const view = this.glassContext.getCurrentContentView();
			const type = this.slideout && this.slideout.content && this.slideout.content.type;
			return {
				assetType: type ? type.assetType : this.getAssetType(view.perspective),
				assetTitle: type ? type.assetTitle : (view.getTitle && view.getTitle()),
				assetSubTitle: this.getSubviewTitle(view),
				assetSubTitleObj: this.getSubviewTitleObj(view)
			};
		},

		/**
		 * @instance
		 * @param {object} connector
		 * @param {object} data
		 * @returns {promise}
		 */
		send: function (connector, data) {
			return connector.send(Object.assign(data, this.getAssetStrings()));
		},

		/**
		 * Gets asset subview title.
		 * @param {string} view
		 * @return {string}
		 */
		getSubviewTitle: function (view) {
			var func = PERSPECTIVE_SUBTITLE_MAP[view.perspective];
			return func && func(view);
		},

		/**
		 * Gets asset subview title of the object.
		 * @param {string} view
		 * @return {string}
		 */
		getSubviewTitleObj: function (view) {
			var func = PERSPECTIVE_SUBTITLE_OBJ_MAP[view.perspective];
			return func && func(view);
		},

		/**
		 * Gets asset type by perspective
		 * @param {string} perspective
		 * @return {string}
		 */
		getAssetType: function (perspective) {
			return AssetTypeUtil.getLocalizedTypeByPerspective(perspective);
		},

		/**
		 * Notifies share action controller after share slideout shows.
		 *
		 * @instance
		 * @returns {Promise}
		 */
		enterShareState: function () {
			return this._shareableItems.enterShareState(_.pick(this, ['slideout', 'glassContext']));
		},

		/**
		 * Notifies share action controller after share slideout hides.
		 *
		 * @instance
		 * @returns {Promise}
		 */
		leaveShareState: function () {
			return this._shareableItems.leaveShareState(_.pick(this, ['slideout', 'glassContext']));
		},

		/**
		 * Asks the share action controller if export is supported.
		 *
		 * @instance
		 * @returns {Promise}
		 */
		canExportToPDF: function (glassContext) {
			return this._shareableItems.getActionController(glassContext)
				.then(function (actionController) {
					if (actionController && actionController.canExportToPDF) {
						return actionController.canExportToPDF({ glassContext });
					}
					return false;
				}.bind(this));
		},

		/**
		 * Tells the share action controller if export is supported. This will
		 * only ever be called if canExportToPDF() returned true
		 *
		 * @instance
		 * @returns {Promise}
		 */
		exportToPDF: function (glassContext, pageSize, printFilters) {
			return this._shareableItems.getActionController(glassContext)
				.then(function (actionController) {
					if (actionController && actionController.exportToPDF) {
						return actionController.exportToPDF({ glassContext }, pageSize, printFilters);
					}
				}.bind(this));
		},

		/**
		 * Instrument a Share gesture.
		 *
		 * @instance
		 * @returns {Promise}
		 */
		getInstrumentation: function (glassContext) {
			return this._shareableItems.getActionController(glassContext)
				.then((actionController) => {
					let instrumentData;
					if (actionController && typeof actionController.getInstrumentation === 'function') {
						instrumentData = actionController.getInstrumentation({ glassContext });
					}
					return instrumentData || {};
				});
		}
	});

	return ShareController;
});