-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.js
More file actions
250 lines (158 loc) · 5.7 KB
/
app2.js
File metadata and controls
250 lines (158 loc) · 5.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
// -------------variables-------------
// the input section elements
const text_input = document.querySelector(".input-container > input")
const input_clear = document.querySelector(".input-clear")
const input_add = document.querySelector(".input-add")
// the section where the to-do sections will get added
const todo_section = document.querySelector("#todo-section")
window.onload = () => {
get_from_ls()
}
// -----Adding the clear input box feature-----
input_clear.addEventListener("click", () => {
text_input.value = ""
})
// Adding the add todo function when preseed enter
text_input.addEventListener("keydown", (e)=>{
if(e.key === "Enter")
{
add_todo()
}
})
// ----Checking the input for no value----
input_add.addEventListener("click", ()=>{
if(text_input.value === "")
{
text_input.classList.add("red-border")
}
})
text_input.addEventListener("keyup", ()=>{
if(text_input.value !== "")
{
text_input.classList.remove("red-border")
}
})
// -----Add todo button-----
let id_counter = 0
input_add.addEventListener("click", add_todo)
function add_todo() {
if(text_input.value === "") return
// creating an object everytime the add buttons is clicked
let myobj = {
content: text_input.value,
id: id_counter++,
strike_status: false
}
// adding to the dom
todo_section.innerHTML += `<div class="todo-container" id="${myobj.id}">
<p class="todo-para">${myobj.content}</p>
<span class="material-icons">done</span>
<span class="material-icons">delete</span>
</div>`
// adding the current todo to the local storage
add_to_ls(myobj)
// checking if the todo container has no todos and removing the warning if todos are present
if(todo_section.children.length > 0){
document.querySelector(".no-todos").style.cssText = "display: none"
}
// clear input box
text_input.value = ""
}
// Adding the todo input to the local storage
function add_to_ls(todo) {
let todos
// checking if the local storage is empty
if (localStorage.getItem("todos") === null) {
todos = []
}
// getting the local storage items in the array
else {
todos = JSON.parse(localStorage.getItem("todos"))
}
// pushing the current todo to the array
todos.push(todo)
// pushing the updated array to the local storage
localStorage.setItem("todos", JSON.stringify(todos))
}
// If no todos are present in the todo container
// --------Getting all the elements from the local storage and printing it top the dom--------
function get_from_ls() {
let todos = JSON.parse(localStorage.getItem("todos"))
//displaying the message if no todos are present in the container
if(todos.length == 0){
document.querySelector(".no-todos").style.cssText += "display: inherit; text-align: center"
}
else{
document.querySelector(".no-todos").style.cssText += "display: none"
}
// adding the individual objects to the DOM
todos.forEach(element => {
let temp;
todo_section.innerHTML += `<div class="todo-container ${element.strike_status == "true"?"done-animation" : ""}" id="${element.id}">
<p class="todo-para ${element.strike_status == "true"?"strike" : ""}">${element.content}</p>
<span class="material-icons">done</span>
<span class="material-icons">delete</span>
</div>`
id_counter = element.id + 1
});
}
// adding event listeners to the indiviual todo to implement the done and delete feature
todo_section.addEventListener("click", (e) => {
// Event listener for the trash button
if (e.target.textContent === "delete") {
e.target.parentElement.classList.add("delete-animation")
// calling the delete function after 400ms to allow the animation to be visible
setTimeout(() => {
del_todo(e.target.parentElement)
}, 400);
}
// Event listener for the done button
else if (e.target.textContent === "done") {
// e.target.parentElement.classList.toggle("done-animation")
done_todo(e.target.parentElement)
}
})
// ----Deleting items from local storage and DOM----
function del_todo(parentElement) {
// console.log(parentElement.id);
let todos = JSON.parse(localStorage.getItem("todos"))
for (let index = 0; index < todos.length; index++) {
if (todos[index].id == parentElement.id) {
todos.splice(index, 1)
}
}
localStorage.setItem("todos", JSON.stringify(todos))
// removing from dom
parentElement.remove()
}
// adding the strike thorugh feature
function done_todo(parentElement) {
// making the strike animation changes in DOM
if (parentElement.children[0].classList.contains("strike")) {
parentElement.children[0].classList.remove("strike")
}
else {
parentElement.children[0].classList.add("strike")
}
// making the change scale animation in DOM
if (parentElement.classList.contains("done-animation")) {
parentElement.classList.remove("done-animation")
}
else {
parentElement.classList.add("done-animation")
}
let todos = JSON.parse(localStorage.getItem("todos"))
for (let index = 0; index < todos.length; index++) {
if (todos[index].id == parentElement.id) {
// adding the strike_status value to the local storage
if(parentElement.children[0].classList.contains("strike"))
{
todos[index].strike_status = "true"
}
else{
todos[index].strike_status = "false"
}
}
}
localStorage.setItem("todos", JSON.stringify(todos))
}