From 2af8dabeca08eae444ee911387f043338ec7b2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gonza=CC=81lez?= Date: Fri, 13 Sep 2024 15:57:40 +0200 Subject: [PATCH] LPD-36211 Only show confirmation when trash is disabled --- .../context/MBEditMessageDisplayContext.java | 18 ++++- .../resources/js/message_boards/MBPortlet.js | 66 ++++++++++++------- .../resources/message_boards/edit_message.jsp | 2 +- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/modules/apps/message-boards/message-boards-web/src/main/java/com/liferay/message/boards/web/internal/display/context/MBEditMessageDisplayContext.java b/modules/apps/message-boards/message-boards-web/src/main/java/com/liferay/message/boards/web/internal/display/context/MBEditMessageDisplayContext.java index 33bb7a0e262501..540e6dac613ee4 100644 --- a/modules/apps/message-boards/message-boards-web/src/main/java/com/liferay/message/boards/web/internal/display/context/MBEditMessageDisplayContext.java +++ b/modules/apps/message-boards/message-boards-web/src/main/java/com/liferay/message/boards/web/internal/display/context/MBEditMessageDisplayContext.java @@ -12,26 +12,37 @@ import com.liferay.portal.kernel.portlet.LiferayWindowState; import com.liferay.portal.kernel.portlet.PortletURLUtil; import com.liferay.portal.kernel.portlet.url.builder.PortletURLBuilder; +import com.liferay.portal.kernel.theme.ThemeDisplay; import com.liferay.portal.kernel.util.Constants; import com.liferay.portal.kernel.util.HashMapBuilder; +import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.kernel.workflow.WorkflowConstants; +import com.liferay.trash.TrashHelper; import java.util.Map; import javax.portlet.ResourceURL; +import javax.servlet.http.HttpServletRequest; + /** * @author Ambrín Chaudhary */ public class MBEditMessageDisplayContext { public MBEditMessageDisplayContext( + HttpServletRequest httpServletRequest, LiferayPortletRequest liferayPortletRequest, - LiferayPortletResponse liferayPortletResponse, MBMessage message) { + LiferayPortletResponse liferayPortletResponse, MBMessage message, + TrashHelper trashHelper) { _liferayPortletRequest = liferayPortletRequest; _liferayPortletResponse = liferayPortletResponse; _message = message; + _trashHelper = trashHelper; + + _themeDisplay = (ThemeDisplay)httpServletRequest.getAttribute( + WebKeys.THEME_DISPLAY); } public Map getMBPortletComponentContext() throws Exception { @@ -55,6 +66,9 @@ public Map getMBPortletComponentContext() throws Exception { ).put( "rootNodeId", _liferayPortletResponse.getNamespace() + "mbEditPageContainer" + ).put( + "trashEnabled", + () -> _trashHelper.isTrashEnabled(_themeDisplay.getScopeGroupId()) ).build(); if (_message != null) { @@ -90,5 +104,7 @@ public Map getMBPortletComponentContext() throws Exception { private final LiferayPortletRequest _liferayPortletRequest; private final LiferayPortletResponse _liferayPortletResponse; private MBMessage _message; + private final ThemeDisplay _themeDisplay; + private final TrashHelper _trashHelper; } \ No newline at end of file diff --git a/modules/apps/message-boards/message-boards-web/src/main/resources/META-INF/resources/js/message_boards/MBPortlet.js b/modules/apps/message-boards/message-boards-web/src/main/resources/META-INF/resources/js/message_boards/MBPortlet.js index 8955009b4c8519..99883ae55c9c66 100644 --- a/modules/apps/message-boards/message-boards-web/src/main/resources/META-INF/resources/js/message_boards/MBPortlet.js +++ b/modules/apps/message-boards/message-boards-web/src/main/resources/META-INF/resources/js/message_boards/MBPortlet.js @@ -31,6 +31,7 @@ class MBPortlet { confirmDiscardImages: CONFIRM_DISCARD_IMAGES, }, viewTrashAttachmentsURL, + trashEnabled, }) { this._namespace = namespace; this._constants = constants; @@ -39,6 +40,7 @@ class MBPortlet { this._replyToMessageId = replyToMessageId; this._strings = strings; this._viewTrashAttachmentsURL = viewTrashAttachmentsURL; + this._trashEnabled = trashEnabled; this.rootNode = document.getElementById(rootNodeId); @@ -104,7 +106,7 @@ class MBPortlet { .get('contentBox') .delegate( 'click', - this._removeAttachment.bind(this), + this._confirmRemoveAttachment.bind(this), '.delete-attachment' ); }); @@ -154,39 +156,53 @@ class MBPortlet { } /** - * Sends a request to remove the selected attachment. + * Show a confimation modal if trash is enabled * * @param {Event} event The click event that triggered the remove action */ - _removeAttachment(event) { + _confirmRemoveAttachment(event) { event.preventDefault(); - openConfirmModal({ - message: Liferay.Language.get( - 'are-you-sure-you-want-to-delete-this' - ), - onConfirm: (isConfirmed) => { - if (!isConfirmed) { - return; - } + if (this._trashEnabled) { + this._removeAttachment(event); + } + else { + openConfirmModal({ + message: Liferay.Language.get( + 'are-you-sure-you-want-to-delete-this' + ), + onConfirm: (isConfirmed) => { + if (!isConfirmed) { + return; + } - const link = event.currentTarget; - const deleteURL = link.getAttribute('href'); + this._removeAttachment(event); + }, + }); + } + } - fetch(deleteURL).then(() => { - Liferay.componentReady(this.searchContainerId).then( - (searchContainer) => { - searchContainer.deleteRow( - link.ancestor('tr'), - link.getAttribute('data-rowid') - ); - searchContainer.updateDataStore(); - } + /** + * Sends a request to remove the selected attachment. + * + * @param {Event} event The click event that triggered the remove action + */ + _removeAttachment(event) { + const link = event.currentTarget; + const deleteURL = link.getAttribute('href'); + + fetch(deleteURL).then(() => { + Liferay.componentReady(this.searchContainerId).then( + (searchContainer) => { + searchContainer.deleteRow( + link.ancestor('tr'), + link.getAttribute('data-rowid') ); + searchContainer.updateDataStore(); + } + ); - this._updateRemovedAttachments(); - }); - }, + this._updateRemovedAttachments(); }); } diff --git a/modules/apps/message-boards/message-boards-web/src/main/resources/META-INF/resources/message_boards/edit_message.jsp b/modules/apps/message-boards/message-boards-web/src/main/resources/META-INF/resources/message_boards/edit_message.jsp index 279023f0a358c1..820d12fc0cb799 100644 --- a/modules/apps/message-boards/message-boards-web/src/main/resources/META-INF/resources/message_boards/edit_message.jsp +++ b/modules/apps/message-boards/message-boards-web/src/main/resources/META-INF/resources/message_boards/edit_message.jsp @@ -457,7 +457,7 @@ if (portletTitleBasedNavigation) { <% -MBEditMessageDisplayContext mbEditMessageDisplayContext = new MBEditMessageDisplayContext(liferayPortletRequest, liferayPortletResponse, message); +MBEditMessageDisplayContext mbEditMessageDisplayContext = new MBEditMessageDisplayContext(request, liferayPortletRequest, liferayPortletResponse, message, trashHelper); %>