diff --git a/ui/src/layout-editor/main.js b/ui/src/layout-editor/main.js index 2e7875ad45..b91800d96e 100644 --- a/ui/src/layout-editor/main.js +++ b/ui/src/layout-editor/main.js @@ -1530,11 +1530,11 @@ lD.deleteObject = function( * @param {boolean=} showConfirmationModal */ lD.deleteMultipleObjects = function(showConfirmationModal = true) { - const deleteElementsOrGroupElements = function( + const deleteElements = function( itemsArray, - type = 'elements', ) { let auxWidget = null; + let auxWidgetId = null; itemsArray.each((idx, item) => { const itemId = $(item).attr('id'); const widgetId = $(item).data('widgetId'); @@ -1552,8 +1552,11 @@ lD.deleteMultipleObjects = function(showConfirmationModal = true) { ) ); - // Get parent widget if doesn't exist or we need to save + // Get parent widget if doesn't exist + // if widget is a new one + // or we need to save if ( + widgetFullId != auxWidgetId || !auxWidget || save ) { @@ -1562,26 +1565,18 @@ lD.deleteMultipleObjects = function(showConfirmationModal = true) { widgetFullId, 'canvas', ); - } - // Delete element from widget - if (type === 'elements') { - auxWidget.removeElement( - itemId, - { - save: save, - reload: false, - }, - ); - } else { - auxWidget.removeElementGroup( - itemId, - { - save: save, - reload: false, - }, - ); + auxWidgetId = widgetFullId; } + + // Delete elements from widget + auxWidget.removeElement( + itemId, + { + save: save, + reload: false, + }, + ); }); }; @@ -1616,17 +1611,13 @@ lD.deleteMultipleObjects = function(showConfirmationModal = true) { return; } - // First delete elements if they exist - const $elementsToBeDeleted = + // Get elements to be deleted + let $elementsToBeDeleted = lD.viewer.DOMObject.find('.selected.designer-element').sort((a, b) => { return Number($(b).data('widgetId')) - Number($(a).data('widgetId')); }); - if ($elementsToBeDeleted.length > 0) { - deleteElementsOrGroupElements($elementsToBeDeleted); - } - - // Then delete element groups + // Get groups to be deleted const $elementGroupsToBeDeleted = lD.viewer.DOMObject.find('.selected.designer-element-group') .sort((a, b) => { @@ -1636,13 +1627,22 @@ lD.deleteMultipleObjects = function(showConfirmationModal = true) { ); }); + // Grab elements from the groups if ($elementGroupsToBeDeleted.length > 0) { - deleteElementsOrGroupElements( - $elementGroupsToBeDeleted, - 'elementGroups', + // Add elements from the groups to all elements to be deleted + $elementsToBeDeleted = $elementsToBeDeleted.add( + $elementGroupsToBeDeleted.find('.designer-element').sort((a, b) => { + return Number($(b).data('widgetId')) - Number($(a).data('widgetId')); + }), ); } + // Delete elements (groups will be automatically deleted + // if all elements from a group are also deleted) + if ($elementsToBeDeleted.length > 0) { + deleteElements($elementsToBeDeleted); + } + // Finally, delete regions one by one const $regionsToBeDeleted = lD.viewer.DOMObject.find('.selected.designer-region');