|
1 | 1 | /** |
2 | 2 | * Handles reordering of dynamic page rows |
3 | 3 | */ |
4 | | - |
5 | 4 | (function () { |
6 | 5 | "use strict"; |
7 | 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 | + |
8 | 13 | // Initialize when DOM is loaded |
9 | 14 | if (document.readyState === "loading") { |
10 | 15 | document.addEventListener("DOMContentLoaded", initRowReordering); |
|
13 | 18 | } |
14 | 19 |
|
15 | 20 | function initRowReordering() { |
16 | | - // Only run on dynamic-view with edit permissions |
17 | 21 | if ( |
18 | 22 | !document.body.classList.contains("template-dynamic-view") || |
19 | 23 | !document.body.classList.contains("can_edit") |
20 | 24 | ) { |
21 | 25 | return; |
22 | 26 | } |
23 | 27 |
|
| 28 | + // Initialize drag-and-drop reordering |
| 29 | + initDragAndDropReordering(); |
| 30 | + |
24 | 31 | // Find all move up/down buttons |
25 | 32 | const moveUpButtons = document.querySelectorAll('a[data-action="move-up"]'); |
26 | 33 | const moveDownButtons = document.querySelectorAll( |
27 | 34 | 'a[data-action="move-down"]' |
28 | 35 | ); |
29 | 36 |
|
30 | | - // Add event listeners to move up buttons |
31 | 37 | moveUpButtons.forEach((button) => { |
32 | | - button.addEventListener( |
33 | | - "click", |
34 | | - function (e) { |
35 | | - e.preventDefault(); |
36 | | - e.stopPropagation(); // Detener la propagación del evento |
37 | | - |
38 | | - // Deshabilitar el botón temporalmente |
39 | | - if (this.disabled) return; |
40 | | - this.disabled = true; |
41 | | - |
42 | | - const element = this.closest('[data-move-target="true"]'); |
43 | | - |
44 | | - // Re-habilitar el botón después de un tiempo |
45 | | - setTimeout(() => { |
46 | | - this.disabled = false; |
47 | | - }, 2000); |
48 | | - |
49 | | - moveElement(element, -1); |
50 | | - }, |
51 | | - { once: true } |
52 | | - ); // Usar { once: true } para que el listener se ejecute solo una vez |
| 38 | + button.addEventListener("click", (e) => handleButtonClick(e, -1)); |
53 | 39 | }); |
54 | 40 |
|
55 | | - // Add event listeners to move down buttons |
56 | 41 | moveDownButtons.forEach((button) => { |
57 | | - button.addEventListener( |
58 | | - "click", |
59 | | - function (e) { |
60 | | - e.preventDefault(); |
61 | | - e.stopPropagation(); // Detener la propagación del evento |
62 | | - |
63 | | - // Deshabilitar el botón temporalmente |
64 | | - if (this.disabled) return; |
65 | | - this.disabled = true; |
66 | | - |
67 | | - const element = this.closest('[data-move-target="true"]'); |
68 | | - |
69 | | - // Re-habilitar el botón después de un tiempo |
70 | | - setTimeout(() => { |
71 | | - this.disabled = false; |
72 | | - }, 2000); |
73 | | - |
74 | | - moveElement(element, 1); |
75 | | - }, |
76 | | - { once: true } |
77 | | - ); // Usar { once: true } para que el listener se ejecute solo una vez |
| 42 | + button.addEventListener("click", (e) => handleButtonClick(e, 1)); |
78 | 43 | }); |
79 | 44 | } |
80 | 45 |
|
81 | | - function moveElement(element, delta) { |
| 46 | + function handleButtonClick(e, delta) { |
| 47 | + e.preventDefault(); |
| 48 | + e.stopPropagation(); |
| 49 | + |
| 50 | + const button = e.currentTarget; |
| 51 | + if (button.disabled) return; |
| 52 | + button.disabled = true; |
| 53 | + |
| 54 | + const element = button.closest('[data-move-target="true"]'); |
| 55 | + if (element) { |
| 56 | + moveElementInDOM(element, delta); |
| 57 | + sendReorderRequest(element, delta); |
| 58 | + } |
| 59 | + |
| 60 | + setTimeout(() => (button.disabled = false), 500); |
| 61 | + } |
| 62 | + |
| 63 | + function initDragAndDropReordering() { |
| 64 | + if (typeof Sortable === "undefined") { |
| 65 | + console.log("SortableJS not loaded. Drag-and-drop reordering disabled."); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // Find the container of the rows. We assume all draggable items share the same parent. |
| 70 | + const firstDraggableElement = document.querySelector( |
| 71 | + '[data-move-target="true"]' |
| 72 | + ); |
| 73 | + if (!firstDraggableElement?.parentElement) { |
| 74 | + return; |
| 75 | + } |
| 76 | + const container = firstDraggableElement.parentElement; |
| 77 | + |
| 78 | + // Add a class for styling the handle |
| 79 | + container.classList.add("sortable-container"); |
| 80 | + |
| 81 | + // Initialize SortableJS |
| 82 | + new Sortable(container, { |
| 83 | + animation: 150, // ms, animation speed moving items when sorting, `0` — without animation |
| 84 | + handle: ".drag-handle", // Restrict drag start to elements with this class |
| 85 | + ghostClass: "sortable-ghost", // Class for the drop placeholder |
| 86 | + chosenClass: "sortable-chosen", // Class for the chosen item |
| 87 | + dragClass: "sortable-drag", // Class for the dragging item |
| 88 | + |
| 89 | + // Element is dropped |
| 90 | + onEnd: (evt) => { |
| 91 | + const { oldIndex, newIndex, item } = evt; |
| 92 | + if (oldIndex !== newIndex) { |
| 93 | + const delta = newIndex - oldIndex; |
| 94 | + sendReorderRequest(item, delta); |
| 95 | + } |
| 96 | + }, |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + function moveElementInDOM(element, delta) { |
| 101 | + const parent = element.parentNode; |
| 102 | + if (!parent) return; |
| 103 | + |
| 104 | + if (delta > 0) { |
| 105 | + // Move down |
| 106 | + const nextTarget = element.nextElementSibling?.nextElementSibling || null; |
| 107 | + parent.insertBefore(element, nextTarget); |
| 108 | + } else { |
| 109 | + // Move up |
| 110 | + const target = element.previousElementSibling; |
| 111 | + if (target) { |
| 112 | + parent.insertBefore(element, target); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + function sendReorderRequest(element, delta) { |
82 | 118 | const elementId = element.dataset.elementid; |
83 | 119 | if (!elementId) { |
84 | | - const errorMsg = "No data-element-id attribute found on element"; |
85 | | - alert(errorMsg); |
| 120 | + console.error("No data-element-id attribute found on element"); |
86 | 121 | return; |
87 | 122 | } |
88 | 123 |
|
|
111 | 146 | throw error; |
112 | 147 | } |
113 | 148 | }) |
114 | | - .finally(() => { |
115 | | - sessionStorage.setItem( |
116 | | - "toast-message", |
117 | | - "Element reordered successfully." |
118 | | - ); |
119 | | - // Refresh the page after successful update |
120 | | - window.location.reload(); |
| 149 | + .catch((error) => { |
| 150 | + console.error("Error reordering element:", error); |
| 151 | + alert("Error reordering element. Please refresh the page."); |
121 | 152 | }); |
122 | 153 | } |
123 | 154 | })(); |
0 commit comments