PinAction.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../../../lib/@waca/dashboard-common/dist/ui/interaction/Utils', '../../../../lib/@waca/core-client/js/core-client/ui/ProgressToast', '../../../../app/nls/StringResources', '../../../../dashboard/util/DashboardSpecHelper', '../../../../app/util/ErrorUtils', 'jquery', '../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../../lib/@waca/dashboard-common/dist/api/ContentActionsProviderAPI', 'underscore'], function (Util, ProgressToast, stringResources, DashboardSpecHelper, ErrorUtils, $, APIFactory, ContentActionsProviderAPI, _) {
  9. var placeholderMappings = {
  10. infographic: 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOS4wLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vc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Qo8L2c+DQo8L3N2Zz4NCg=='
  11. };
  12. var PinAction = function () {
  13. function PinAction(_ref) {
  14. var features = _ref.features;
  15. _classCallCheck(this, PinAction);
  16. if (features) {
  17. this.dashboard = features.API;
  18. this.toast = {};
  19. features.ContentActions.registerProvider('pin', this.getAPI());
  20. this._icons = features.Icons;
  21. }
  22. }
  23. PinAction.prototype.getAPI = function getAPI() {
  24. if (!this._api) {
  25. this._api = APIFactory.createAPI(this, [ContentActionsProviderAPI]);
  26. }
  27. return this._api;
  28. };
  29. PinAction.prototype.getLifeCycleHandlers = function getLifeCycleHandlers() {
  30. return [{
  31. name: 'post:dashboard.initialize',
  32. action: this.postDashboardInitialize.bind(this)
  33. }];
  34. };
  35. PinAction.prototype.postDashboardInitialize = function postDashboardInitialize() {
  36. this.controller = this.dashboard.getFeature('InteractionController.internal');
  37. this.specHelper = new DashboardSpecHelper(this.controller);
  38. return Promise.resolve();
  39. };
  40. PinAction.prototype._isEnabled = function _isEnabled(idList) {
  41. return idList.length && this._isSupportedVisualizations(idList) && this.dashboard.getMode() !== this.dashboard.MODES.EDIT_GROUP && ErrorUtils.hasCapability(this.dashboard, 'canAuthorDashboard');
  42. };
  43. PinAction.prototype._isSupportedVisualizations = function _isSupportedVisualizations(idList) {
  44. for (var index = 0; index < idList.length; index++) {
  45. if (this._isNotSupportedVisualization(idList[index])) {
  46. return false;
  47. }
  48. }
  49. return true;
  50. };
  51. PinAction.prototype._isNotSupportedVisualization = function _isNotSupportedVisualization(id) {
  52. var content = this.dashboard.getCanvas().getContent(id);
  53. if (!content) {
  54. return false;
  55. }
  56. var visualization = content.getFeature('Visualization');
  57. return content.getType() === 'widget.live' && visualization && visualization.getDefinition().getId() === 'com.ibm.vis.schematicsPreview';
  58. };
  59. PinAction.prototype.getContentActionList = function getContentActionList(idList) {
  60. if (this._isEnabled(idList)) {
  61. return [{
  62. name: 'pin',
  63. label: stringResources.get('toolbarActionPinOld'),
  64. icon: this._icons.getIcon('pin').id,
  65. type: 'Button',
  66. actions: {
  67. apply: this.pinItems.bind(this)
  68. }
  69. }];
  70. } else {
  71. return [];
  72. }
  73. };
  74. PinAction.prototype.pinItems = function pinItems() {
  75. var _this = this;
  76. return new Promise(function (resolve, reject) {
  77. var _pinItems = function _pinItems(toast_id) {
  78. var promises = [];
  79. var boardName = _this.controller.boardModel.name;
  80. var path = _this._createPath();
  81. var pinContents = _this.specHelper.getContents(true);
  82. pinContents.forEach(function (pinContent) {
  83. var pin = {
  84. source: 'authoredview',
  85. sourceName: boardName,
  86. sourceUri: path + '?embed=ba1', // source uri needs embed link for reopen
  87. sourceInstanceId: path,
  88. contentType: 'boardFragment',
  89. content: pinContent
  90. };
  91. if (_this._shouldUsePlaceholder(pinContent.widgets)) {
  92. pin.thumbUri = _this._getPlaceholder(pinContent.widgets);
  93. _this._pinItem(pin);
  94. } else {
  95. var isGroup = pinContent.layout.type === 'group';
  96. // TODO lifecycle_cleanup -- cleanup how we access the dom here
  97. var selectedNode = $('#' + pinContent.layout.id)[0];
  98. var widgetContent = selectedNode;
  99. if (!isGroup) {
  100. widgetContent = $(widgetContent).find('.widgetContentWrapper')[0];
  101. }
  102. var nodes = {
  103. widget: selectedNode,
  104. content: widgetContent
  105. };
  106. var nodeStyles = {};
  107. _this._prepareNodeStylesForThumbnail(nodes, nodeStyles);
  108. promises.push(_this._createThumbnail(widgetContent).then(function (dataUri) {
  109. pin.thumbUri = dataUri;
  110. _this._restoreStylesAfterThumbnail(nodes, nodeStyles);
  111. }, function (err) {
  112. _this.dashboard.getGlassCoreSvc('.Logger').error(err);
  113. }).then(_this._pinItem.bind(_this, pin)));
  114. }
  115. });
  116. return Promise.all(promises).then(function () {
  117. _this.toast[toast_id].setComplete(100, {
  118. duration: 0,
  119. completeMsg: stringResources.get('pinAddedToast'),
  120. isComplete: true
  121. });
  122. resolve();
  123. }).catch(function () {
  124. _this.toast[toast_id].fail(stringResources.get('contentPinFailOld'));
  125. reject();
  126. }).finally(function () {
  127. _this.toast[toast_id].remove();
  128. _this.toast[toast_id] = null;
  129. });
  130. };
  131. var toast_id = _.uniqueId('pinToast');
  132. var toast = new ProgressToast({
  133. noCancelBtn: true,
  134. onShown: _pinItems.bind(_this, toast_id)
  135. });
  136. toast.show(stringResources.get('pinningToast'));
  137. toast.indefinite(stringResources.get('pinningToast'));
  138. _this.toast[toast_id] = toast;
  139. });
  140. };
  141. PinAction.prototype._createPath = function _createPath() {
  142. var gateway = this.controller.gatewayUrl ? this.controller.gatewayUrl : '';
  143. var boardID = this.controller.boardModel.id;
  144. return gateway + '/board/' + boardID;
  145. };
  146. /**
  147. * Determines if a placeholder should be used for a given list of widget models
  148. * @param widgetModelsForPinning - List of widget models
  149. * @returns boolean value as to whether a placeholder should be used
  150. */
  151. PinAction.prototype._shouldUsePlaceholder = function _shouldUsePlaceholder(widgetModelsForPinning) {
  152. return widgetModelsForPinning.length === 1 && widgetModelsForPinning[0].mapping && widgetModelsForPinning[0].mapping[0] && widgetModelsForPinning[0].mapping[0].graphic;
  153. };
  154. /**
  155. * Gets the placeholder image to be shown for pinning, for the given widget model
  156. * @param widgetModelsForPinning - List of widget models
  157. * @returns placeholder (base 64 string) to be shown for pinning
  158. */
  159. PinAction.prototype._getPlaceholder = function _getPlaceholder(widgetModelsForPinning) {
  160. var placeholder;
  161. if (widgetModelsForPinning.length === 1 && widgetModelsForPinning[0].mapping && widgetModelsForPinning[0].mapping[0] && widgetModelsForPinning[0].mapping[0].graphic) {
  162. placeholder = placeholderMappings.infographic;
  163. }
  164. return placeholder;
  165. };
  166. PinAction.prototype._pinItem = function _pinItem(item) {
  167. return this.dashboard.getGlassSvc('.DashboardPinning').then(function (dashboardPinningService) {
  168. return dashboardPinningService.addPin(item);
  169. });
  170. };
  171. PinAction.prototype._prepareNodeStylesForThumbnail = function _prepareNodeStylesForThumbnail(nodes, styles) {
  172. if (nodes.widget !== nodes.content) {
  173. styles.widget = nodes.widget.getAttribute('style') || '';
  174. styles.content = nodes.content.getAttribute('style') || '';
  175. var tempWidgetStyles = [],
  176. tempContentStyles = [];
  177. styles.widget.split(';').forEach(function (styleRule) {
  178. var ruleName = styleRule.split(':')[0];
  179. switch (ruleName.trim()) {
  180. case 'width':
  181. case 'height':
  182. case 'top':
  183. case 'left':
  184. tempWidgetStyles.push(styleRule);
  185. break;
  186. default:
  187. tempContentStyles.push(styleRule);
  188. }
  189. });
  190. nodes.widget.setAttribute('style', tempWidgetStyles.join(';'));
  191. nodes.content.setAttribute('style', styles.content + ';' + tempContentStyles.join(';'));
  192. }
  193. $(nodes.widget).addClass('pinThumbnail');
  194. };
  195. PinAction.prototype._createThumbnail = function _createThumbnail(selectedNode) {
  196. return this.dashboard.getDashboardSvc('.Thumbnail').then(function (thumbnailSvc) {
  197. return thumbnailSvc.generateImage(selectedNode);
  198. });
  199. };
  200. PinAction.prototype._restoreStylesAfterThumbnail = function _restoreStylesAfterThumbnail(nodes, styles) {
  201. if (nodes.widget !== nodes.content) {
  202. nodes.widget.setAttribute('style', styles.widget);
  203. nodes.content.setAttribute('style', styles.content);
  204. }
  205. $(nodes.widget).removeClass('pinThumbnail');
  206. };
  207. return PinAction;
  208. }();
  209. return PinAction;
  210. });
  211. //# sourceMappingURL=PinAction.js.map