DeleteAction.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/utils/DialogBlocker', '../../../../dashboard/glass/util/InstrumentationUtil', '../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../../lib/@waca/dashboard-common/dist/api/ContentActionsProviderAPI', './api/DeleteActionAPI'], function (stringResources, Utils, $, _, DialogBlocker, InstrumentationUtil, APIFactory, ContentActionsProviderAPI, DeleteActionAPI) {
  9. var DeleteAction = function () {
  10. function DeleteAction(_ref) {
  11. var _this = this;
  12. var features = _ref.features;
  13. _classCallCheck(this, DeleteAction);
  14. if (features) {
  15. this.dashboard = features.API;
  16. this._icons = features.Icons;
  17. features.ContentActions.registerProvider('delete', this.getAPI());
  18. }
  19. this.id = _.uniqueId('id');
  20. this.dashboard.getCanvasWhenReady().then(function (canvas) {
  21. _this.canvas = canvas;
  22. canvas.on('change:selections:select', _this.onContentSelectionChange, _this);
  23. canvas.on('change:selections:deselect', _this.onContentSelectionChange, _this);
  24. });
  25. }
  26. DeleteAction.prototype.getAPI = function getAPI() {
  27. if (!this._api) {
  28. this._api = APIFactory.createAPI(this, [ContentActionsProviderAPI, DeleteActionAPI]);
  29. }
  30. return this._api;
  31. };
  32. DeleteAction.prototype.getLifeCycleHandlers = function getLifeCycleHandlers() {
  33. return [{
  34. name: 'post:dashboard.initialize',
  35. action: this.postDashboardInitialize.bind(this)
  36. }];
  37. };
  38. DeleteAction.prototype.postDashboardInitialize = function postDashboardInitialize() {
  39. if (this.dashboard) {
  40. this.transactionApi = this.dashboard.getFeature('Transaction');
  41. this.controller = this.dashboard.getFeature('InteractionController.internal');
  42. }
  43. return Promise.resolve();
  44. };
  45. DeleteAction.prototype.destroy = function destroy() {
  46. if (this.canvas) {
  47. this.canvas.off('change:selections:select', this.onContentSelectionChange, this);
  48. this.canvas.off('change:selections:deselect', this.onContentSelectionChange, this);
  49. this.canvas = null;
  50. }
  51. this._deRegisterEvent();
  52. this.dashboard = null;
  53. this.controller = null;
  54. this.id = null;
  55. };
  56. DeleteAction.prototype.getNodes = function getNodes(idList) {
  57. return Utils.getNodes(this.controller, idList);
  58. };
  59. DeleteAction.prototype._isEnabled = function _isEnabled(idList) {
  60. return idList.length && this.dashboard.getMode() === this.dashboard.MODES.EDIT;
  61. };
  62. DeleteAction.prototype.getContentActionList = function getContentActionList(idList) {
  63. if (this._isEnabled(idList)) {
  64. return [{
  65. name: 'delete',
  66. label: stringResources.get('toolbarActionDelete'),
  67. icon: this._icons.getIcon('dashboard-delete').id,
  68. type: 'Button',
  69. actions: {
  70. apply: this.deleteContent.bind(this, idList)
  71. }
  72. }];
  73. }
  74. return [];
  75. };
  76. DeleteAction.prototype.onContentSelectionChange = function onContentSelectionChange() {
  77. var idList = this.canvas.getSelectedContentList().map(function (content) {
  78. return content.getId();
  79. });
  80. // TODO: The fix is temporary until we have an API to allow disabling features.
  81. var applyInteraction = this.dashboard.getAppConfig('interactions');
  82. if (applyInteraction && applyInteraction.delete === false) {
  83. return;
  84. }
  85. this._deRegisterEvent();
  86. if (this._isEnabled(idList)) {
  87. $('html').on('keyup.interactionDeleteKey' + this.id, this.deleteKeyPress.bind(this, idList));
  88. }
  89. };
  90. DeleteAction.prototype._deRegisterEvent = function _deRegisterEvent() {
  91. $('html').off('keyup.interactionDeleteKey' + this.id);
  92. };
  93. DeleteAction.prototype.deleteKeyPress = function deleteKeyPress(idList, e) {
  94. var target = e.srcElement || e.target;
  95. // If the event is triggered in an input/text area do not delete items i.e. Filter dialogue on widgets
  96. if (target.tagName.toLowerCase().match(/input|textarea|select/igm)) {
  97. return;
  98. }
  99. // If the event is triggered on the element within slot editor, we do not handle it
  100. if (this._elementIsInSlotEditor(target)) {
  101. return;
  102. }
  103. if (!e.ctrlKey && (e.keyCode === 46 || e.keyCode === 8) && !DialogBlocker.isShowingDialogBlocker()) {
  104. return this.deleteContent(idList);
  105. }
  106. };
  107. DeleteAction.prototype._elementIsInSlotEditor = function _elementIsInSlotEditor(e) {
  108. // All of the elements in slot editor have the common parent of SlotEditor
  109. return e.closest('.slotEditor');
  110. };
  111. DeleteAction.prototype.setOnBeforeDeleteCallback = function setOnBeforeDeleteCallback(callback) {
  112. this.onBeforeDeleteCallback = callback;
  113. };
  114. /**
  115. * Delete the selected items for the board layout
  116. * @param {String[]} idList Array of model id list.
  117. */
  118. DeleteAction.prototype.deleteContent = function deleteContent(idList) {
  119. var _this2 = this;
  120. // it is possible that we are receiving the delete key from a different view.
  121. // make sure that we only delete if the view element is visible.
  122. if (this.controller.$el.is(':visible')) {
  123. var widgetId = void 0;
  124. var deleteIDs = [];
  125. var ungroupedNodesList = [];
  126. var transactionToken = this.transactionApi.startTransaction();
  127. var undoRedoTransactionId = transactionToken.transactionId;
  128. // A delete operation might include ungrouping an item if we are delete an item that is part of a group.
  129. // In order to group all these in one undo action, we need to create a transaction id and pass it along.
  130. if (this.onBeforeDeleteCallback) {
  131. ungroupedNodesList = this.onBeforeDeleteCallback({ 'currentSelection': idList, 'undoRedoTransactionId': undoRedoTransactionId, 'transactionToken': transactionToken });
  132. } else {
  133. ungroupedNodesList = this.getNodes(idList);
  134. }
  135. for (var i = 0; i < ungroupedNodesList.length; i++) {
  136. widgetId = this.controller.layoutController.nodeIdToModelId(ungroupedNodesList[i].id);
  137. deleteIDs.push(widgetId);
  138. InstrumentationUtil.trackWidget('deleted', this.dashboard, null, widgetId);
  139. if (ungroupedNodesList[i]._layout && ungroupedNodesList[i]._layout.widgetChromeEventRouter) {
  140. ungroupedNodesList[i]._layout.widgetChromeEventRouter.trigger('widget:onDeleteAction', { 'data': { 'undoRedoTransactionId': undoRedoTransactionId, 'transactionToken': transactionToken } });
  141. }
  142. }
  143. this.controller.selectionHandler.deselectAll();
  144. deleteIDs.forEach(function (id) {
  145. _this2.dashboard.getCanvas().removeContent(id, transactionToken);
  146. });
  147. return this._deleteAfter({ ids: deleteIDs, undoRedoTransactionId: undoRedoTransactionId }, transactionToken);
  148. }
  149. };
  150. DeleteAction.prototype._deleteAfter = function _deleteAfter(payload, transactionToken) {
  151. var _this3 = this;
  152. return this.dashboard.getFeature('.LifeCycleManager').invokeLifeCycleHandlers('post:widget.delete', payload).then(function () {
  153. _this3.transactionApi.endTransaction(transactionToken);
  154. return _this3.controller.onSelectionDelete();
  155. });
  156. };
  157. return DeleteAction;
  158. }();
  159. return DeleteAction;
  160. });
  161. //# sourceMappingURL=DeleteAction.js.map