DuplicateAction.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(['../../../../app/nls/StringResources', '../../../../lib/@waca/dashboard-common/dist/ui/interaction/Utils', 'jquery', 'underscore', '../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../../lib/@waca/dashboard-common/dist/api/ContentActionsProviderAPI'], function (stringResources, Utils, $, _, APIFactory, ContentActionsProviderAPI) {
  9. var DuplicateAction = function () {
  10. function DuplicateAction(_ref) {
  11. var features = _ref.features;
  12. _classCallCheck(this, DuplicateAction);
  13. if (features) {
  14. this.dashboard = features.API;
  15. features.ContentActions.registerProvider('duplicate', this.getAPI());
  16. this._dashboardState = features.DashboardState;
  17. this._icons = features.Icons;
  18. }
  19. }
  20. DuplicateAction.prototype.getAPI = function getAPI() {
  21. if (!this._api) {
  22. this._api = APIFactory.createAPI(this, [ContentActionsProviderAPI]);
  23. }
  24. return this._api;
  25. };
  26. DuplicateAction.prototype.getLifeCycleHandlers = function getLifeCycleHandlers() {
  27. return [{
  28. name: 'post:dashboard.initialize',
  29. action: this.postDashboardInitialize.bind(this)
  30. }];
  31. };
  32. DuplicateAction.prototype.postDashboardInitialize = function postDashboardInitialize() {
  33. if (this.dashboard) {
  34. this.controller = this.dashboard.getFeature('InteractionController.internal');
  35. }
  36. return Promise.resolve();
  37. };
  38. DuplicateAction.prototype.destroy = function destroy() {
  39. this.dashboard = null;
  40. this.controller = null;
  41. };
  42. DuplicateAction.prototype.getNodes = function getNodes(idList) {
  43. return Utils.getNodes(this.controller, idList);
  44. };
  45. DuplicateAction.prototype._isAvailable = function _isAvailable(idList) {
  46. // Make sure duplicate action is not available for filter widget
  47. return idList.length && this._isSupportedVisualizations(idList) && this.dashboard.getMode() === this.dashboard.MODES.EDIT && _.find(this.getNodes(idList), function (n) {
  48. return $(n).hasClass('filterWidget');
  49. }) === undefined;
  50. };
  51. DuplicateAction.prototype._isSupportedVisualizations = function _isSupportedVisualizations(idList) {
  52. for (var index = 0; index < idList.length; index++) {
  53. if (this._isNotSupportedVisualization(idList[index])) {
  54. return false;
  55. }
  56. }
  57. return true;
  58. };
  59. DuplicateAction.prototype._isNotSupportedVisualization = function _isNotSupportedVisualization(id) {
  60. var content = this.dashboard.getCanvas().getContent(id);
  61. if (!content) {
  62. return false;
  63. }
  64. var visualization = content.getFeature('Visualization');
  65. return content.getType() === 'widget.live' && visualization && visualization.getDefinition().getId() === 'com.ibm.vis.schematicsPreview';
  66. };
  67. DuplicateAction.prototype._isDisabled = function _isDisabled() {
  68. return this._dashboardState.getUiState().focus;
  69. };
  70. DuplicateAction.prototype.getContentActionList = function getContentActionList(idList) {
  71. if (this._isAvailable(idList)) {
  72. return [{
  73. name: 'duplicate',
  74. label: stringResources.get('duplicate'),
  75. icon: this._icons.getIcon('dashboard-duplicate').id,
  76. type: 'Button',
  77. disabled: this._isDisabled.bind(this),
  78. actions: {
  79. apply: this.duplicateContent.bind(this, idList)
  80. }
  81. }];
  82. }
  83. return [];
  84. };
  85. /**
  86. * Duplicate the selected items for the board layout
  87. * @param {String[]} idList Array of model id list.
  88. */
  89. DuplicateAction.prototype.duplicateContent = function duplicateContent(idList, evt) {
  90. var _this = this;
  91. var modelIds = [];
  92. var outBoundIds = [];
  93. //Get view port bounds
  94. var viewPortView = this.controller.layoutController.getLayoutContentContainer();
  95. _.each(this.getNodes(idList), function (node) {
  96. var modelId = this.controller.layoutController.nodeIdToModelId(node.id);
  97. var bounds = this._checkLayoutBoundLimits(node, viewPortView);
  98. if (!bounds.right || !bounds.bottom) {
  99. outBoundIds.push({ 'id': modelId, 'right': bounds.right, 'bottom': bounds.bottom });
  100. }
  101. modelIds.push(modelId);
  102. }.bind(this));
  103. var options = {
  104. modelIds: modelIds
  105. };
  106. if (outBoundIds.length > 0) {
  107. options.outBoundIds = outBoundIds;
  108. }
  109. var duplicatedIdsPromise = this.controller.boardModel.duplicateSelection(options);
  110. this.controller.selectionHandler.deselectAll();
  111. var returnedPromise = [];
  112. duplicatedIdsPromise.forEach(function (duplicatedIdPromise) {
  113. return returnedPromise.push(duplicatedIdPromise.then(function (duplicatedId) {
  114. return _this.controller.layoutController.selectLayouts([duplicatedId], evt);
  115. }).catch(function (err) {
  116. console.log(err);
  117. }));
  118. });
  119. return returnedPromise;
  120. };
  121. /**
  122. * Check if duplicate item will fall out of bound of the current consume view
  123. * Only applies for Guided Journey layouts
  124. */
  125. DuplicateAction.prototype._checkLayoutBoundLimits = function _checkLayoutBoundLimits(node, viewPort) {
  126. var shouldExpand = { right: true, bottom: true };
  127. /* Get current consume view bounds */
  128. var consumViewMaxHeight = node._layout.parentLayout.getMaximumHeight();
  129. var consumViewMaxWidth = node._layout.parentLayout.getMaximumWidth();
  130. /* Only in GJ will the numbers being finite*/
  131. if (isFinite(consumViewMaxHeight) && isFinite(consumViewMaxWidth)) {
  132. /* Get the view port bounds */
  133. var viewPortMaxHeight = viewPort.clientHeight;
  134. var viewPortMaxWidth = viewPort.clientWidth;
  135. /* Should be finite. Check just in case */
  136. if (isFinite(viewPortMaxHeight) && isFinite(viewPortMaxWidth)) {
  137. /* Get the styles from layout model for this widget */
  138. var layOutModel = node._layout.model;
  139. var style = layOutModel.style;
  140. if (style) {
  141. /*
  142. * Calculate the bounds based on a 5% down/right move of the current node
  143. * The style contains % of the view port space, not the consume view
  144. */
  145. var expectedTop = parseFloat(layOutModel.incrementStyleValue(style.top)) / 100 * viewPortMaxHeight;
  146. var expectedLeft = parseFloat(layOutModel.incrementStyleValue(style.left)) / 100 * viewPortMaxWidth;
  147. /* Height and Width of this object */
  148. var objHeight = parseFloat(style.height) / 100 * viewPortMaxHeight;
  149. var objWidth = parseFloat(style.width) / 100 * viewPortMaxWidth;
  150. if (expectedTop + objHeight > consumViewMaxHeight) {
  151. shouldExpand.bottom = false;
  152. }
  153. if (expectedLeft + objWidth > consumViewMaxWidth) {
  154. shouldExpand.right = false;
  155. }
  156. }
  157. }
  158. }
  159. return shouldExpand;
  160. };
  161. return DuplicateAction;
  162. }();
  163. return DuplicateAction;
  164. });
  165. //# sourceMappingURL=DuplicateAction.js.map