-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
166 lines (152 loc) · 4.5 KB
/
app.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
const addBtn = document.querySelector(".addBtn");
const todoListul = document.querySelector(".tasks ul");
const inputValue = document.getElementById("input");
//
const createLi = document.querySelectorAll(".createLi");
let todos = [];
/* ==================== date and time =================== */
const dateTime = document.querySelector(".date-time");
let year = new Date().getFullYear();
let month = new Date().getMonth() + 1;
let day = new Date().getDate();
let date = `${day}/${month}/${year}`;
let spanDate = document.createElement("span");
dateTime.appendChild(spanDate);
spanDate.textContent = date;
// let hour = new Date().getHours();
// let minute = new Date().getMinutes();
// let time = `${hour} : ${minute}`;
// let spanTime = document.createElement("span");
// dateTime.appendChild(spanTime);
// spanTime.textContent = time;
/* ==================== date and time =================== */
const addItem = (e) => {
if (!inputValue.value.trim()) {
swal();
} else if (
todos.some(
(todo) => todo.text === inputValue.value.trim().toLowerCase()
)
) {
Swal.fire({
icon: "warning",
title: "Duplicate Task",
text: "This task already exists!",
});
} else {
let createLi = document.createElement("li");
createLi.className = "createLi";
todoListul.appendChild(createLi);
createLi.innerHTML = `
<div class="checkbox-wrapper-39">
<label>
<input type="checkbox" class ="checkbx" />
<span class="checkbox"></span>
</label>
</div>
<span class="todoText">${inputValue.value}</span> <i id="remove" style="display:none" class="fa-solid fa-trash-can"></i> `;
if ((e.target = "fa-plus")) {
todos.push({
text: inputValue.value,
cheq: false,
line: false,
trash: false,
});
localStorage.setItem("todos", JSON.stringify(todos));
inputValue.value = "";
}
}
};
addBtn.addEventListener("click", addItem);
//
todoListul.addEventListener("change", (e) => {
if (e.target.classList.contains("checkbx")) {
const liElement = e.target.closest("li");
const liText = liElement.querySelector(".todoText");
const removeBtn = liElement.querySelector(".fa-trash-can");
const index = Array.from(todoListul.children).indexOf(liElement);
todos[index].line = e.target.checked;
todos[index].trash = e.target.checked;
if (e.target.checked) {
liText.style.textDecoration = todos[index].line
? "line-through"
: "none";
liText.style.color = "gray";
removeBtn.style.display = todos[index].trash ? "flex" : "none";
removeBtn.style.cursor = "pointer";
todos[index].cheq = true;
} else {
liText.style.color = "rgb(247, 226, 223)";
liText.style.textDecoration = todos[index].line
? "none"
: "line-through";
liText.style.textDecoration = "none";
removeBtn.style.display = todos[index].trash ? "flex" : "none";
todos[index].cheq = false;
}
}
localStorage.setItem("todos", JSON.stringify(todos));
});
todoListul.addEventListener("click", (e) => {
const removeLi = e.target.closest("li");
if (e.target.id === "remove") {
const index = Array.from(todoListul.children).indexOf(removeLi);
todos.splice(index, 1);
removeLi.remove();
}
localStorage.setItem("todos", JSON.stringify(todos));
});
document.querySelector("body").addEventListener("keyup", (e) => {
inputValue.focus()
if (e.key === "Enter") {
addItem(e);
}
});
const swal = () => {
Swal.fire({
position: "center",
icon: "warning",
title: "The input field cannot be empty!",
showConfirmButton: false,
timer: 1100
});
};
// const del = () => {
// let li = document.querySelector(".createLi");
// if (li && !inputValue.value) {
// todoListul.lastElementChild.remove();
// } else {
// Swal.fire({
// position: "center",
// icon: "question",
// title: "There is no task to delete!",
// showConfirmButton: false,
// timer: 500,
// });
// }
// };
function showList() {
const savedTodos = localStorage.getItem("todos", todos);
if (savedTodos) {
todos = JSON.parse(savedTodos);
todos.forEach((todoItem) => {
const li = document.createElement("li");
li.className = "createLi";
li.innerHTML = `
<div class="checkbox-wrapper-39">
<label>
<input type="checkbox" class ="checkbx" ${todoItem.cheq ? "checked" : ""} />
<span class="checkbox" "></span>
</label>
</div>
<span class="todoText" style="text-decoration:${
todoItem.line ? "line-through" : "none"
} ">
${todoItem.text}</span> <i id="remove" style="display: ${
todoItem.trash ? "flex" : "none"
}" class="fa-solid fa-trash-can"></i>`;
todoListul.appendChild(li);
});
}
}
showList();