|
4 | 4 | (function () { |
5 | 5 | "use strict"; |
6 | 6 |
|
7 | | - // Store references to avoid re-querying the DOM |
8 | | - let sortableInstance = null; |
9 | | - const upClickHandlers = new Map(); |
10 | | - const downClickHandlers = new Map(); |
11 | | - let isInitialized = false; |
12 | | - |
13 | 7 | // Initialize when DOM is loaded |
14 | 8 | if (document.readyState === "loading") { |
15 | 9 | document.addEventListener("DOMContentLoaded", initRowReordering); |
|
55 | 49 | if (element) { |
56 | 50 | moveElementInDOM(element, delta); |
57 | 51 | sendReorderRequest(element, delta); |
| 52 | + updateRowPositions(); // Recalculate plus button positions |
58 | 53 | } |
59 | 54 |
|
60 | 55 | setTimeout(() => (button.disabled = false), 500); |
61 | 56 | } |
62 | 57 |
|
63 | 58 | function initDragAndDropReordering() { |
64 | 59 | if (typeof Sortable === "undefined") { |
65 | | - console.log("SortableJS not loaded. Drag-and-drop reordering disabled."); |
66 | 60 | return; |
67 | 61 | } |
68 | 62 |
|
69 | 63 | // Find the container of the rows. We assume all draggable items share the same parent. |
70 | 64 | const firstDraggableElement = document.querySelector( |
71 | | - '[data-move-target="true"]' |
| 65 | + '.dynamic-row-wrapper' |
72 | 66 | ); |
73 | 67 | if (!firstDraggableElement?.parentElement) { |
74 | 68 | return; |
|
85 | 79 | ghostClass: "sortable-ghost", // Class for the drop placeholder |
86 | 80 | chosenClass: "sortable-chosen", // Class for the chosen item |
87 | 81 | dragClass: "sortable-drag", // Class for the dragging item |
| 82 | + draggable: ".dynamic-row-wrapper", // Only allow dragging the wrappers |
88 | 83 |
|
89 | 84 | // Element is dropped |
90 | 85 | onEnd: (evt) => { |
91 | 86 | const { oldIndex, newIndex, item } = evt; |
92 | 87 | if (oldIndex !== newIndex) { |
93 | 88 | const delta = newIndex - oldIndex; |
94 | 89 | sendReorderRequest(item, delta); |
| 90 | + updateRowPositions(); // Recalculate plus button positions |
95 | 91 | } |
96 | 92 | }, |
97 | 93 | }); |
98 | 94 | } |
99 | 95 |
|
| 96 | + /** |
| 97 | + * Recalculates all data-position attributes for add-row-plus-buttons |
| 98 | + * based on their current order in the DOM. |
| 99 | + */ |
| 100 | + function updateRowPositions() { |
| 101 | + const plusButtons = document.querySelectorAll(".add-row-plus-btn"); |
| 102 | + plusButtons.forEach((btn, index) => { |
| 103 | + btn.setAttribute("data-position", index); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
100 | 107 | function moveElementInDOM(element, delta) { |
101 | 108 | const parent = element.parentNode; |
102 | 109 | if (!parent) return; |
|
0 commit comments