-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
363 lines (299 loc) · 12.7 KB
/
index.js
File metadata and controls
363 lines (299 loc) · 12.7 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
class Category {
constructor(catName = "New Category") {
this.catName = catName;
this.catId = Date.now();
this.tasks = [];
}
addTask(taskDesc) {
this.tasks.push(new Task(taskDesc));
}
} // CHECKED
class Task {
constructor(taskDesc) {
this.taskId = Date.now() + 1;
this.taskDesc = taskDesc;
this.complete = false;
}
} // CHECKED
function addCat() {
const catInput = document.getElementById("cat-input");
const value = catInput.value
if (value.trim() !== '') {
dm.addCategory(value);
catInput.value = '';
}
} // CHECKED
function addTask() {
const taskInput = document.getElementById("task-input");
const value = taskInput.value;
const catId = document.querySelectorAll(".sidebar .cat-name.active")[0].dataset.catId;
if (catId && value.trim() !== '') {
dm.addTask(catId, value);
taskInput.value = '';
}
} // CHECKED
class DataManager {
constructor() {
this.datas = this.loadFromLocalStorage();
} // CHECKED
loadFromLocalStorage() {
return JSON.parse(localStorage.getItem('tm.datas')) || [{ catName: "My Tasks", catId: Date.now(), tasks: [] }];
} // CHECKED
saveToLocalStorage() {
localStorage.setItem('tm.datas', JSON.stringify(this.datas));
} // CHECKED
addCategory(categoryName) {
this.datas.push(new Category(categoryName));
this.saveToLocalStorage();
this.renderCategory();
setFirstCatActive()
} // CHECKED
editCategory(catId, newCatName) {
const selectedCategory = this.datas.find(item => item.catId == catId);
if (selectedCategory) {
// console.log(selectedCategory.catName)
selectedCategory.catName = newCatName;
this.saveToLocalStorage();
this.renderCategory();
}
} // CHECKED
deleteCategory(catId) {
this.datas = this.datas.filter(category => category.catId !== catId);
this.saveToLocalStorage();
this.renderCategory();
setFirstCatActive()
} // CHECKED
addTask(catId, taskDesc) {
const selectedCategory = this.datas.find(item => item.catId == catId);
if (selectedCategory) {
selectedCategory.tasks.push(new Task(taskDesc));
this.saveToLocalStorage();
this.renderTasksByCatId(catId);
}
} // CHECKED
editTask(catId, taskId, newTaskDesc) {
const selectedCategory = this.datas.find(item => item.catId == catId);
if (selectedCategory) {
const selectedTask = selectedCategory.tasks.find(task => task.taskId == taskId);
console.log(selectedCategory, selectedTask)
if (selectedTask) {
selectedTask.taskDesc = newTaskDesc;
this.saveToLocalStorage();
this.renderTasksByCatId(catId);
}
}
} // CHECKED
deleteTask(catId, taskId) {
const selectedCategory = this.datas.find(item => item.catId == catId);
if (selectedCategory) {
selectedCategory.tasks = selectedCategory.tasks.filter(task => task.taskId !== taskId);
this.saveToLocalStorage();
this.renderTasksByCatId(catId);
}
} // CHECKED
toggleTaskComplete(catId, taskId) {
const selectedCategory = this.datas.find(item => item.catId == catId);
if (selectedCategory) {
const selectedTask = selectedCategory.tasks.find(task => task.taskId == taskId);
if (selectedTask) {
selectedTask.complete = !selectedTask.complete;
this.saveToLocalStorage();
this.renderTasksByCatId(catId);
}
}
} // CHECKED
deleteCompletedTasks(catId) {
const selectedCategory = this.datas.find(item => item.catId == catId);
if (selectedCategory) {
selectedCategory.tasks = selectedCategory.tasks.filter(task => !task.complete);
this.saveToLocalStorage();
this.renderTasksByCatId(catId);
}
}
renderCategory() {
const catContainer = document.getElementById("cat-container");
removeChildrensById("cat-container");
this.datas.forEach(item => {
const catElem = document.createElement('li');
catElem.classList.add("cat-name", "flex-between", "flex-center");
catElem.dataset.catId = item.catId;
const categoryName = document.createElement('span');
categoryName.textContent = item.catName;
catElem.appendChild(categoryName);
if (item.catName !== "My Tasks") {
const btnWrapper = document.createElement('div');
btnWrapper.classList.add("btn-wrapper");
const checkbox = document.createElement('input');
checkbox.classList.add("cb");
checkbox.type = "checkbox";
const btnBox = document.createElement('div');
btnBox.classList.add("btn-box");
const editButton = document.createElement('button');
editButton.classList.add("btn", "btn-outline");
editButton.textContent = 'Edit';
editButton.addEventListener('click', () => editCategory(item.catId));
const deleteButton = document.createElement('button');
deleteButton.classList.add("btn", "btn-outline");
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', () => this.deleteCategory(item.catId));
btnBox.appendChild(editButton);
btnBox.appendChild(deleteButton);
btnWrapper.appendChild(checkbox);
btnWrapper.appendChild(btnBox);
catElem.appendChild(btnWrapper);
}
const catElemDivider = document.createElement('hr');
catElemDivider.classList.add("cat-divider");
catContainer.appendChild(catElem);
catContainer.appendChild(catElemDivider);
});
} // CHECKED
renderTasksByCatId(catId) {
const tasksHeader = document.getElementById("tasks-header");
const tasksContainer = document.getElementById("tasks-container");
removeChildrensById("tasks-header");
removeChildrensById("tasks-container");
const category = this.datas.find(item => item.catId == catId);
// Count completed and remaining tasks
const completedTasks = category.tasks.filter(task => task.complete).length;
const remainingTasks = category.tasks.length - completedTasks;
const heading = document.createElement("h2");
heading.classList.add("heading");
heading.textContent = category.catName;
const deleteTasksButton = document.createElement("button");
// Add classes and attributes to the button
deleteTasksButton.classList.add("btn", "btn-ghost");
// deleteTasksButton.dataset.catId = catId;
deleteTasksButton.textContent = "Delete Completed Tasks";
deleteTasksButton.addEventListener('click', () => {
// this.toggleTaskComplete(catId, item.taskId);
this.deleteCompletedTasks(catId);
});
const taskCounter = document.createElement("span");
taskCounter.classList.add("task-counter")
taskCounter.textContent = ` ${remainingTasks} out of ${category.tasks.length} remaining`;
// if(completedTasks.length)
tasksHeader.appendChild(heading);
if (completedTasks > 0) {
tasksHeader.appendChild(deleteTasksButton);
}
tasksHeader.appendChild(taskCounter);
category.tasks.forEach(item => {
const taskElem = document.createElement('li');
taskElem.classList.add("task", "flex-between", "flex-center", `${item.complete && "complete"}`);
const indicatorBoxElem = document.createElement("span");
indicatorBoxElem.classList.add("indicator-box");
const cbElem = document.createElement("input");
cbElem.type = "checkbox";
cbElem.classList.add("indicator");
cbElem.checked = item.complete;
cbElem.addEventListener('change', () => {
this.toggleTaskComplete(catId, item.taskId);
});
const taskDesc = document.createElement('p');
taskDesc.classList.add("task-desc");
taskDesc.textContent = item.taskDesc;
const btnWrapper = document.createElement('div');
btnWrapper.classList.add("btn-wrapper");
const checkbox = document.createElement('input');
checkbox.classList.add("cb");
checkbox.type = "checkbox";
const btnBox = document.createElement('div');
btnBox.classList.add("btn-box");
const editButton = document.createElement('button');
editButton.classList.add("btn", "btn-outline");
editButton.textContent = 'Edit';
editButton.addEventListener('click', () => editTask(catId, item.taskId));
const deleteButton = document.createElement('button');
deleteButton.classList.add("btn", "btn-outline");
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', () => this.deleteTask(catId, item.taskId));
indicatorBoxElem.appendChild(cbElem);
btnBox.appendChild(editButton);
btnBox.appendChild(deleteButton);
btnWrapper.appendChild(checkbox);
btnWrapper.appendChild(btnBox);
taskElem.appendChild(indicatorBoxElem);
taskElem.appendChild(taskDesc);
taskElem.appendChild(btnWrapper);
tasksContainer.appendChild(taskElem);
});
} // CHECKED
}
// Add event listener for editing a category
function editCategory(catId) {
if (catId) {
const newCatName = prompt("Enter the new category name:");
if (newCatName !== null && newCatName.trim() !== '') {
dm.editCategory(catId, newCatName);
}
}
} // CHECKED
// Add event listener for editing a task
function editTask(catId, taskId) {
if (catId && taskId) {
const newTaskDesc = prompt("Enter the new task description:");
if (newTaskDesc !== null && newTaskDesc.trim() !== '') {
dm.editTask(catId, taskId, newTaskDesc);
}
}
} // CHECKED
// Add event listener for deleting a task
function deleteTask() {
const catId = document.querySelector(".sidebar .cat-name.active").dataset.catId;
const taskId = document.querySelector(".tasks-container .task.active").dataset.taskId;
if (catId && taskId) {
dm.deleteTask(catId, taskId);
}
} // CHECKED
// =================================================================================
// HELPER FUNCTION
// =================================================================================
// set active category
const catContainer = document.querySelector("#cat-container");
catContainer.addEventListener("click", (event) => {
const clickedElement = event.target;
if (clickedElement.classList.contains("cat-name")) {
const catNames = document.querySelectorAll(".sidebar .cat-name");
catNames.forEach(item => {
item.classList.remove("active");
});
clickedElement.classList.add("active");
dm.renderTasksByCatId(clickedElement.dataset.catId);
}
}); // CHECKED
function setFirstCatActive() {
const elem = document.querySelectorAll("#cat-container .cat-name")
elem[0].click();
} // CHECKED
function removeChildrensById(id) {
const parentElement = document.getElementById(id);
while (parentElement.firstChild) {
parentElement.removeChild(parentElement.firstChild);
}
} // CHECKED
document.getElementById("cat-input").addEventListener("keydown", (e) => {
if ((e.key === "Enter" || e.keyCode === 13) && e.ctrlKey) {
addCat()
}
}) // CHECKED
document.getElementById("task-input").addEventListener("keydown", (e) => {
if ((e.key === "Enter" || e.keyCode === 13) && e.ctrlKey) {
addTask()
}
}) // CHECKED
function calcAndSetSidebarHeight() {
var sidebar = document.querySelector('#sidebar');
var windowWidth = window.innerWidth;
if (windowWidth < 767) {
var sidebarHeight = sidebar.clientHeight;
sidebar.style.setProperty('--sidebar-height', sidebarHeight + 'px');
} else {
sidebar.style.setProperty('--sidebar-height', '0');
}
} window.addEventListener('resize', calcAndSetSidebarHeight);
// ================================== INITIAL CALL =====================================
calcAndSetSidebarHeight();
const dm = new DataManager(); // CHECKED
dm.renderCategory(); // CHECKED
setFirstCatActive() // CHECKED