|
| 1 | +/** |
| 2 | + * Handles reordering of dynamic page rows |
| 3 | + */ |
| 4 | + |
| 5 | +(function () { |
| 6 | + "use strict"; |
| 7 | + |
| 8 | + // Initialize when DOM is loaded |
| 9 | + if (document.readyState === "loading") { |
| 10 | + document.addEventListener("DOMContentLoaded", initRowReordering); |
| 11 | + } else { |
| 12 | + initRowReordering(); |
| 13 | + } |
| 14 | + |
| 15 | + function initRowReordering() { |
| 16 | + // Only run on dynamic-view with edit permissions |
| 17 | + if ( |
| 18 | + !document.body.classList.contains("template-dynamic-view") || |
| 19 | + !document.body.classList.contains("userrole-manager") |
| 20 | + ) { |
| 21 | + console.log( |
| 22 | + "Not in dynamic-view edit mode, skipping row reordering initialization" |
| 23 | + ); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + console.log("Initializing row reordering..."); |
| 28 | + // Find all move up/down buttons |
| 29 | + const moveUpButtons = document.querySelectorAll('a[data-action="move-up"]'); |
| 30 | + const moveDownButtons = document.querySelectorAll( |
| 31 | + 'a[data-action="move-down"]' |
| 32 | + ); |
| 33 | + console.log( |
| 34 | + `Found ${moveUpButtons.length} move-up buttons and ${moveDownButtons.length} move-down buttons` |
| 35 | + ); |
| 36 | + |
| 37 | + // Add event listeners to move up buttons |
| 38 | + moveUpButtons.forEach((button) => { |
| 39 | + button.addEventListener("click", function (e) { |
| 40 | + e.preventDefault(); |
| 41 | + console.log("Move up button clicked"); |
| 42 | + const row = this.closest(".dynamic-row"); |
| 43 | + console.log(`Moving row with ID: ${row.dataset.rowid} up`); |
| 44 | + moveRow(row, -1); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + // Add event listeners to move down buttons |
| 49 | + moveDownButtons.forEach((button) => { |
| 50 | + button.addEventListener("click", function (e) { |
| 51 | + e.preventDefault(); |
| 52 | + console.log("Move down button clicked"); |
| 53 | + const row = this.closest(".dynamic-row"); |
| 54 | + console.log(`Moving row with ID: ${row.dataset.rowid} down`); |
| 55 | + moveRow(row, 1); |
| 56 | + }); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + function moveRow(row, delta) { |
| 61 | + const rowId = row.dataset.rowid; |
| 62 | + if (!rowId) { |
| 63 | + const errorMsg = "No data-row-id attribute found on row"; |
| 64 | + console.error(errorMsg); |
| 65 | + alert(errorMsg); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + console.log(`Preparing to move row ${rowId} with delta ${delta}`); |
| 70 | + |
| 71 | + const baseUrl = row.dataset.rowurl || ""; |
| 72 | + console.log(`Sending request to: ${baseUrl}`); |
| 73 | + |
| 74 | + const requestBody = { |
| 75 | + ordering: { |
| 76 | + obj_id: rowId, |
| 77 | + delta: delta, |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + console.log("Request payload:", JSON.stringify(requestBody, null, 2)); |
| 82 | + |
| 83 | + fetch(baseUrl, { |
| 84 | + method: "PATCH", |
| 85 | + headers: { |
| 86 | + "Content-Type": "application/json", |
| 87 | + Accept: "application/json", |
| 88 | + "X-Requested-With": "XMLHttpRequest", |
| 89 | + }, |
| 90 | + body: JSON.stringify(requestBody), |
| 91 | + credentials: "same-origin", |
| 92 | + }).then((response) => { |
| 93 | + console.log(`Received response with status: ${response.status}`); |
| 94 | + console.log("Response headers:", response.headers); |
| 95 | + console.log("Response ok:", response.ok); |
| 96 | + if (!response.ok) { |
| 97 | + const error = new Error(`HTTP error! status: ${response.status}`); |
| 98 | + console.error("Response not OK:", error); |
| 99 | + throw error; |
| 100 | + } |
| 101 | + // Refresh the page after successful update |
| 102 | + window.location.reload(); |
| 103 | + }); |
| 104 | + } |
| 105 | +})(); |
0 commit comments