BlueGit

timfa filemanager

Files

filemanager / public / drag-move.js

drag-move.js

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