BlueGit

timfa filemanager

Files

filemanager / public / drag-move.js

drag-move.js

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