-
Notifications
You must be signed in to change notification settings - Fork 14
/
drag-n-drop.js
49 lines (39 loc) · 1.3 KB
/
drag-n-drop.js
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
const dragStart = target => {
target.classList.add('dragging');
};
const dragEnd = target => {
target.classList.remove('dragging');
};
const dragEnter = event => {
event.currentTarget.classList.add('drop');
};
const dragLeave = event => {
event.currentTarget.classList.remove('drop');
};
const drag = event => {
event.dataTransfer.setData('text/html', event.currentTarget.outerHTML);
event.dataTransfer.setData('text/plain', event.currentTarget.dataset.id);
};
const drop = event => {
document.querySelectorAll('.column').forEach(column => column.classList.remove('drop'));
document.querySelector(`[data-id="${event.dataTransfer.getData('text/plain')}"]`).remove();
event.preventDefault();
event.currentTarget.innerHTML = event.currentTarget.innerHTML + event.dataTransfer.getData('text/html');
};
const allowDrop = event => {
event.preventDefault();
};
document.querySelectorAll('.column').forEach(column => {
column.addEventListener('dragenter', dragEnter);
column.addEventListener('dragleave', dragLeave);
});
document.addEventListener('dragstart', e => {
if (e.target.className.includes('card')) {
dragStart(e.target);
}
});
document.addEventListener('dragend', e => {
if (e.target.className.includes('card')) {
dragEnd(e.target);
}
});