-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
67 lines (53 loc) · 2.12 KB
/
main.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
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("new-task");
const input = document.getElementById("task-input");
const todoLane = document.getElementById("initial");
const handleDragStart = (e) => e.target.classList.add("drag");
const handleDragEnd = (e) => e.target.classList.remove("drag");
const insertAboveTask = (zone, mouseY) => {
const els = zone.querySelectorAll(".task:not(.drag)");
let closestTask = null;
let closestOffset = Number.NEGATIVE_INFINITY;
els.forEach((task) => {
const { top } = task.getBoundingClientRect();
const offset = mouseY - top;
if (offset < 0 && offset > closestOffset) {
closestOffset = offset;
closestTask = task;
}
});
return closestTask;
};
const handleDragOver = (e) => {
e.preventDefault();
const zone = e.currentTarget;
const bottomTask = insertAboveTask(zone, e.clientY);
const draggingTask = document.querySelector(".drag");
if (bottomTask) {
zone.insertBefore(draggingTask, bottomTask);
} else {
zone.appendChild(draggingTask);
}
};
form.addEventListener("submit", (e) => {
e.preventDefault();
const value = input.value.trim();
if (!value) return;
const newTask = document.createElement("p");
newTask.classList.add("task");
newTask.setAttribute("draggable", "true");
newTask.innerText = value;
newTask.addEventListener("dragstart", handleDragStart);
newTask.addEventListener("dragend", handleDragEnd);
todoLane.appendChild(newTask);
input.value = "";
});
document.querySelectorAll(".task-stake").forEach((zone) => {
zone.addEventListener("dragover", handleDragOver);
zone.addEventListener("drop", handleDragOver);
});
document.querySelectorAll(".task").forEach((task) => {
task.addEventListener("dragstart", handleDragStart);
task.addEventListener("dragend", handleDragEnd);
});
});