-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
179 lines (145 loc) · 5.7 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
167
168
169
170
171
172
173
174
175
176
177
178
179
const taskInput = document.querySelector('#task-input');
const addTaskBtn = document.querySelector('#addTaskBtn');
const taskContainer = document.querySelector('.task-container');
const tasks = document.getElementsByClassName('task');
const removeAllBtn = document.querySelector('#removeAllBtn');
// Button class
class Button {
#isDone = false;
#id;
#icon;
#doneIcon;
constructor(id, icon, doneIcon, isDone) {
this.#id = id;
this.#icon = icon;
this.#doneIcon = doneIcon;
this.#isDone = isDone
}
get id() {
return this.#id;
}
get icon() {
return this.#isDone ? this.#doneIcon : this.#icon;
}
}
function addTask(content, isDone = false, isRestored = false) {
if (content === "") return; // exit if user input is blank
const li = document.createElement('li');
isDone ? li.classList.add('task', 'done') : li.classList.add('task');
const p = document.createElement('p');
p.textContent = content;
taskInput.value = ''; // clear the input
const markDoneBtnObj = new Button('markDoneBtn', `<i class="fa-regular fa-circle"></i>`, `<i class="fa-solid fa-circle-check"></i>`, isDone);
const editBtnObj = new Button('editBtn', `<i class="fa-solid fa-pen-to-square"></i>`);
const removeBtnObj = new Button('removeBtn', `<i class="fa-solid fa-trash-can"></i>`);
let markDoneBtn, editBtn, removeBtn;
// create button elements and assign id for each
[markDoneBtn, editBtn, removeBtn] = [markDoneBtnObj, editBtnObj, removeBtnObj]
.map(obj => {
const button = document.createElement('button');
button.setAttribute('id', obj.id);
button.innerHTML = obj.icon;
return button;
});
[markDoneBtn, p, editBtn, removeBtn].forEach(elem => li.appendChild(elem));
if (isRestored) {
li.style.animation = "slideUp 1s"; // add restore animation
taskContainer.appendChild(li);
} else {
taskContainer.prepend(li);
saveTasks();
}
}
function displayEditPage(content, task) {
const container = document.createElement('div');
container.classList.add('edit-container');
const input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('id', 'task-edit');
input.setAttribute('placeholder', 'Edit your task');
input.value = content;
input.addEventListener('keydown', (e) => {
if (e.key === "Enter") editTask(input.value, task);
})
const editBtnObj = new Button('doneBtn', `<i class="fa-solid fa-check"></i>`);
const editBtn = document.createElement('button');
editBtn.setAttribute('id', editBtnObj.id);
editBtn.innerHTML = editBtnObj.icon;
editBtn.addEventListener('click', () => editTask(input.value, task));
[input, editBtn].forEach(elem => container.appendChild(elem));
document.body.append(container);
}
function editTask(newContent, task) {
if (newContent === "") return;
const taskContent = task.firstChild.nextElementSibling;
const editContainer = document.querySelector('.edit-container');
taskContent.textContent = newContent;
editContainer.classList.add('hide');
saveTasks();
setTimeout(() => editContainer.remove(), 300);
}
function manageTask(e) {
const target = e.target;
const parent = target.parentElement;
// Removing the task
if (target.matches('button#removeBtn')) {
parent.classList.add('removed');
setTimeout(() => {
parent.remove();
}, 1100);
// Marking the task as done
} else if (target.matches('button#markDoneBtn')) {
if (parent.classList.contains('done')) {
target.innerHTML = `<i class="fa-regular fa-circle"></i>`;
parent.classList.remove('done');
} else {
target.innerHTML = `<i class="fa-solid fa-circle-check"></i>`;
parent.classList.add('done');
}
} else if (target.matches('button#editBtn')) {
displayEditPage(target.previousElementSibling.textContent, parent)
} else return; // don't save if the target
// matches none of the above conditions
saveTasks();
}
function saveTasks() {
const formattedTasks = [...tasks].reduce((result, task) => {
if (!task.classList.contains('removed')) {
result.push({
content: task.firstElementChild.nextElementSibling.textContent,
isDone: task.classList.contains('done')
});
}
return result;
}, []);
localStorage.setItem('data', JSON.stringify(formattedTasks));
// add remove all button accordingly
removeAllBtn.classList.toggle('show', formattedTasks.length > 1);
}
(function restoreTasks() {
const data = localStorage.getItem('data');
if (!data) return; // exit if no tasks were found
const tasks = JSON.parse(data);
// add tasks with nice animation + delay
tasks.forEach((task, index) => {
setTimeout(() => {
addTask(task.content, task.isDone, true);
}, 100 * index)
});
// add remove all button accordingly
setTimeout(() => {
removeAllBtn.classList.toggle('show', tasks.length > 1);
}, tasks.length * 100);
})();
function removeAllTasks() {
taskContainer.innerHTML = "";
localStorage.removeItem('data'); // empty task data
removeAllBtn.classList.remove('show'); // hide the button itself
}
addTaskBtn.addEventListener('click', () => addTask(taskInput.value));
taskInput.addEventListener('keydown', (e) => {
if (e.keyCode === 13) addTask(taskInput.value);
});
// manage means marking as done + removing
taskContainer.addEventListener('click', manageTask);
removeAllBtn.addEventListener('click', removeAllTasks);