-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
204 lines (165 loc) · 5.13 KB
/
script.js
File metadata and controls
204 lines (165 loc) · 5.13 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
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
const addTaskBtn = document.getElementById("add-task");
const taskInput = document.getElementById("task-input");
const taskCategoryEle = document.querySelectorAll(".task-category");
const todoListEle = document.getElementById("todo-list");
const itemsLeftELe = document.getElementById("items-left");
const clearCompletedBtn = document.getElementById("clear-completed");
const emptyStateEle = document.querySelector(".empty-state");
const emptyIcon = document.getElementById("empty-icon");
const emptyText = document.getElementById("empty-text");
const dateEle = document.getElementById("date");
let todos = [];
let currentCategory = "all";
addTaskBtn.addEventListener("click", () => {
addTodo(taskInput.value);
});
taskInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
addTodo(taskInput.value);
}
});
clearCompletedBtn.addEventListener("click", () => {
todos = todos.filter((todo) => !todo.completed);
saveTodo();
renderTodo();
});
function addTodo(text) {
if (text.trim() === "") return;
const todo = {
id: Date.now(),
text: text,
completed: false,
};
todos.push(todo);
saveTodo();
renderTodo();
taskInput.value = "";
}
function saveTodo() {
localStorage.setItem("todos", JSON.stringify(todos));
updateItemCount();
checkEmptyState();
}
function updateItemCount() {
const uncompletedTodos = todos.filter((todo) => !todo.completed);
itemsLeftELe.textContent = `${uncompletedTodos.length} item${uncompletedTodos.length !== 1 ? "s" : ""} left`;
}
function checkEmptyState() {
const categorizedTodo = categorizeTodo(currentCategory);
if (categorizedTodo.length === 0) {
emptyStateEle.classList.remove("hidden");
if (currentCategory === "active") {
emptyIcon.className = "fas fa-glass-cheers";
emptyText.textContent = "All caught up! No active tasks.";
} else if (currentCategory === "completed") {
emptyIcon.className = "fas fa-book";
emptyText.textContent = "No completed tasks! Let's get to work!";
} else {
emptyIcon.className = "fas fa-clipboard-list";
emptyText.textContent = "No tasks here yet! Add one above.";
}
} else {
emptyStateEle.classList.add("hidden");
}
}
function categorizeTodo(category) {
switch (category) {
case "active":
return todos.filter((todo) => !todo.completed);
case "completed":
return todos.filter((todo) => todo.completed);
default:
return todos;
}
}
function renderTodo() {
todoListEle.innerHTML = "";
const categorizedTodo = categorizeTodo(currentCategory);
categorizedTodo.forEach((todo) => {
const todoItem = document.createElement("li");
todoItem.classList.add("todo-item");
if (todo.completed) {
todoItem.classList.add("completed");
}
const checkboxContainer = document.createElement("label");
checkboxContainer.classList.add("checkbox-container");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.classList.add("todo-checkbox");
checkbox.checked = todo.completed;
checkbox.addEventListener("change", () => toggleTodo(todo.id));
const checkmark = document.createElement("span");
checkmark.classList.add("checkmark");
checkboxContainer.appendChild(checkbox);
checkboxContainer.appendChild(checkmark);
const todoText = document.createElement("span");
todoText.classList.add("todo-item-text");
todoText.textContent = todo.text;
const deleteBtn = document.createElement("button");
deleteBtn.classList.add("delete-btn");
deleteBtn.innerHTML = "<i class='fas fa-times'></i>";
deleteBtn.addEventListener("click", () => deleteTodo(todo.id));
todoItem.appendChild(checkboxContainer);
todoItem.appendChild(todoText);
todoItem.appendChild(deleteBtn);
todoListEle.appendChild(todoItem);
});
checkEmptyState();
}
function toggleTodo(id) {
todos = todos.map((todo) => {
if (todo.id === id) {
return { ...todo, completed: !todo.completed };
}
return todo;
});
saveTodo();
renderTodo();
}
function deleteTodo(id) {
todos = todos.filter((todo) => todo.id !== id);
saveTodo();
renderTodo();
}
function clearCompleted() {
todos = todos.filter((todo) => !todo.completed);
saveTodo();
renderTodo();
}
function loadTodo() {
const storedTodo = localStorage.getItem("todos");
if (storedTodo) {
todos = JSON.parse(storedTodo);
}
renderTodo();
}
taskCategoryEle.forEach((category) => {
category.addEventListener("click", () => {
setActiveCategory(category.getAttribute("data-filter"));
});
});
function setActiveCategory(category) {
currentCategory = category;
taskCategoryEle.forEach((thing) => {
if (thing.getAttribute("data-filter") === category) {
thing.classList.add("active-category");
} else {
thing.classList.remove("active-category");
}
});
renderTodo();
}
function setDate() {
const formatOfDate = {
weekday: "long",
month: "short",
day: "numeric",
};
const today = new Date();
dateEle.textContent = today.toLocaleDateString("en-US", formatOfDate);
}
window.addEventListener("DOMContentLoaded", () => {
loadTodo();
updateItemCount();
setDate();
});