-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (128 loc) · 4.3 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
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
const todoInput = document.getElementById("todo_input");
const todoTasksContainer = document.getElementById("todo_tasks_container");
const modalContainer = document.getElementById("modals_container");
const editModal = document.getElementById("edit_modal");
const deleteModal = document.getElementById("delete_modal");
const editInput = document.getElementById("edit_input");
const themeTogglerButton = document.getElementById("theme_toggler");
// App Theme
let theme = localStorage.getItem("theme");
if (theme == null) {
theme = "dark";
localStorage.setItem("theme", theme);
document.body.classList.add(theme);
} else {
document.body.classList.add(theme);
}
themeTogglerButton.addEventListener("click", () => {
document.body.classList.remove(theme);
theme = localStorage.getItem("theme");
if (theme == "light") {
theme = "dark";
} else if (theme == "dark") {
theme = "light";
}
document.body.classList.add(theme);
localStorage.setItem("theme", theme);
});
let todoTasksArray = JSON.parse(localStorage.getItem("todo-tasks"));
todoTasksArray == null ? (todoTasksArray = []) : todoTasksArray;
let queueIndex = null;
function addTodo(e) {
e.preventDefault();
const todoValue = todoInput.value;
if (todoValue != "") {
todoTasksArray.push(todoValue);
todoInput.value = "";
printTasks();
}
}
function printTasks() {
updateLocalStorage();
todoTasksContainer.innerHTML = "";
todoTasksArray.forEach((todoTask) => {
const todoTaskDiv = document.createElement("div");
todoTaskDiv.classList.add("todo_task");
todoTaskDiv.innerHTML = `
<div class="move_buttons">
<button onclick="moveUp(${todoTasksArray.indexOf(todoTask)})">
<span class="material-symbols-outlined"> arrow_drop_up </span>
</button>
<button onclick="moveDown(${todoTasksArray.indexOf(todoTask)})">
<span class="material-symbols-outlined"> arrow_drop_down </span>
</button>
</div>
<div class="todo_text">${todoTask}</div>
<button class="edit_button" onclick="editTask(${todoTasksArray.indexOf(
todoTask
)})">
<span class="material-symbols-outlined"> edit </span>
</button>
<button class="delete_button" onclick="attemptDelete(${todoTasksArray.indexOf(
todoTask
)})">
<span class="material-symbols-outlined"> delete </span>
</button>
`;
todoTasksContainer.appendChild(todoTaskDiv);
});
if (todoTasksArray.length >= todoTasksContainer.clientHeight / (43 + 20)) {
todoTasksContainer.style.overflowY = "scroll";
}
}
function moveUp(index) {
if (index > 0) {
let prevTask = todoTasksArray[index - 1];
let currTask = todoTasksArray[index];
todoTasksArray[index - 1] = currTask;
todoTasksArray[index] = prevTask;
printTasks();
}
}
function moveDown(index) {
if (index < todoTasksArray.length - 1) {
let nextTask = todoTasksArray[index + 1];
let currTask = todoTasksArray[index];
todoTasksArray[index + 1] = currTask;
todoTasksArray[index] = nextTask;
printTasks();
}
}
function editTask(index) {
queueIndex = index;
editInput.value = todoTasksArray[index];
modalContainer.classList.add("active");
editModal.classList.add("active");
}
function updateTask(e) {
e.preventDefault();
todoTasksArray[queueIndex] = editInput.value;
modalContainer.classList.remove("active");
editModal.classList.remove("active");
printTasks();
queueIndex = null;
}
function attemptDelete(index) {
queueIndex = index;
modalContainer.classList.add("active");
deleteModal.classList.add("active");
}
function deleteTask() {
todoTasksArray.splice(queueIndex, 1);
modalContainer.classList.remove("active");
deleteModal.classList.remove("active");
queueIndex = null;
printTasks();
}
function cancelOperation(e) {
e.preventDefault();
modalContainer.classList.remove("active");
deleteModal.classList.remove("active");
editModal.classList.remove("active");
queueIndex = null;
}
function updateLocalStorage() {
const stringifiedTasksArray = JSON.stringify(todoTasksArray);
localStorage.setItem("todo-tasks", stringifiedTasksArray);
}
printTasks();