-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
224 lines (183 loc) · 5.79 KB
/
script.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const form = document.querySelector("form");
const newTaskInput = document.querySelector("#New-Task");
const delToDo = document.querySelector("#toDo");
const delinProgress = document.querySelector("#inProgress");
const delDone = document.querySelector("#done");
// Load items
loadItems();
// Load Event Listeners
eventListeners();
// Event Listeners
function eventListeners() {
// add a new task listener
form.addEventListener("submit", addTask);
// delete an item listener
delToDo.addEventListener("click", deleteItem);
delinProgress.addEventListener("click", deleteItem);
delDone.addEventListener("click", deleteItem);
document.addEventListener('dragstart', (e) => {
e.target.classList.add('dragging');
const textValue = e.target.textContent;
console.log(textValue);
const categories = e.target.parentElement.id;
console.log(categories);
deleteItemFromLS(textValue, categories);
})
document.addEventListener('dragend', (e) => {
e.target.classList.remove('dragging');
const textValue1 = e.target.textContent;
console.log(textValue1);
const categories2 = e.target.parentElement.id;
console.log(categories2);
setItemToLS(categories2, textValue1);
})
}
// Add a new task
function addTask(e) {
if (newTaskInput.value === "") {
alert("Please add a task");
} else {
const categories = optionSelect.options[optionSelect.selectedIndex].value;
createItem(newTaskInput.value, categories);
setItemToLS(categories, newTaskInput.value);
newTaskInput.value = "";
e.preventDefault();
}
}
// Load items
function loadItems() {
toDo = getItemsFromLStoDo();
inProgress = getItemsFromLSinProgress();
done = getItemsFromLSdone();
toDo.forEach((item) => {
createItem(item, "toDo");
});
inProgress.forEach((item) => {
createItem(item, "inProgress");
});
done.forEach((item) => {
createItem(item, "done");
});
}
// get items from Local Storage
function getItemsFromLStoDo() {
if (localStorage.getItem("toDo") === null) {
toDo = [];
} else {
toDo = JSON.parse(localStorage.getItem("toDo"));
}
return toDo;
}
function getItemsFromLSinProgress() {
if (localStorage.getItem("inProgress") === null) {
inProgress = [];
} else {
inProgress = JSON.parse(localStorage.getItem("inProgress"));
}
return inProgress;
}
function getItemsFromLSdone() {
if (localStorage.getItem("done") === null) {
done = [];
} else {
done = JSON.parse(localStorage.getItem("done"));
}
return done;
}
// set item to Local Storage
function setItemToLS(categ, text) {
if (categ === "toDo") {
toDo = getItemsFromLStoDo();
toDo.push(text);
localStorage.setItem("toDo", JSON.stringify(toDo));
} else if (categ === "inProgress") {
inProgress = getItemsFromLSinProgress();
inProgress.push(text);
localStorage.setItem("inProgress", JSON.stringify(inProgress));
} else if (categ === "done") {
done = getItemsFromLSdone();
done.push(text);
localStorage.setItem("done", JSON.stringify(done));
}
}
// delete item from LS
function deleteItemFromLS(text, cater) {
if (cater === "toDo") {
toDo = getItemsFromLStoDo();
toDo.forEach(function (item, index) {
if (item === text) {
toDo.splice(index, 1);
}
});
localStorage.setItem("toDo", JSON.stringify(toDo));
} else if (cater === "inProgress") {
inProgress = getItemsFromLSinProgress();
inProgress.forEach(function (item, index) {
if (item === text) {
inProgress.splice(index, 1);
}
});
localStorage.setItem("inProgress", JSON.stringify(inProgress));
} else if (cater === "done") {
done = getItemsFromLSdone();
done.forEach(function (item, index) {
if (item === text) {
done.splice(index, 1);
}
});
localStorage.setItem("done", JSON.stringify(done));
}
}
function createItem(text, catesgorie) {
// create li
const li = document.createElement("li");
li.className =
"draggable list-group-item list-group-item-action d-flex justify-content-between";
li.setAttribute("draggable", "true");
li.appendChild(document.createTextNode(text));
reo = '<i class="fa-solid fa-xmark"></i>';
li.innerHTML += reo;
document.getElementById(catesgorie).appendChild(li);
}
function deleteItem(e) {
if (e.target.className === "fa-solid fa-xmark") {
let cater = e.target.parentNode;
cater = cater.parentElement.id;
e.target.parentElement.remove();
// delete item from LS
let textCont = e.target.parentElement.textContent;
deleteItemFromLS(textCont, cater);
}
e.preventDefault();
}
let draggables = document.querySelectorAll(".draggable");
let containers = document.querySelectorAll(".dropzone");
containers.forEach((container) => {
container.addEventListener("dragover", (e) => {
e.preventDefault();
const afterElement = getDragAfterElement(container, e.clientY);
const draggable = document.querySelector(".dragging");
if (afterElement == null) {
container.appendChild(draggable);
} else {
container.insertBefore(draggable, afterElement);
}
});
});
function getDragAfterElement(container, y) {
const draggableElements = [
...container.querySelectorAll(".draggable: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;
}