| 1 | /* ========================================================= |
| 2 | DRAG-MOVE — icon drag-into-folder behaviour |
| 3 | Depends on: api(), canWrite(), navigateTo(), STATE, |
| 4 | closeSidebar() — all defined in app.js |
| 5 | ========================================================= */ |
| 6 | 'use strict'; |
| 7 | |
| 8 | /** |
| 9 | * Call this once per .file-item element created by makeFileItem(). |
| 10 | * @param {HTMLElement} el The .file-item element |
| 11 | * @param {object} item The file/folder metadata object |
| 12 | */ |
| 13 | function makeDraggable (el, item) |
| 14 | { |
| 15 | el.addEventListener('pointerdown', (e) => |
| 16 | { |
| 17 | // Only primary mouse button or first touch point |
| 18 | if (e.button !== undefined && e.button !== 0) return; |
| 19 | // Don't start a drag immediately — wait for a small movement threshold |
| 20 | _dragStart(e, el, item); |
| 21 | }); |
| 22 | } |
| 23 | |
| 24 | // ─── Internal drag state ─────────────────────────────────────────────────── |
| 25 | |
| 26 | const _drag = { |
| 27 | active: false, |
| 28 | sourceEl: null, |
| 29 | sourceItem: null, |
| 30 | ghost: null, |
| 31 | originRect: null, |
| 32 | offsetX: 0, |
| 33 | offsetY: 0, |
| 34 | currentTarget: null, // .file-item folder currently hovered |
| 35 | }; |
| 36 | |
| 37 | const DRAG_THRESHOLD = 6; // px movement before drag is considered started |
| 38 | |
| 39 | function _dragStart (downEvent, el, item) |
| 40 | { |
| 41 | let started = false; |
| 42 | let startX = downEvent.clientX; |
| 43 | let startY = downEvent.clientY; |
| 44 | |
| 45 | function onMove (e) |
| 46 | { |
| 47 | const dx = e.clientX - startX; |
| 48 | const dy = e.clientY - startY; |
| 49 | if (!started) |
| 50 | { |
| 51 | if (Math.hypot(dx, dy) < DRAG_THRESHOLD) return; |
| 52 | started = true; |
| 53 | _beginDrag(downEvent, el, item); |
| 54 | } |
| 55 | if (_drag.active) _moveDrag(e); |
| 56 | } |
| 57 | |
| 58 | function onUp (e) |
| 59 | { |
| 60 | document.removeEventListener('pointermove', onMove); |
| 61 | document.removeEventListener('pointerup', onUp); |
| 62 | if (started && _drag.active) _endDrag(e); |
| 63 | } |
| 64 | |
| 65 | document.addEventListener('pointermove', onMove); |
| 66 | document.addEventListener('pointerup', onUp); |
| 67 | } |
| 68 | |
| 69 | function _beginDrag (e, el, item) |
| 70 | { |
| 71 | if (!canWrite()) return; |
| 72 | |
| 73 | const rect = el.getBoundingClientRect(); |
| 74 | _drag.active = true; |
| 75 | _drag.sourceEl = el; |
| 76 | _drag.sourceItem = item; |
| 77 | _drag.originRect = rect; |
| 78 | _drag.offsetX = e.clientX - rect.left; |
| 79 | _drag.offsetY = e.clientY - rect.top; |
| 80 | _drag.currentTarget = null; |
| 81 | |
| 82 | // Create ghost |
| 83 | const ghost = el.cloneNode(true); |
| 84 | ghost.className = 'file-item drag-ghost'; |
| 85 | ghost.style.width = rect.width + 'px'; |
| 86 | ghost.style.height = rect.height + 'px'; |
| 87 | ghost.style.left = '0px'; |
| 88 | ghost.style.top = '0px'; |
| 89 | _setGhostPos(ghost, e.clientX - _drag.offsetX, e.clientY - _drag.offsetY); |
| 90 | document.body.appendChild(ghost); |
| 91 | _drag.ghost = ghost; |
| 92 | |
| 93 | // Dim the source |
| 94 | el.classList.add('drag-source'); |
| 95 | |
| 96 | // Prevent text selection during drag |
| 97 | document.body.style.userSelect = 'none'; |
| 98 | } |
| 99 | |
| 100 | function _setGhostPos (ghost, x, y) |
| 101 | { |
| 102 | ghost.style.transform = `translate(${x}px, ${y}px)`; |
| 103 | } |
| 104 | |
| 105 | function _moveDrag (e) |
| 106 | { |
| 107 | const x = e.clientX - _drag.offsetX; |
| 108 | const y = e.clientY - _drag.offsetY; |
| 109 | _setGhostPos(_drag.ghost, x, y); |
| 110 | |
| 111 | // Hit-test: find .file-item[data-isdir] under cursor (excluding source) |
| 112 | _drag.ghost.style.display = 'none'; |
| 113 | const under = document.elementFromPoint(e.clientX, e.clientY); |
| 114 | _drag.ghost.style.display = ''; |
| 115 | |
| 116 | const targetEl = under && under.closest('.file-item[data-isdir]'); |
| 117 | const isValidTarget = targetEl && targetEl !== _drag.sourceEl; |
| 118 | |
| 119 | if (_drag.currentTarget && _drag.currentTarget !== targetEl) |
| 120 | { |
| 121 | _drag.currentTarget.classList.remove('drop-target'); |
| 122 | } |
| 123 | if (isValidTarget) |
| 124 | { |
| 125 | targetEl.classList.add('drop-target'); |
| 126 | _drag.currentTarget = targetEl; |
| 127 | } else |
| 128 | { |
| 129 | _drag.currentTarget = null; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | function _endDrag (e) |
| 134 | { |
| 135 | const target = _drag.currentTarget; |
| 136 | |
| 137 | // Clean up hover state |
| 138 | if (target) target.classList.remove('drop-target'); |
| 139 | document.body.style.userSelect = ''; |
| 140 | |
| 141 | if (!target) |
| 142 | { |
| 143 | // Snap back to origin |
| 144 | _snapBack(); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | // Compute animation variables: where ghost currently is vs folder top-centre |
| 149 | const ghostRect = _drag.ghost.getBoundingClientRect(); |
| 150 | const folderRect = target.getBoundingClientRect(); |
| 151 | |
| 152 | // Fall destination: top-centre of folder icon, adjusted for ghost size |
| 153 | const fallX = folderRect.left + folderRect.width / 2 - ghostRect.width / 2; |
| 154 | const fallY = folderRect.top; |
| 155 | |
| 156 | const curX = ghostRect.left; |
| 157 | const curY = ghostRect.top; |
| 158 | |
| 159 | // Set CSS custom properties for the animation keyframes |
| 160 | const ghost = _drag.ghost; |
| 161 | ghost.style.setProperty('--dx', `${curX}px`); |
| 162 | ghost.style.setProperty('--dy', `${curY}px`); |
| 163 | ghost.style.setProperty('--fall-y', `${fallY}px`); |
| 164 | // Reset transform to (0,0) so the animation's translate values are absolute |
| 165 | ghost.style.transform = 'translate(0px, 0px)'; |
| 166 | |
| 167 | ghost.classList.add('dropping'); |
| 168 | target.classList.add('folder-bounce'); |
| 169 | |
| 170 | const sourceEl = _drag.sourceEl; |
| 171 | const sourceItem = _drag.sourceItem; |
| 172 | |
| 173 | ghost.addEventListener('animationend', async () => |
| 174 | { |
| 175 | _cleanupDrag(); |
| 176 | target.classList.remove('folder-bounce'); |
| 177 | |
| 178 | // Determine target folder path |
| 179 | const targetPath = target.dataset.path; |
| 180 | if (!targetPath) return; |
| 181 | |
| 182 | try |
| 183 | { |
| 184 | await api('POST', '/files/move', { from: sourceItem.path, toDir: targetPath }); |
| 185 | } |
| 186 | catch (err) |
| 187 | { |
| 188 | toast('Move failed: ' + err.message, 'err'); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | // If the moved item was selected, close its sidebar |
| 193 | if (STATE.selectedItem && STATE.selectedItem.path === sourceItem.path) |
| 194 | { |
| 195 | closeSidebar(); |
| 196 | } |
| 197 | |
| 198 | navigateTo(STATE.currentDir, STATE.currentRoot); |
| 199 | }, { once: true }); |
| 200 | } |
| 201 | |
| 202 | function _snapBack () |
| 203 | { |
| 204 | const ghost = _drag.ghost; |
| 205 | if (!ghost) { _cleanupDrag(); return; } |
| 206 | |
| 207 | const ghostRect = ghost.getBoundingClientRect(); |
| 208 | ghost.style.setProperty('--dx', `${ghostRect.left}px`); |
| 209 | ghost.style.setProperty('--dy', `${ghostRect.top}px`); |
| 210 | ghost.style.transform = 'translate(0px, 0px)'; |
| 211 | ghost.classList.add('snapping'); |
| 212 | |
| 213 | ghost.addEventListener('animationend', () => |
| 214 | { |
| 215 | _cleanupDrag(); |
| 216 | }, { once: true }); |
| 217 | } |
| 218 | |
| 219 | function _cleanupDrag () |
| 220 | { |
| 221 | if (_drag.sourceEl) _drag.sourceEl.classList.remove('drag-source'); |
| 222 | if (_drag.ghost && _drag.ghost.parentNode) _drag.ghost.parentNode.removeChild(_drag.ghost); |
| 223 | if (_drag.currentTarget) _drag.currentTarget.classList.remove('drop-target'); |
| 224 | |
| 225 | _drag.active = false; |
| 226 | _drag.sourceEl = null; |
| 227 | _drag.sourceItem = null; |
| 228 | _drag.ghost = null; |
| 229 | _drag.originRect = null; |
| 230 | _drag.currentTarget = null; |
| 231 | } |