| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 | <!DOCTYPE html><html lang="en"><head>    <meta charset="utf-8">    <title>JSDoc: Source: api/sharing/ShareableItems.js</title>    <script src="scripts/prettify/prettify.js"> </script>    <script src="scripts/prettify/lang-css.js"> </script>    <!--[if lt IE 9]>      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>    <![endif]-->    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"></head><body><div id="main">    <h1 class="page-title">Source: api/sharing/ShareableItems.js</h1>            <section>        <article>            <pre class="prettyprint source linenums"><code>/** * Licensed Materials - Property of IBM * IBM Cognos Products: Collaboration * (C) Copyright IBM Corp. 2017, 2019 * * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */define([	'jquery',	'../../lib/@waca/core-client/js/core-client/ui/core/Class',	'../../lib/@waca/baglass/js/baglass/utils/Utils',	'../../lib/@waca/baglass/js/baglass/api/Url',	'../../nls/StringResources',	'./GenerateImage'], function ($, Class, Utils, Url, StringResources, GenerateImage) {	'use strict';	const HTML_2_CANVAS_PROXY = 'v1/collaboration/proxy/html2canvas';	const SHARE_CONTEXTUAL_ACTION_KEY = 'com.ibm.ca.collaboration.shareContextual';	const SVG_NO_IMAGE = '<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">' +		'<rect x="0" y="0" width="100%" height="100%" fill="#eaeaea"/>' +		'<g transform="translate(-64, -64)">' +		'<svg x="50%" y="50%" width="128" height="128">' +		'<g fill="#c0bfc0">' +		'<path d="M64,12L4,116h120L64,12z M64,20.004L117.074,112H10.926L64,20.004z"/>' +		'<polygon points="60,56 60,64 62,84 66,84 68,64 68,56"/>' +		'<circle cx="64" cy="92" r="4"/>' +		'</g>' +		'</svg>' +		'</g>' +		'</svg>';	const ShareableItems = Class.extend( /** @lends ShareableItems */ {		/**		 * @desc Constructor for ShareableItems.		 * @constructs ShareableItems		 * @extends Class		 * @public		 * @param {object} options		 * @param {object} options.logger		 */		init: function (options) {			this._url = new Url();			this._proxy = options && options.html2canvasProxy || HTML_2_CANVAS_PROXY;			this.setLogger(options);		},		setLogger: function (options) {			if (options && options.glassContext) {				this._logger = options.glassContext.getCoreSvc('.Logger');			} else {				this._logger = null;			}		},		/**		 * Returns an object containing a link representing the current context.		 *		 * @instance		 * @param {object} glassContext		 * @param {object} target		 * @returns {Promise} resolved as an object that contains shareUrl and embedUrl		 */		getLink: function (glassContext, target) {			return this._isValid(glassContext, target)				.then(function (isValid) {					//if target exist, means share accessed from context menu so no need to validate					if (!target && !isValid) {						return '';					}					return this._getPublicContext(glassContext, target)						.then(function (publicContext) {							return this._getActionController(glassContext, publicContext, target)								.then(function (actionController) {									return this._url.getUrlMap(actionController, glassContext, publicContext)										.then(function (urlMap) {											if (urlMap.link) {												// special case: take the link as-is												return { shareUrl: urlMap.link };											}											return {												shareUrl: this._url.getUrl({ urlMap: urlMap }, glassContext),												embedUrl: this._url.getUrl({ urlMap: urlMap, isEmbed: true }, glassContext)											};										}.bind(this));								}.bind(this));						}.bind(this));				}.bind(this));		},		_isValid: function (glassContext, target) {			return Promise.resolve(!!this._getAssetId(glassContext, target));		},		_getResourceToShare: function (glassContext, target) {			return this._getPublicContext(glassContext, target)				.then(function (publicContext) {					return this._getActionController(glassContext, publicContext, target)						.then(function (actionController) {							return this._getShareableItems(actionController, publicContext)								.then(function (items) {									const item = items[0];									return {										el: item.el && item.el.length ? item.el[0] : item.el,										label: item.label,										type: publicContext.urlMap.type									};								}.bind(this));						}.bind(this));				}.bind(this))				.catch(function (err) {					if (this._logger) {						this._logger.error(err);					}					throw err;				}.bind(this));		},		/**		 * Returns an object containing an image representing the current context.		 *		 * @instance		 * @param {object} glassContext		 * @returns {Promise} with image (as text).		 */		getScreenshot: function (glassContext, target) {			return this._getResourceToShare(glassContext, target)				.then(resource => {					return this._buildItem(resource.el, resource.label, resource.type);				});		},		/**		 * Check whether it's possible to screenshot the current context.		 * 		 * @param {*} glassContext 		 * @param {*} target 		 * @returns {Promise} that resolves to true or false.		 */		canCaptureImage: function (glassContext, target) {			const isPdfReport = (resource) => {				const reportTypes = [ 'report', 'reportView', 'output' ];				if (reportTypes.indexOf(resource.type) > -1) {					return $(resource.el).find('div#idPdfViewer').is(':visible');				}				return false;			};			return this._getResourceToShare(glassContext, target)				.then(resource => {					if (isPdfReport(resource)) {						return false;					}					// Add more checks here to detect resources that can't be captured.					return true;				});		},		/**		 * Gets share action controller		 *		 * @instance		 * @param {object} glassContext		 * @returns {Promise}		 */		getActionController: function (glassContext, target) {			return this._getPublicContext(glassContext, target)				.then(this._getActionController.bind(this, glassContext));		},		/**		 * Notifies share action controller after share slideout shows.		 * @param {object} options		 * @param {object} options.glassContext		 * @param {object} options.slideout		 * @instance		 * @returns {Promise}		 */		enterShareState: function (options) {			return this.getActionController(options.glassContext)				.then(function (actionController) {					if (actionController && actionController.enterShareState) {						return actionController.enterShareState(options);					}				}.bind(this));		},		/**		 * Notifies share action controller after share slideout hides.		 * @param {object} options.glassContext		 * @param {object} options.slideout		 * @instance		 * @returns {Promise}		 */		leaveShareState: function (options) {			return this.getActionController(options.glassContext)				.then(function (actionController) {					if (actionController && actionController.leaveShareState) {						return actionController.leaveShareState(options);					}				}.bind(this));		},		_getActionController: function (glassContext, context, target) {			const type = context.urlMap.type;			const selection = target && target.plugin && target.plugin.activeObject && target.plugin.activeObject.aSelectedContext;			return Utils.getSharedResourceActionController(glassContext, type, selection);		},		_getPublicContext: function (glassContext, target) {			const isDefaultAction = !!target;			target = target || {};			return new Promise(function (resolve) {				const publicContext = {					urlMap: {						objRef: this._getResourceObjRef(glassContext, target),						type: this._getResourceType(glassContext, target)					},					mode: this._getMode(glassContext, target),					isDefaultAction: isDefaultAction,					target: target,					glassContext: glassContext // required by getUrlMap				};				const mode = this._getResourceMode(glassContext, target);				if (mode) {					publicContext.urlMap.mode = mode;				}				resolve(publicContext);			}.bind(this));		},		_getShareableItems: function (actionController, publicContext) {			return new Promise(function (resolve, reject) {				if (actionController && typeof actionController.getShareableItems === 'function') {					resolve(actionController.getShareableItems(publicContext));				} else {					reject(new Error(StringResources.get('error_not_implemented')));				}			});		},		_getGenerator: function () {			return new GenerateImage(this._getGenerateImageOptions());		},		_getGenerateImageOptions: function () {			return {				logger: this._logger,				proxy: this._proxy,				elementMap: {					'mediaWidget': SVG_NO_IMAGE,					'webpageWidget': SVG_NO_IMAGE				}			};		},		_buildItem: function (el, label, resourceType) {			const item = {};			item.label = label;			// TEMP FIX FOR DEMO: delay the image generation by 1sec to help with animation			// See 223989: [Collaboration]: screenshot panel is rolled into share panel during screenshot capture			return new Promise(function (resolve, reject) {				const generator = this._getGenerator();				window.setTimeout(function () {					generator						.generateImage(el, null, resourceType)						.then(function (image) {							item.image = image;							return item;						})						.then(resolve)						.catch(reject)						.finally(function () {							if (generator) {								generator.destroy();							}						});				}, 1000);			}.bind(this));		},		_getResourceType: function (glassContext, target) {			let type;			try {				type = target.plugin.options[0].type;			} catch (e) {				type = glassContext.currentAppView.getType();			}			return type;		},		_getResourceObjRef: function (glassContext, target) {			let objRef;			try {				objRef = target.plugin.options[0].id;			} catch (e) {				objRef = this._getAssetId(glassContext, target);			}			return objRef;		},		_getAssetId: function (glassContext, target) {			const content = glassContext.currentAppView.getContent();			//Dashboard, Exploration, Storytelling & Data Modules updates content.objRef when an asset is saved			//Report and Datasets updates content.application.storeID when it is saved			//Notebooks update content.id			const type = this._getResourceType(glassContext, target);			const id = type === 'jupyterNotebook' ? content.id : content.objRef;			return id || (content.application && content.application.storeID);		},		_getResourceMode: function (glassContext, target) {			let mode;			try {				const obj = target.activeObject.aSelectedContext[0];				if (obj.defaultScreenTip === 'story') {					mode = 'story';				}			} catch (e) {				const content = glassContext.currentAppView.getContent();				mode = content.mode;				if (mode === undefined && content.isStoryMode) {					// When we open a story then reload the page					mode = 'story';				}			}			return mode;		},		_getMode: function (glassContext, target) {			try {				const isDynamic = target.itemId === SHARE_CONTEXTUAL_ACTION_KEY;				return isDynamic ? Url.MODES.DYNAMIC : Url.MODES.CURRENT;			} catch (e) {				return Url.MODES.CURRENT;			}		}	});	return ShareableItems;});</code></pre>        </article>    </section></div><nav>    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ConnectorBase.html">ConnectorBase</a></li><li><a href="Connectors.html">Connectors</a></li><li><a href="EmailClient.html">EmailClient</a></li><li><a href="EmailConnector.html">EmailConnector</a></li><li><a href="MSTeamsAuth.html">MSTeamsAuth</a></li><li><a href="MSTeamsClient.html">MSTeamsClient</a></li><li><a href="MSTeamsConnector.html">MSTeamsConnector</a></li><li><a href="ShareableItems.html">ShareableItems</a></li><li><a href="ShareController.html">ShareController</a></li><li><a href="ShareView.html">ShareView</a></li><li><a href="SlackAuth.html">SlackAuth</a></li><li><a href="SlackClient.html">SlackClient</a></li><li><a href="SlackConnector.html">SlackConnector</a></li></ul><h3>Interfaces</h3><ul><li><a href="ShareInterface.html">ShareInterface</a></li></ul></nav><br class="clear"><footer>    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed May 25 2022 13:54:53 GMT+0000 (UTC)</footer><script> prettyPrint(); </script><script src="scripts/linenumber.js"> </script></body></html>
 |