BlueGit

timfa filemanager

Files

filemanager / public / drag-move.js

drag-move.js

drag-move.js 6.7 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 originRect: null,
29 offsetX: 0,
30 offsetY: 0,
31 currentTarget: null,
32};
33
34const DRAG_THRESHOLD = 6;
35
36function _dragStart (downEvent, el, item)
37{
38 let started = false;
39 const startX = downEvent.clientX;
40 const startY = downEvent.clientY;
41
42 function onMove (e)
43 {
44 if (!started)
45 {
46 if (Math.hypot(e.clientX - startX, e.clientY - startY) < DRAG_THRESHOLD) return;
47 started = true;
48 _beginDrag(downEvent, el, item);
49 }
50 if (_drag.active) _moveDrag(e);
51 }
52
53 function onUp ()
54 {
55 document.removeEventListener('pointermove', onMove);
56 document.removeEventListener('pointerup', onUp);
57 if (started && _drag.active) _endDrag();
58 }
59
60 document.addEventListener('pointermove', onMove);
61 document.addEventListener('pointerup', onUp);
62}
63
64function _beginDrag (e, el, item)
65{
66 if (!canWrite()) return;
67 if (STATE.previewExpandState) return;
68
69 const rect = el.getBoundingClientRect();
70
71 const placeholder = document.createElement('div');
72 placeholder.style.cssText = 'visibility:hidden;pointer-events:none;flex-shrink:0';
73 placeholder.style.width = rect.width + 'px';
74 placeholder.style.height = rect.height + 'px';
75
76 el.parentNode.insertBefore(placeholder, el);
77 el.remove();
78
79 document.body.appendChild(el);
80 el.style.position = 'fixed';
81 el.style.left = '0';
82 el.style.top = '0';
83 el.style.width = rect.width + 'px';
84 el.style.height = rect.height + 'px';
85 el.style.margin = '0';
86 el.style.zIndex = '9999';
87 el.style.transition = 'none';
88 el.style.transform = `translate(${rect.left}px, ${rect.top}px)`;
89 el.style.pointerEvents = 'none';
90 el.classList.add('dragging');
91
92 _drag.active = true;
93 _drag.sourceEl = el;
94 _drag.sourceItem = item;
95 _drag.placeholder = placeholder;
96 _drag.originRect = rect;
97 _drag.offsetX = e.clientX - rect.left;
98 _drag.offsetY = e.clientY - rect.top;
99 _drag.currentTarget = null;
100
101 document.body.style.userSelect = 'none';
102}
103
104function _moveDrag (e)
105{
106 const x = e.clientX - _drag.offsetX;
107 const y = e.clientY - _drag.offsetY;
108 _drag.sourceEl.style.transform = `translate(${x}px, ${y}px)`;
109
110 const under = document.elementFromPoint(e.clientX, e.clientY);
111 const targetEl = under && under.closest('.file-item[data-isdir]');
112 const isValidTarget = targetEl && targetEl !== _drag.placeholder;
113
114 if (_drag.currentTarget && _drag.currentTarget !== targetEl)
115 {
116 _drag.currentTarget.classList.remove('drop-target');
117 }
118
119 if (isValidTarget)
120 {
121 targetEl.classList.add('drop-target');
122 _drag.currentTarget = targetEl;
123 } else
124 {
125 _drag.currentTarget = null;
126 }
127}
128
129function _endDrag ()
130{
131 const target = _drag.currentTarget;
132 if (target) target.classList.remove('drop-target');
133 document.body.style.userSelect = '';
134
135 if (!target)
136 {
137 _snapBack();
138 return;
139 }
140
141 const el = _drag.sourceEl;
142 const match = (el.style.transform || '').match(/translate\(([^,]+),\s*([^)]+)\)/);
143 const curX = match ? parseFloat(match[1]) : _drag.originRect.left;
144 const curY = match ? parseFloat(match[2]) : _drag.originRect.top;
145
146 const folderRect = target.getBoundingClientRect();
147 const fallX = folderRect.left + folderRect.width / 2 - _drag.originRect.width / 2;
148 const fallY = folderRect.top;
149
150 el.style.setProperty('--dx', curX + 'px');
151 el.style.setProperty('--dy', curY + 'px');
152 el.style.setProperty('--fall-x', fallX + 'px');
153 el.style.setProperty('--fall-y', fallY + 'px');
154 el.classList.add('dropping');
155 target.classList.add('folder-bounce');
156
157 const sourceItem = _drag.sourceItem;
158
159 el.addEventListener('animationend', async () =>
160 {
161 target.classList.remove('folder-bounce');
162 const targetPath = target.dataset.path;
163 if (!targetPath) { _cleanupDrag(true); return; }
164
165 try
166 {
167 await api('POST', '/files/move', { from: sourceItem.path, toDir: targetPath });
168 }
169 catch (err)
170 {
171 _cleanupDrag(true);
172 toast('Move failed: ' + err.message, 'err');
173 return;
174 }
175
176 _cleanupDrag(false);
177
178 if (STATE.selectedItem && STATE.selectedItem.path === sourceItem.path)
179 {
180 closeSidebar();
181 }
182
183 navigateTo(STATE.currentDir, STATE.currentRoot);
184 }, { once: true });
185}
186
187function _snapBack ()
188{
189 const el = _drag.sourceEl;
190 const rect = _drag.originRect;
191 const match = (el.style.transform || '').match(/translate\(([^,]+),\s*([^)]+)\)/);
192 const startX = match ? parseFloat(match[1]) : rect.left;
193 const startY = match ? parseFloat(match[2]) : rect.top;
194
195 const duration = 200;
196 const start = performance.now();
197
198 function step (now)
199 {
200 const t = Math.min((now - start) / duration, 1);
201 const ease = 1 - Math.pow(1 - t, 3);
202 const x = startX + (rect.left - startX) * ease;
203 const y = startY + (rect.top - startY) * ease;
204 el.style.transform = `translate(${x}px, ${y}px)`;
205
206 if (t < 1)
207 {
208 requestAnimationFrame(step);
209 } else
210 {
211 _cleanupDrag(true);
212 }
213 }
214
215 requestAnimationFrame(step);
216}
217
218function _cleanupDrag (reinsert)
219{
220 const el = _drag.sourceEl;
221 const placeholder = _drag.placeholder;
222
223 if (_drag.currentTarget) _drag.currentTarget.classList.remove('drop-target');
224
225 if (el)
226 {
227 el.classList.remove('dragging', 'dropping');
228 el.style.position = '';
229 el.style.left = '';
230 el.style.top = '';
231 el.style.width = '';
232 el.style.height = '';
233 el.style.margin = '';
234 el.style.zIndex = '';
235 el.style.transition = '';
236 el.style.transform = '';
237 el.style.pointerEvents = '';
238 el.style.removeProperty('--dx');
239 el.style.removeProperty('--dy');
240 el.style.removeProperty('--fall-x');
241 el.style.removeProperty('--fall-y');
242
243 if (reinsert && placeholder && placeholder.parentNode)
244 {
245 placeholder.parentNode.insertBefore(el, placeholder);
246 } else if (el.parentNode)
247 {
248 el.remove();
249 }
250 }
251
252 if (placeholder && placeholder.parentNode) placeholder.remove();
253
254 _drag.active = false;
255 _drag.sourceEl = null;
256 _drag.sourceItem = null;
257 _drag.placeholder = null;
258 _drag.originRect = null;
259 _drag.currentTarget = null;
260}