|
| 1 | +/** |
| 2 | + * Compose Manager – Stack sortable (drag-and-drop reordering) |
| 3 | + * |
| 4 | + * Depends on globals provided by the page: |
| 5 | + * caURL – AJAX endpoint for exec.php |
| 6 | + * composeClientDebug – debug/logging helper (common.js) |
| 7 | + * $.cookie / $.removeCookie – jquery.cookie plugin |
| 8 | + * $.fn.sortable – jQuery UI Sortable |
| 9 | + */ |
| 10 | + |
| 11 | +// ── Sort-mode state ──────────────────────────────────────────────── |
| 12 | + |
| 13 | +function isComposeSortModeEnabled() { |
| 14 | + return $.cookie('lockbutton') != null; |
| 15 | +} |
| 16 | + |
| 17 | +// ── Lock / Unlock button UI ──────────────────────────────────────── |
| 18 | + |
| 19 | +function updateComposeLockButtonUI() { |
| 20 | + var unlocked = isComposeSortModeEnabled(); |
| 21 | + var $button = $('div.nav-item.LockButton'); |
| 22 | + if (!$button.length) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + if (unlocked) { |
| 27 | + $button.find('a').prop('title', 'Lock sortable items'); |
| 28 | + $button.find('b').removeClass('icon-u-lock green-text').addClass('icon-u-lock-open red-text'); |
| 29 | + $button.find('span').text('Lock sortable items'); |
| 30 | + } else { |
| 31 | + $button.find('a').prop('title', 'Unlock sortable items'); |
| 32 | + $button.find('b').removeClass('icon-u-lock-open red-text').addClass('icon-u-lock green-text'); |
| 33 | + $button.find('span').text('Unlock sortable items'); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// ── Persist sort order ───────────────────────────────────────────── |
| 38 | + |
| 39 | +function saveComposeSortOrder() { |
| 40 | + var projects = $('#compose_list > tr.compose-sortable').map(function() { |
| 41 | + return $(this).data('project'); |
| 42 | + }).get(); |
| 43 | + |
| 44 | + return $.post(caURL, { |
| 45 | + action: 'saveStackOrder', |
| 46 | + projects: projects |
| 47 | + }).fail(function(xhr) { |
| 48 | + composeClientDebug('[saveComposeSortOrder] failed', { |
| 49 | + status: xhr.status, |
| 50 | + response: xhr.responseText |
| 51 | + }, 'daemon', 'error'); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +// ── Details-row helpers (detach during drag, reattach on drop) ───── |
| 56 | + |
| 57 | +function getComposeDetailsRowForItem($item) { |
| 58 | + if (!$item || !$item.length) { |
| 59 | + return $(); |
| 60 | + } |
| 61 | + |
| 62 | + var rowId = $item.attr('id') || ''; |
| 63 | + if (rowId.indexOf('stack-row-') !== 0) { |
| 64 | + return $(); |
| 65 | + } |
| 66 | + |
| 67 | + return $('#details-row-' + rowId.replace('stack-row-', '')); |
| 68 | +} |
| 69 | + |
| 70 | +function reattachComposeDetailsRow($item) { |
| 71 | + var $detailsRow = $item.data('compose-details-row'); |
| 72 | + if ($detailsRow && $detailsRow.length) { |
| 73 | + $detailsRow.insertAfter($item); |
| 74 | + $item.removeData('compose-details-row'); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// ── jQuery UI Sortable initialisation ────────────────────────────── |
| 79 | + |
| 80 | +function initComposeSortable() { |
| 81 | + var $tbody = $('#compose_list'); |
| 82 | + if (!$tbody.length) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if ($tbody.hasClass('ui-sortable')) { |
| 87 | + $tbody.sortable('destroy'); |
| 88 | + } |
| 89 | + |
| 90 | + if (!isComposeSortModeEnabled()) { |
| 91 | + $tbody.removeClass('compose-sort-enabled'); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + $tbody.addClass('compose-sort-enabled'); |
| 96 | + $tbody.sortable({ |
| 97 | + helper: 'clone', |
| 98 | + items: '> tr.compose-sortable', |
| 99 | + cursor: 'grab', |
| 100 | + axis: 'y', |
| 101 | + containment: 'parent', |
| 102 | + cancel: '[data-stackid], .compose-updatecolumn a, .compose-updatecolumn .exec, .auto_start, .switchButton, a, button, input', |
| 103 | + delay: 100, |
| 104 | + opacity: 0.5, |
| 105 | + zIndex: 9999, |
| 106 | + forcePlaceholderSize: true, |
| 107 | + start: function(event, ui) { |
| 108 | + var $detailsRow = getComposeDetailsRowForItem(ui.item); |
| 109 | + if ($detailsRow.length) { |
| 110 | + ui.item.data('compose-details-row', $detailsRow.detach()); |
| 111 | + } |
| 112 | + }, |
| 113 | + update: function() { |
| 114 | + saveComposeSortOrder(); |
| 115 | + }, |
| 116 | + stop: function(event, ui) { |
| 117 | + reattachComposeDetailsRow(ui.item); |
| 118 | + } |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +// ── Sync all sort-mode UI (icons, class, sortable instance) ──────── |
| 123 | + |
| 124 | +function syncComposeSortModeUI() { |
| 125 | + var unlocked = isComposeSortModeEnabled(); |
| 126 | + $('#compose_stacks tr.compose-sortable').each(function() { |
| 127 | + var $row = $(this); |
| 128 | + $row.find('.expand-icon').toggle(!unlocked); |
| 129 | + $row.find('.mover').toggle(unlocked); |
| 130 | + }); |
| 131 | + |
| 132 | + $('#compose_list').toggleClass('compose-sort-enabled', unlocked); |
| 133 | + updateComposeLockButtonUI(); |
| 134 | + initComposeSortable(); |
| 135 | +} |
| 136 | + |
| 137 | +// ── Global entry-point called by Unraid navigation lock button ───── |
| 138 | +// When embedded in the Docker tab, DockerContainers.page defines its own |
| 139 | +// LockButton(). We save the previous implementation (if any) and chain |
| 140 | +// to it so both Docker containers AND Compose stacks react to the button. |
| 141 | +// IMPORTANT: We use window.LockButton assignment (not a function declaration) |
| 142 | +// to avoid hoisting — a hoisted function declaration would overwrite the |
| 143 | +// global LockButton before we can capture it. |
| 144 | + |
| 145 | +var _composePrevLockButton = window.LockButton || null; |
| 146 | + |
| 147 | +window.LockButton = function LockButton() { |
| 148 | + if (_composePrevLockButton) { |
| 149 | + // Let the Docker (or other) handler run first — it toggles the |
| 150 | + // cookie and manages its own sortable / UI state. |
| 151 | + _composePrevLockButton(); |
| 152 | + } else { |
| 153 | + // Standalone page (header-menu mode) — we own the cookie. |
| 154 | + if (isComposeSortModeEnabled()) { |
| 155 | + $.removeCookie('lockbutton'); |
| 156 | + } else { |
| 157 | + $.cookie('lockbutton', 'lockbutton'); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + syncComposeSortModeUI(); |
| 162 | +}; |
0 commit comments