-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.js
71 lines (62 loc) · 2.02 KB
/
backup.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// script.js
function slist(target) {
// Set CSS class for the container
target.classList.add("slist");
// Get all list items
let items = target.getElementsByTagName("li");
let current = null;
// Make items draggable and sortable
for (let i of items) {
// Attach draggable property
i.draggable = true;
// Drag start: Yellow highlight drop zones
i.ondragstart = (e) => {
current = i;
for (let it of items) {
if (it != current) {
it.classList.add("hint");
}
}
};
// Drag enter: Red highlight drop zone
i.ondragenter = (e) => {
if (i != current) {
i.classList.add("active");
}
};
// Drag leave: Remove red highlight
i.ondragleave = () => i.classList.remove("active");
// Drag end: Remove all highlights
i.ondragend = () => {
for (let it of items) {
it.classList.remove("hint");
it.classList.remove("active");
}
};
// Drag over: Prevent the default "drop" behavior
i.ondragover = (e) => e.preventDefault();
// On drop: Reorder the items
i.ondrop = (e) => {
e.preventDefault();
if (i != current) {
let currentPos = 0;
let droppedPos = 0;
for (let it = 0; it < items.length; it++) {
if (current == items[it]) {
currentPos = it;
}
if (i == items[it]) {
droppedPos = it;
}
}
if (currentPos < droppedPos) {
i.parentNode.insertBefore(current, i.nextSibling);
} else {
i.parentNode.insertBefore(current, i);
}
}
};
}
}
// Initialize the sortable list
slist(document.getElementById("sortlist"));