BlueGit

timfa filemanager

Files

filemanager / public / drag-move.js

drag-move.js

pip
timfa Tim Falken · pushed by timfa · 6 months ago
aefbcaaa History
drag-move.js 9.5 KiB Download Raw
1/* =========================================================
2 DRAG-MOVE — icon drag-into-folder behaviour
3 Uses the REAL .file-item element (no ghost clone).
4 Depends on: moveWithConflictResolution(), canWrite(), navigateTo(), STATE,
5 closeSidebar(), toast() — all defined in app.js
6 ========================================================= */
7'use strict';
8
9/**
10 * Call this once per .file-item element created by makeFileItem().
11 * @param {HTMLElement} el The .file-item element
12 * @param {object} item The file/folder metadata object
13 */
14function makeDraggable (el, item)
15{
16 el.addEventListener('pointerdown', (e) =>
17 {
18 if (e.button !== undefined && e.button !== 0) return;
19 _dragStart(e, el, item);
20 });
21}
22
23const _drag = {
24 active: false,
25 sourceEl: null,
26 sourceItem: null,
27 placeholder: null,
28 parentTarget: null,
29 originRect: null,
30 offsetX: 0,
31 offsetY: 0,
32 currentTarget: null,
33};
34
35const DRAG_THRESHOLD = 6;
36const TOUCH_DRAG_HOLD_MS = 140;
37
38function _dragStart (downEvent, el, item)
39{
40 let started = false;
41 let canStart = downEvent.pointerType !== 'touch';
42 let holdTimer = null;
43 const startX = downEvent.clientX;
44 const startY = downEvent.clientY;
45
46 if (!canStart)
47 {
48 holdTimer = setTimeout(() =>
49 {
50 canStart = true;
51 holdTimer = null;
52 }, TOUCH_DRAG_HOLD_MS);
53 }
54
55 function cleanupListeners ()
56 {
57 if (holdTimer)
58 {
59 clearTimeout(holdTimer);
60 holdTimer = null;
61 }
62 document.removeEventListener('pointermove', onMove);
63 document.removeEventListener('pointerup', onUp);
64 document.removeEventListener('pointercancel', onCancel);
65 }
66
67 function onMove (e)
68 {
69 const moved = Math.hypot(e.clientX - startX, e.clientY - startY);
70
71 if (!started)
72 {
73 if (!canStart)
74 {
75 if (moved > DRAG_THRESHOLD)
76 {
77 cleanupListeners();
78 }
79 return;
80 }
81
82 if (moved < DRAG_THRESHOLD) return;
83 started = true;
84 _beginDrag(downEvent, el, item);
85 if (downEvent.pointerId != null && typeof el.setPointerCapture === 'function')
86 {
87 try { el.setPointerCapture(downEvent.pointerId); } catch { /* ignore */ }
88 }
89 }
90 if (_drag.active)
91 {
92 e.preventDefault();
93 _moveDrag(e);
94 }
95 }
96
97 function onUp ()
98 {
99 cleanupListeners();
100 if (started && _drag.active) _endDrag();
101 }
102
103 function onCancel ()
104 {
105 cleanupListeners();
106 if (started && _drag.active) _cleanupDrag(true);
107 }
108
109 document.addEventListener('pointermove', onMove);
110 document.addEventListener('pointerup', onUp);
111 document.addEventListener('pointercancel', onCancel);
112}
113
114function _beginDrag (e, el, item)
115{
116 if (!canWrite()) return;
117 if (STATE.previewExpandState) return;
118
119 const rect = el.getBoundingClientRect();
120
121 const placeholder = document.createElement('div');
122 placeholder.style.cssText = 'visibility:hidden;pointer-events:none;flex-shrink:0';
123 placeholder.style.width = rect.width + 'px';
124 placeholder.style.height = rect.height + 'px';
125
126 el.parentNode.insertBefore(placeholder, el);
127 el.remove();
128
129 document.body.appendChild(el);
130 el.style.position = 'fixed';
131 el.style.left = '0';
132 el.style.top = '0';
133 el.style.width = rect.width + 'px';
134 el.style.height = rect.height + 'px';
135 el.style.margin = '0';
136 el.style.zIndex = '9999';
137 el.style.transition = 'none';
138 el.style.transform = `translate(${rect.left}px, ${rect.top}px)`;
139 el.style.pointerEvents = 'none';
140 el.classList.add('dragging');
141
142 _drag.active = true;
143 _drag.sourceEl = el;
144 _drag.sourceItem = item;
145 _drag.placeholder = placeholder;
146 _drag.originRect = rect;
147 _drag.offsetX = e.clientX - rect.left;
148 _drag.offsetY = e.clientY - rect.top;
149 _drag.currentTarget = null;
150
151 const parentPath = _parentDirPathForCurrentDir();
152 if (parentPath)
153 {
154 const parentTarget = _ensureParentDropTarget();
155 parentTarget.dataset.path = parentPath;
156 parentTarget.classList.remove('hidden');
157 _drag.parentTarget = parentTarget;
158 }
159
160 document.body.style.userSelect = 'none';
161}
162
163function _parentDirPathForCurrentDir ()
164{
165 if (!STATE.currentDir || !STATE.currentRoot) return null;
166 const root = STATE.currentRoot.path;
167 const dir = STATE.currentDir;
168 if (samePath(dir, root)) return null;
169
170 const relative = String(dir).replace(String(root), '').replace(/^[\\/]/, '');
171 const parts = relative.split(/[\\/]/).filter(Boolean);
172 if (!parts.length) return null;
173 parts.pop();
174 if (!parts.length) return root;
175
176 let out = root;
177 for (const p of parts) out = joinServerPath(out, p);
178 return out;
179}
180
181function _ensureParentDropTarget ()
182{
183 let el = document.getElementById('drag-parent-target');
184 if (el) return el;
185
186 el = document.createElement('div');
187 el.id = 'drag-parent-target';
188 el.className = 'drag-parent-target hidden';
189 el.innerHTML = '<span class="folder">📁</span><span class="up">↑</span><span class="label">Parent folder</span>';
190 document.body.appendChild(el);
191 return el;
192}
193
194function _setTargetVisual (el, active)
195{
196 if (!el) return;
197 if (el.classList.contains('file-item'))
198 {
199 el.classList.toggle('drop-target', !!active);
200 return;
201 }
202 el.classList.toggle('active', !!active);
203}
204
205function _moveDrag (e)
206{
207 const x = e.clientX - _drag.offsetX;
208 const y = e.clientY - _drag.offsetY;
209 _drag.sourceEl.style.transform = `translate(${x}px, ${y}px)`;
210
211 const under = document.elementFromPoint(e.clientX, e.clientY);
212 const targetEl = under && under.closest('.file-item[data-isdir], .drag-parent-target, .crumb-drop-target');
213 const targetPath = targetEl ? targetEl.dataset.path : null;
214 const isValidTarget = !!(targetEl && targetPath && targetEl !== _drag.placeholder);
215
216 if (_drag.currentTarget && _drag.currentTarget !== targetEl)
217 {
218 _setTargetVisual(_drag.currentTarget, false);
219 }
220
221 if (isValidTarget)
222 {
223 _setTargetVisual(targetEl, true);
224 _drag.currentTarget = targetEl;
225 } else
226 {
227 _drag.currentTarget = null;
228 }
229}
230
231function _endDrag ()
232{
233 const target = _drag.currentTarget;
234 if (target) _setTargetVisual(target, false);
235 document.body.style.userSelect = '';
236
237 if (!target)
238 {
239 _snapBack();
240 return;
241 }
242
243 const el = _drag.sourceEl;
244 const match = (el.style.transform || '').match(/translate\(([^,]+),\s*([^)]+)\)/);
245 const curX = match ? parseFloat(match[1]) : _drag.originRect.left;
246 const curY = match ? parseFloat(match[2]) : _drag.originRect.top;
247
248 const targetRect = target.getBoundingClientRect();
249 const fallX = targetRect.left + targetRect.width / 2 - _drag.originRect.width / 2;
250 const fallY = targetRect.top;
251
252 el.style.setProperty('--dx', curX + 'px');
253 el.style.setProperty('--dy', curY + 'px');
254 el.style.setProperty('--fall-x', fallX + 'px');
255 el.style.setProperty('--fall-y', fallY + 'px');
256 el.classList.add('dropping');
257 if (target.classList.contains('file-item')) target.classList.add('folder-bounce');
258
259 const sourceItem = _drag.sourceItem;
260
261 el.addEventListener('animationend', async () =>
262 {
263 target.classList.remove('folder-bounce');
264 const targetPath = target.dataset.path;
265 if (!targetPath) { _cleanupDrag(true); return; }
266
267 try
268 {
269 const moved = await moveWithConflictResolution(sourceItem, targetPath);
270 if (!moved)
271 {
272 _cleanupDrag(true);
273 return;
274 }
275 }
276 catch (err)
277 {
278 _cleanupDrag(true);
279 toast('Move failed: ' + err.message, 'err');
280 return;
281 }
282
283 _cleanupDrag(false);
284
285 if (STATE.selectedItem && STATE.selectedItem.path === sourceItem.path)
286 {
287 closeSidebar();
288 }
289
290 navigateTo(STATE.currentDir, STATE.currentRoot);
291 }, { once: true });
292}
293
294function _snapBack ()
295{
296 const el = _drag.sourceEl;
297 const rect = _drag.originRect;
298 const match = (el.style.transform || '').match(/translate\(([^,]+),\s*([^)]+)\)/);
299 const startX = match ? parseFloat(match[1]) : rect.left;
300 const startY = match ? parseFloat(match[2]) : rect.top;
301
302 const duration = 200;
303 const start = performance.now();
304
305 function step (now)
306 {
307 const t = Math.min((now - start) / duration, 1);
308 const ease = 1 - Math.pow(1 - t, 3);
309 const x = startX + (rect.left - startX) * ease;
310 const y = startY + (rect.top - startY) * ease;
311 el.style.transform = `translate(${x}px, ${y}px)`;
312
313 if (t < 1)
314 {
315 requestAnimationFrame(step);
316 } else
317 {
318 _cleanupDrag(true);
319 }
320 }
321
322 requestAnimationFrame(step);
323}
324
325function _cleanupDrag (reinsert)
326{
327 const el = _drag.sourceEl;
328 const placeholder = _drag.placeholder;
329
330 if (_drag.currentTarget) _setTargetVisual(_drag.currentTarget, false);
331 if (_drag.parentTarget)
332 {
333 _drag.parentTarget.classList.remove('active');
334 _drag.parentTarget.classList.add('hidden');
335 }
336
337 if (el)
338 {
339 el.classList.remove('dragging', 'dropping');
340 el.style.position = '';
341 el.style.left = '';
342 el.style.top = '';
343 el.style.width = '';
344 el.style.height = '';
345 el.style.margin = '';
346 el.style.zIndex = '';
347 el.style.transition = '';
348 el.style.transform = '';
349 el.style.pointerEvents = '';
350 el.style.removeProperty('--dx');
351 el.style.removeProperty('--dy');
352 el.style.removeProperty('--fall-x');
353 el.style.removeProperty('--fall-y');
354
355 if (reinsert && placeholder && placeholder.parentNode)
356 {
357 placeholder.parentNode.insertBefore(el, placeholder);
358 } else if (el.parentNode)
359 {
360 el.remove();
361 }
362 }
363
364 if (placeholder && placeholder.parentNode) placeholder.remove();
365
366 _drag.active = false;
367 _drag.sourceEl = null;
368 _drag.sourceItem = null;
369 _drag.placeholder = null;
370 _drag.parentTarget = null;
371 _drag.originRect = null;
372 _drag.currentTarget = null;
373}