/* ========================================================= DRAG-MOVE — icon drag-into-folder behaviour Depends on: api(), canWrite(), navigateTo(), STATE, closeSidebar() — all defined in app.js ========================================================= */ 'use strict'; /** * Call this once per .file-item element created by makeFileItem(). * @param {HTMLElement} el The .file-item element * @param {object} item The file/folder metadata object */ function makeDraggable (el, item) { el.addEventListener('pointerdown', (e) => { // Only primary mouse button or first touch point if (e.button !== undefined && e.button !== 0) return; // Don't start a drag immediately — wait for a small movement threshold _dragStart(e, el, item); }); } // ─── Internal drag state ─────────────────────────────────────────────────── const _drag = { active: false, sourceEl: null, sourceItem: null, ghost: null, originRect: null, offsetX: 0, offsetY: 0, currentTarget: null, // .file-item folder currently hovered }; const DRAG_THRESHOLD = 6; // px movement before drag is considered started function _dragStart (downEvent, el, item) { let started = false; let startX = downEvent.clientX; let startY = downEvent.clientY; function onMove (e) { const dx = e.clientX - startX; const dy = e.clientY - startY; if (!started) { if (Math.hypot(dx, dy) < DRAG_THRESHOLD) return; started = true; _beginDrag(downEvent, el, item); } if (_drag.active) _moveDrag(e); } function onUp (e) { document.removeEventListener('pointermove', onMove); document.removeEventListener('pointerup', onUp); if (started && _drag.active) _endDrag(e); } document.addEventListener('pointermove', onMove); document.addEventListener('pointerup', onUp); } function _beginDrag (e, el, item) { if (!canWrite()) return; const rect = el.getBoundingClientRect(); _drag.active = true; _drag.sourceEl = el; _drag.sourceItem = item; _drag.originRect = rect; _drag.offsetX = e.clientX - rect.left; _drag.offsetY = e.clientY - rect.top; _drag.currentTarget = null; // Create ghost const ghost = el.cloneNode(true); ghost.className = 'file-item drag-ghost'; ghost.style.width = rect.width + 'px'; ghost.style.height = rect.height + 'px'; ghost.style.left = '0px'; ghost.style.top = '0px'; _setGhostPos(ghost, e.clientX - _drag.offsetX, e.clientY - _drag.offsetY); document.body.appendChild(ghost); _drag.ghost = ghost; // Dim the source el.classList.add('drag-source'); // Prevent text selection during drag document.body.style.userSelect = 'none'; } function _setGhostPos (ghost, x, y) { ghost.style.transform = `translate(${x}px, ${y}px)`; } function _moveDrag (e) { const x = e.clientX - _drag.offsetX; const y = e.clientY - _drag.offsetY; _setGhostPos(_drag.ghost, x, y); // Hit-test: find .file-item[data-isdir] under cursor (excluding source) _drag.ghost.style.display = 'none'; const under = document.elementFromPoint(e.clientX, e.clientY); _drag.ghost.style.display = ''; const targetEl = under && under.closest('.file-item[data-isdir]'); const isValidTarget = targetEl && targetEl !== _drag.sourceEl; if (_drag.currentTarget && _drag.currentTarget !== targetEl) { _drag.currentTarget.classList.remove('drop-target'); } if (isValidTarget) { targetEl.classList.add('drop-target'); _drag.currentTarget = targetEl; } else { _drag.currentTarget = null; } } function _endDrag (e) { const target = _drag.currentTarget; // Clean up hover state if (target) target.classList.remove('drop-target'); document.body.style.userSelect = ''; if (!target) { // Snap back to origin _snapBack(); return; } // Compute animation variables: where ghost currently is vs folder top-centre const ghostRect = _drag.ghost.getBoundingClientRect(); const folderRect = target.getBoundingClientRect(); // Fall destination: top-centre of folder icon, adjusted for ghost size const fallX = folderRect.left + folderRect.width / 2 - ghostRect.width / 2; const fallY = folderRect.top; const curX = ghostRect.left; const curY = ghostRect.top; // Set CSS custom properties for the animation keyframes const ghost = _drag.ghost; ghost.style.setProperty('--dx', `${curX}px`); ghost.style.setProperty('--dy', `${curY}px`); ghost.style.setProperty('--fall-y', `${fallY}px`); // Reset transform to (0,0) so the animation's translate values are absolute ghost.style.transform = 'translate(0px, 0px)'; ghost.classList.add('dropping'); target.classList.add('folder-bounce'); const sourceEl = _drag.sourceEl; const sourceItem = _drag.sourceItem; ghost.addEventListener('animationend', async () => { _cleanupDrag(); target.classList.remove('folder-bounce'); // Determine target folder path const targetPath = target.dataset.path; if (!targetPath) return; try { await api('POST', '/files/move', { from: sourceItem.path, toDir: targetPath }); } catch (err) { toast('Move failed: ' + err.message, 'err'); return; } // If the moved item was selected, close its sidebar if (STATE.selectedItem && STATE.selectedItem.path === sourceItem.path) { closeSidebar(); } navigateTo(STATE.currentDir, STATE.currentRoot); }, { once: true }); } function _snapBack () { const ghost = _drag.ghost; if (!ghost) { _cleanupDrag(); return; } const ghostRect = ghost.getBoundingClientRect(); ghost.style.setProperty('--dx', `${ghostRect.left}px`); ghost.style.setProperty('--dy', `${ghostRect.top}px`); ghost.style.transform = 'translate(0px, 0px)'; ghost.classList.add('snapping'); ghost.addEventListener('animationend', () => { _cleanupDrag(); }, { once: true }); } function _cleanupDrag () { if (_drag.sourceEl) _drag.sourceEl.classList.remove('drag-source'); if (_drag.ghost && _drag.ghost.parentNode) _drag.ghost.parentNode.removeChild(_drag.ghost); if (_drag.currentTarget) _drag.currentTarget.classList.remove('drop-target'); _drag.active = false; _drag.sourceEl = null; _drag.sourceItem = null; _drag.ghost = null; _drag.originRect = null; _drag.currentTarget = null; }