-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
109 lines (80 loc) · 3.37 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
var taskArr = [];
const updateView = () => {
const tasksList = document.getElementById("tasksList");
var child = tasksList.lastChild;
while(child) {
tasksList.removeChild(child);
child = tasksList.lastChild;
}
taskArr.forEach((Element, index) => {
const newTask = document.createElement("div");
newTask.setAttribute("class", "task-div");
const taskText = document.createElement("div");
taskText.setAttribute("class", Element.isDone ? "task-text task-completed" : "task-text");
taskText.innerHTML = (index + 1) + ". " + Element.task;
const taskControls = document.createElement("div");
taskControls.setAttribute("class", "task-controls");
const taskEdit = document.createElement("button");
taskEdit.innerHTML = "Edit";
taskEdit.setAttribute("id", index + "edit");
taskEdit.setAttribute("class", "task-btn task-btn-edit");
taskEdit.addEventListener("click", (event) => editTask(event.target.id));
const taskDelete = document.createElement("button");
taskDelete.innerHTML = "Delete";
taskDelete.setAttribute("id", index + "delete");
taskDelete.setAttribute("class", "task-btn task-btn-delete");
taskDelete.addEventListener("click", (event) => deleteTask(event.target.id));
const taskDo = document.createElement("button");
taskDo.innerHTML = Element.isDone ? "Undo" : "Done";
taskDo.setAttribute("id", index + "do");
taskDo.setAttribute("class", "task-btn task-btn-do");
taskDo.addEventListener("click", (event) => doTask(event.target.id));
taskControls.appendChild(taskEdit);
taskControls.appendChild(taskDelete);
taskControls.appendChild(taskDo);
newTask.appendChild(taskText);
newTask.appendChild(taskControls);
tasksList.appendChild(newTask);
});
}
const addTask = (isDone) => {
const task = document.getElementById("task-input").value;
if(task === null || task.trim() === "") return;
taskArr.push({task, isDone});
localStorage.setItem("savedTasks", JSON.stringify(taskArr));
updateView();
const taskInput = document.getElementById("task-input");
taskInput.value = "";
}
const editTask = (id) => {
const taskIndex = parseInt(id[0]);
const taskText = taskArr[taskIndex].task;
taskArr.splice(taskIndex, 1);
localStorage.setItem("savedTasks", JSON.stringify(taskArr));
updateView();
const taskInput = document.getElementById("task-input");
taskInput.value = taskText;
}
const deleteTask = (id) => {
const taskIndex = parseInt(id[0]);
taskArr.splice(taskIndex, 1);
localStorage.setItem("savedTasks", JSON.stringify(taskArr));
updateView();
}
const doTask = (id) => {
const taskIndex = parseInt(id[0]);
taskArr[taskIndex].isDone = !taskArr[taskIndex].isDone;
localStorage.setItem("savedTasks", JSON.stringify(taskArr));
updateView();
}
document.addEventListener("DOMContentLoaded", () => {
const savedTasks = JSON.parse(localStorage.getItem("savedTasks"));
if(savedTasks !== null) taskArr = [...savedTasks];
updateView();
})
document.getElementById("task-submit-btn").addEventListener("click", () => addTask(false));
document.getElementById("task-clear-btn").addEventListener("click", () => {
localStorage.clear();
taskArr = [];
updateView();
})