-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
195 lines (169 loc) · 6.25 KB
/
index.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const inputBox = document.getElementById('inputBox');
const addBtn = document.getElementById('addBtn');
const todoList = document.getElementById('todoList');
//global variable
let editTodo = null;
const addTodo = () => {
const inputText = inputBox.value.trim();
if (inputText.length <= 0) {
alert("You must write something!!")
return false;
}
if (addBtn.value === 'Edit') {
editLocalTodos(editTodo.target.previousElementSibling.innerHTML);
editTodo.target.previousElementSibling.innerHTML = inputText;
addBtn.value = 'Add';
inputBox.value = "";
}
else {
//Creating P tag
const li = document.createElement('li');
const p = document.createElement('p');
p.innerHTML = inputText;
li.appendChild(p);
todoList.appendChild(li);
//Creating Edit Button
const editBtn = document.createElement('button');
editBtn.innerText = 'Edit';
editBtn.classList.add('btn', 'editBtn');
li.appendChild(editBtn);
//Creating Delete btn
const deleteBtn = document.createElement('button');
deleteBtn.innerText = 'Remove';
deleteBtn.classList.add('btn', 'deleteBtn')
li.appendChild(deleteBtn);
//Creating Done btn
const doneBtn = document.createElement('button');
doneBtn.innerText = 'Done';
doneBtn.classList.add('btn', 'doneBtn')
li.appendChild(doneBtn);
//Empty todo box after adding
inputBox.value = "";
saveLocalTodos(inputText);
}
}
// const updateTodo = (e) => {
// if (e.target.innerHTML === 'Remove') {
// todoList.removeChild(e.target.parentElement);
// deleteLocalTodos(e.target.parentElement);
// }
// else if (e.target.innerHTML === 'Edit') {
// inputBox.value = e.target.previousElementSibling.innerHTML;
// inputBox.focus();
// addBtn.value = 'Edit';
// editTodo = e;
// }
// else if (e.target.innerHTML === 'Done') {
// const list = e.target.parentElement;
// const doneText = list.querySelector('p');
// doneText.style.textDecoration = 'line-through';
// // list.parentElement.appendChild(list);
// e.target.innerHTML = 'Undo';
// }
// else if (e.target.innerHTML === 'Undo') {
// const list = e.target.parentElement;
// const doneText = list.querySelector('p');
// doneText.style.textDecoration = '';
// e.target.innerHTML = 'Done';
// }
// }
const updateTodo = (e) => {
if (e.target.innerHTML === 'Remove') {
todoList.removeChild(e.target.parentElement);
deleteLocalTodos(e.target.parentElement);
} else if (e.target.innerHTML === 'Edit') {
inputBox.value = e.target.previousElementSibling.innerHTML;
inputBox.focus();
addBtn.value = 'Edit';
editTodo = e;
} else if (e.target.innerHTML === 'Done') {
const list = e.target.parentElement;
const doneText = list.querySelector('p');
doneText.style.textDecoration = 'line-through';
e.target.innerHTML = 'Undo';
// Update local storage
const todos = JSON.parse(localStorage.getItem("todos"));
const todoIndex = todos.findIndex(todo => todo === doneText.innerHTML);
if (todoIndex !== -1) {
todos[todoIndex] = `<s>${doneText.innerHTML}</s>`; // Add line-through HTML tags
localStorage.setItem("todos", JSON.stringify(todos));
}
} else if (e.target.innerHTML === 'Undo') {
const list = e.target.parentElement;
const doneText = list.querySelector('p');
doneText.style.textDecoration = '';
e.target.innerHTML = 'Done';
// Update local storage
const todos = JSON.parse(localStorage.getItem("todos"));
const todoIndex = todos.findIndex(todo => todo === `<s>${doneText.innerHTML}</s>`);
if (todoIndex !== -1) {
todos[todoIndex] = doneText.innerHTML; // Remove line-through HTML tags
localStorage.setItem("todos", JSON.stringify(todos));
}
}
}
const saveLocalTodos = (todo) => {
let todos;
if (localStorage.getItem("todos") === null) {
todos = [];
}
else {
todos = JSON.parse(localStorage.getItem("todos"));
}
todos.push(todo);
localStorage.setItem("todos", JSON.stringify(todos));
}
const getLocalTodos = () => {
let todos;
if (localStorage.getItem("todos") === null) {
todos = [];
}
else {
todos = JSON.parse(localStorage.getItem("todos"));
todos.forEach(todo => {
//Creating P tag
const li = document.createElement('li');
const p = document.createElement('p');
p.innerHTML = todo;
li.appendChild(p);
//Creating Edit Button
const editBtn = document.createElement('button');
editBtn.innerText = 'Edit';
editBtn.classList.add('btn', 'editBtn');
li.appendChild(editBtn);
//Creating Delete btn
const deleteBtn = document.createElement('button');
deleteBtn.innerText = 'Remove';
deleteBtn.classList.add('btn', 'deleteBtn')
li.appendChild(deleteBtn);
//Creating Done btn
const doneBtn = document.createElement('button');
doneBtn.innerText = 'Done';
doneBtn.classList.add('btn', 'doneBtn')
li.appendChild(doneBtn);
todoList.appendChild(li);
});
}
}
const deleteLocalTodos = (todo) => {
let todos;
if (localStorage.getItem("todos") === null) {
todos = [];
}
else {
todos = JSON.parse(localStorage.getItem("todos"));
}
let todoText = todo.children[0].innerHTML;
let todoIndex = todos.indexOf(todoText);
todos.splice(todoIndex, 1);
localStorage.setItem("todos", JSON.stringify(todos));
}
const editLocalTodos = (todo) => {
let todos = JSON.parse(localStorage.getItem("todos"));
let todoIndex = todos.indexOf(todo);
todos[todoIndex] = inputBox.value;
localStorage.setItem("todos", JSON.stringify(todos));
}
document.addEventListener('DOMContentLoaded', getLocalTodos);
addBtn.addEventListener('click', addTodo);
todoList.addEventListener('click', updateTodo);