-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (90 loc) · 3.18 KB
/
script.js
File metadata and controls
100 lines (90 loc) · 3.18 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const form = document.querySelector('.FRM');
const input = document.querySelector('#todo');
const toDoList = document.querySelectorAll('.ONE')[0];
const notStartedList = document.querySelectorAll('.ONE')[1];
const inProgressList = document.querySelectorAll('.ONE')[2];
const completedList = document.querySelectorAll('.ONE')[3];
form.addEventListener('submit', function (e) {
e.preventDefault();
if (input.value.trim() === "") {
alert("Add The Todo Data.");
} else {
const p = document.createElement('p');
p.textContent = input.value;
p.style.backgroundColor = 'lightblue';
p.style.margin = '5px';
p.style.padding = '5px';
p.style.borderRadius = '20px';
p.draggable = true;
p.addEventListener('dragstart', dragStart);
p.addEventListener('dragend', dragEnd);
toDoList.appendChild(p);
input.value = '';
}
});
function dragStart(e) {
e.dataTransfer.setData('text/plain', e.target.outerHTML);
e.dataTransfer.effectAllowed = 'move';
setTimeout(() => {
e.target.classList.add('dragging');
}, 0);
}
function dragEnd(e) {
e.target.classList.remove('dragging');
}
function dragOver(e) {
e.preventDefault();
const afterElement = getDragAfterElement(e.target.closest('.ONE'), e.clientY);
const container = e.target.closest('.ONE');
const dragging = document.querySelector('.dragging');
if (afterElement == null) {
container.appendChild(dragging);
} else {
container.insertBefore(dragging, afterElement);
}
}
function dragEnter(e) {
e.preventDefault();
if (e.target.classList.contains('ONE')) {
e.target.style.border = '2px dashed #ccc';
}
}
function dragLeave(e) {
if (e.target.classList.contains('ONE')) {
e.target.style.border = '2px solid black';
}
}
function drop(e) {
e.preventDefault();
const dragging = document.querySelector('.dragging');
const dropTarget = e.target.closest('.ONE');
dropTarget.style.border = '2px solid black';
dragging.style.display = 'block';
if (dropTarget === notStartedList) {
dragging.style.backgroundColor = 'orange';
} else if (dropTarget === inProgressList) {
dragging.style.backgroundColor = 'yellow';
} else if (dropTarget === completedList) {
dragging.style.backgroundColor = 'lightgreen';
} else {
dragging.style.backgroundColor = 'lightblue';
}
}
function getDragAfterElement(container, y) {
const draggableElements = [...container.querySelectorAll('p:not(.dragging)')];
return draggableElements.reduce((closest, child) => {
const box = child.getBoundingClientRect();
const offset = y - box.top - box.height / 2;
if (offset < 0 && offset > closest.offset) {
return { offset: offset, element: child };
} else {
return closest;
}
}, { offset: Number.NEGATIVE_INFINITY }).element;
}
[toDoList, notStartedList, inProgressList, completedList].forEach(list => {
list.addEventListener('dragover', dragOver);
list.addEventListener('dragenter', dragEnter);
list.addEventListener('dragleave', dragLeave);
list.addEventListener('drop', drop);
});