-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragdrop.mjs
55 lines (49 loc) · 1.64 KB
/
dragdrop.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export {allowDrop, dragStart, dragOver, olDrop};
function allowDrop(ev) {
ev.preventDefault();
}
let _dragStartEl;
function dragStart(ev) {
ev.target.classList.add('dragging');
if(ev.target.parentNode.id != 'Songs' && ev.altKey) {
ev.target.remove();
} else {
ev.dataTransfer.effectAllowed = ev.target.parentNode.id === 'Songs' || ev.shiftKey ? 'copy' : 'move';
_dragStartEl = ev.target;
}
}
function dragOver(e) {
if(e.target.parentNode.id === 'Songs') {
e.target.classList.remove('dragging');
return;
}
if(_dragStartEl.parentNode != e.target.parentNode) return;
e.preventDefault();
e.target.classList.remove('dragging');
if (isBefore(_dragStartEl, e.target)) {
e.target.parentNode.insertBefore(_dragStartEl, e.target);
} else {
e.target.parentNode.insertBefore(_dragStartEl, e.target.nextSibling);
}
}
function olDrop(ev) {
_dragStartEl.classList.remove('dragging');
(ev.target.nodeName === 'LI' ? ev.target.parentNode : ev.target).setAttribute('changed', 'yes');
document.querySelector('#savePlBtn').hidden = false;
if(ev.target === _dragStartEl) return;
const target = ev.target.nodeName === 'LI' ? ev.target.parentNode : ev.target;
if(ev.dataTransfer.effectAllowed === 'copy') {
_dragStartEl= _dragStartEl.cloneNode(true);
// cloning DOES NOT copy event listeners
_dragStartEl.addEventListener('dragover', dragOver);
_dragStartEl.addEventListener('dragstart', dragStart);
}
target.appendChild(_dragStartEl);
}
function isBefore(el1, el2) {
if (el2.parentNode === el1.parentNode)
for (var cur = el1.previousSibling; cur && cur.nodeType !== 9; cur = cur.previousSibling)
if (cur === el2)
return true;
return false;
}