api_sharing_ShareableItems.js.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: api/sharing/ShareableItems.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: api/sharing/ShareableItems.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * Licensed Materials - Property of IBM
  21. * IBM Cognos Products: Collaboration
  22. * (C) Copyright IBM Corp. 2017, 2019
  23. *
  24. * US Government Users Restricted Rights - Use, duplication or disclosure
  25. * restricted by GSA ADP Schedule Contract with IBM Corp.
  26. */
  27. define([
  28. 'jquery',
  29. '../../lib/@waca/core-client/js/core-client/ui/core/Class',
  30. '../../lib/@waca/baglass/js/baglass/utils/Utils',
  31. '../../lib/@waca/baglass/js/baglass/api/Url',
  32. '../../nls/StringResources',
  33. './GenerateImage'
  34. ], function ($, Class, Utils, Url, StringResources, GenerateImage) {
  35. 'use strict';
  36. const HTML_2_CANVAS_PROXY = 'v1/collaboration/proxy/html2canvas';
  37. const SHARE_CONTEXTUAL_ACTION_KEY = 'com.ibm.ca.collaboration.shareContextual';
  38. const SVG_NO_IMAGE = '&lt;svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">' +
  39. '&lt;rect x="0" y="0" width="100%" height="100%" fill="#eaeaea"/>' +
  40. '&lt;g transform="translate(-64, -64)">' +
  41. '&lt;svg x="50%" y="50%" width="128" height="128">' +
  42. '&lt;g fill="#c0bfc0">' +
  43. '&lt;path d="M64,12L4,116h120L64,12z M64,20.004L117.074,112H10.926L64,20.004z"/>' +
  44. '&lt;polygon points="60,56 60,64 62,84 66,84 68,64 68,56"/>' +
  45. '&lt;circle cx="64" cy="92" r="4"/>' +
  46. '&lt;/g>' +
  47. '&lt;/svg>' +
  48. '&lt;/g>' +
  49. '&lt;/svg>';
  50. const ShareableItems = Class.extend( /** @lends ShareableItems */ {
  51. /**
  52. * @desc Constructor for ShareableItems.
  53. * @constructs ShareableItems
  54. * @extends Class
  55. * @public
  56. * @param {object} options
  57. * @param {object} options.logger
  58. */
  59. init: function (options) {
  60. this._url = new Url();
  61. this._proxy = options &amp;&amp; options.html2canvasProxy || HTML_2_CANVAS_PROXY;
  62. this.setLogger(options);
  63. },
  64. setLogger: function (options) {
  65. if (options &amp;&amp; options.glassContext) {
  66. this._logger = options.glassContext.getCoreSvc('.Logger');
  67. } else {
  68. this._logger = null;
  69. }
  70. },
  71. /**
  72. * Returns an object containing a link representing the current context.
  73. *
  74. * @instance
  75. * @param {object} glassContext
  76. * @param {object} target
  77. * @returns {Promise} resolved as an object that contains shareUrl and embedUrl
  78. */
  79. getLink: function (glassContext, target) {
  80. return this._isValid(glassContext, target)
  81. .then(function (isValid) {
  82. //if target exist, means share accessed from context menu so no need to validate
  83. if (!target &amp;&amp; !isValid) {
  84. return '';
  85. }
  86. return this._getPublicContext(glassContext, target)
  87. .then(function (publicContext) {
  88. return this._getActionController(glassContext, publicContext, target)
  89. .then(function (actionController) {
  90. return this._url.getUrlMap(actionController, glassContext, publicContext)
  91. .then(function (urlMap) {
  92. if (urlMap.link) {
  93. // special case: take the link as-is
  94. return { shareUrl: urlMap.link };
  95. }
  96. return {
  97. shareUrl: this._url.getUrl({ urlMap: urlMap }, glassContext),
  98. embedUrl: this._url.getUrl({ urlMap: urlMap, isEmbed: true }, glassContext)
  99. };
  100. }.bind(this));
  101. }.bind(this));
  102. }.bind(this));
  103. }.bind(this));
  104. },
  105. _isValid: function (glassContext, target) {
  106. return Promise.resolve(!!this._getAssetId(glassContext, target));
  107. },
  108. _getResourceToShare: function (glassContext, target) {
  109. return this._getPublicContext(glassContext, target)
  110. .then(function (publicContext) {
  111. return this._getActionController(glassContext, publicContext, target)
  112. .then(function (actionController) {
  113. return this._getShareableItems(actionController, publicContext)
  114. .then(function (items) {
  115. const item = items[0];
  116. return {
  117. el: item.el &amp;&amp; item.el.length ? item.el[0] : item.el,
  118. label: item.label,
  119. type: publicContext.urlMap.type
  120. };
  121. }.bind(this));
  122. }.bind(this));
  123. }.bind(this))
  124. .catch(function (err) {
  125. if (this._logger) {
  126. this._logger.error(err);
  127. }
  128. throw err;
  129. }.bind(this));
  130. },
  131. /**
  132. * Returns an object containing an image representing the current context.
  133. *
  134. * @instance
  135. * @param {object} glassContext
  136. * @returns {Promise} with image (as text).
  137. */
  138. getScreenshot: function (glassContext, target) {
  139. return this._getResourceToShare(glassContext, target)
  140. .then(resource => {
  141. return this._buildItem(resource.el, resource.label, resource.type);
  142. });
  143. },
  144. /**
  145. * Check whether it's possible to screenshot the current context.
  146. *
  147. * @param {*} glassContext
  148. * @param {*} target
  149. * @returns {Promise} that resolves to true or false.
  150. */
  151. canCaptureImage: function (glassContext, target) {
  152. const isPdfReport = (resource) => {
  153. const reportTypes = [ 'report', 'reportView', 'output' ];
  154. if (reportTypes.indexOf(resource.type) > -1) {
  155. return $(resource.el).find('div#idPdfViewer').is(':visible');
  156. }
  157. return false;
  158. };
  159. return this._getResourceToShare(glassContext, target)
  160. .then(resource => {
  161. if (isPdfReport(resource)) {
  162. return false;
  163. }
  164. // Add more checks here to detect resources that can't be captured.
  165. return true;
  166. });
  167. },
  168. /**
  169. * Gets share action controller
  170. *
  171. * @instance
  172. * @param {object} glassContext
  173. * @returns {Promise}
  174. */
  175. getActionController: function (glassContext, target) {
  176. return this._getPublicContext(glassContext, target)
  177. .then(this._getActionController.bind(this, glassContext));
  178. },
  179. /**
  180. * Notifies share action controller after share slideout shows.
  181. * @param {object} options
  182. * @param {object} options.glassContext
  183. * @param {object} options.slideout
  184. * @instance
  185. * @returns {Promise}
  186. */
  187. enterShareState: function (options) {
  188. return this.getActionController(options.glassContext)
  189. .then(function (actionController) {
  190. if (actionController &amp;&amp; actionController.enterShareState) {
  191. return actionController.enterShareState(options);
  192. }
  193. }.bind(this));
  194. },
  195. /**
  196. * Notifies share action controller after share slideout hides.
  197. * @param {object} options.glassContext
  198. * @param {object} options.slideout
  199. * @instance
  200. * @returns {Promise}
  201. */
  202. leaveShareState: function (options) {
  203. return this.getActionController(options.glassContext)
  204. .then(function (actionController) {
  205. if (actionController &amp;&amp; actionController.leaveShareState) {
  206. return actionController.leaveShareState(options);
  207. }
  208. }.bind(this));
  209. },
  210. _getActionController: function (glassContext, context, target) {
  211. const type = context.urlMap.type;
  212. const selection = target &amp;&amp; target.plugin &amp;&amp; target.plugin.activeObject &amp;&amp; target.plugin.activeObject.aSelectedContext;
  213. return Utils.getSharedResourceActionController(glassContext, type, selection);
  214. },
  215. _getPublicContext: function (glassContext, target) {
  216. const isDefaultAction = !!target;
  217. target = target || {};
  218. return new Promise(function (resolve) {
  219. const publicContext = {
  220. urlMap: {
  221. objRef: this._getResourceObjRef(glassContext, target),
  222. type: this._getResourceType(glassContext, target)
  223. },
  224. mode: this._getMode(glassContext, target),
  225. isDefaultAction: isDefaultAction,
  226. target: target,
  227. glassContext: glassContext // required by getUrlMap
  228. };
  229. const mode = this._getResourceMode(glassContext, target);
  230. if (mode) {
  231. publicContext.urlMap.mode = mode;
  232. }
  233. resolve(publicContext);
  234. }.bind(this));
  235. },
  236. _getShareableItems: function (actionController, publicContext) {
  237. return new Promise(function (resolve, reject) {
  238. if (actionController &amp;&amp; typeof actionController.getShareableItems === 'function') {
  239. resolve(actionController.getShareableItems(publicContext));
  240. } else {
  241. reject(new Error(StringResources.get('error_not_implemented')));
  242. }
  243. });
  244. },
  245. _getGenerator: function () {
  246. return new GenerateImage(this._getGenerateImageOptions());
  247. },
  248. _getGenerateImageOptions: function () {
  249. return {
  250. logger: this._logger,
  251. proxy: this._proxy,
  252. elementMap: {
  253. 'mediaWidget': SVG_NO_IMAGE,
  254. 'webpageWidget': SVG_NO_IMAGE
  255. }
  256. };
  257. },
  258. _buildItem: function (el, label, resourceType) {
  259. const item = {};
  260. item.label = label;
  261. // TEMP FIX FOR DEMO: delay the image generation by 1sec to help with animation
  262. // See 223989: [Collaboration]: screenshot panel is rolled into share panel during screenshot capture
  263. return new Promise(function (resolve, reject) {
  264. const generator = this._getGenerator();
  265. window.setTimeout(function () {
  266. generator
  267. .generateImage(el, null, resourceType)
  268. .then(function (image) {
  269. item.image = image;
  270. return item;
  271. })
  272. .then(resolve)
  273. .catch(reject)
  274. .finally(function () {
  275. if (generator) {
  276. generator.destroy();
  277. }
  278. });
  279. }, 1000);
  280. }.bind(this));
  281. },
  282. _getResourceType: function (glassContext, target) {
  283. let type;
  284. try {
  285. type = target.plugin.options[0].type;
  286. } catch (e) {
  287. type = glassContext.currentAppView.getType();
  288. }
  289. return type;
  290. },
  291. _getResourceObjRef: function (glassContext, target) {
  292. let objRef;
  293. try {
  294. objRef = target.plugin.options[0].id;
  295. } catch (e) {
  296. objRef = this._getAssetId(glassContext, target);
  297. }
  298. return objRef;
  299. },
  300. _getAssetId: function (glassContext, target) {
  301. const content = glassContext.currentAppView.getContent();
  302. //Dashboard, Exploration, Storytelling &amp; Data Modules updates content.objRef when an asset is saved
  303. //Report and Datasets updates content.application.storeID when it is saved
  304. //Notebooks update content.id
  305. const type = this._getResourceType(glassContext, target);
  306. const id = type === 'jupyterNotebook' ? content.id : content.objRef;
  307. return id || (content.application &amp;&amp; content.application.storeID);
  308. },
  309. _getResourceMode: function (glassContext, target) {
  310. let mode;
  311. try {
  312. const obj = target.activeObject.aSelectedContext[0];
  313. if (obj.defaultScreenTip === 'story') {
  314. mode = 'story';
  315. }
  316. } catch (e) {
  317. const content = glassContext.currentAppView.getContent();
  318. mode = content.mode;
  319. if (mode === undefined &amp;&amp; content.isStoryMode) {
  320. // When we open a story then reload the page
  321. mode = 'story';
  322. }
  323. }
  324. return mode;
  325. },
  326. _getMode: function (glassContext, target) {
  327. try {
  328. const isDynamic = target.itemId === SHARE_CONTEXTUAL_ACTION_KEY;
  329. return isDynamic ? Url.MODES.DYNAMIC : Url.MODES.CURRENT;
  330. } catch (e) {
  331. return Url.MODES.CURRENT;
  332. }
  333. }
  334. });
  335. return ShareableItems;
  336. });</code></pre>
  337. </article>
  338. </section>
  339. </div>
  340. <nav>
  341. <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>
  342. </nav>
  343. <br class="clear">
  344. <footer>
  345. 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)
  346. </footer>
  347. <script> prettyPrint(); </script>
  348. <script src="scripts/linenumber.js"> </script>
  349. </body>
  350. </html>