-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
219 lines (162 loc) · 6.31 KB
/
app.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
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
// define UI vars
const form = document.querySelector('#task-form')
const taskList = document.querySelector('.collection')
const clearBtn = document.querySelector('.clear-task')
const filter = document.querySelector('#filter')
const taskInput = document.querySelector('#task')
// load all event listener call
loadEventListeners()
// load all event listener function
function loadEventListeners() {
// add task event
form.addEventListener('submit', addTask)
// remove task event
taskList.addEventListener('click', removeTask)
// clear task event
clearBtn.addEventListener('click', clearTasks)
// filter tasks event
filter.addEventListener('keyup', filterTasks)
// load dom event => to get localStorage data on load
document.addEventListener('DOMContentLoaded', getTasks)
}
// add task function
function addTask(e) {
e.preventDefault();
// make sure that the field isnot empty on submit
if(taskInput.value == '') {
alert('Add a Task!')
}
// create li element
const li = document.createElement('li')
// add class to li
li.className = 'collection-item'
// create text node & append to li
li.appendChild(document.createTextNode(taskInput.value))
// create new link to li => it'll be a close btn to remove the li
const link = document.createElement('a')
//add class to a
link.className = 'delete-item secondary-content'
// add icon to link
link.innerHTML = '<i class="fa fa-remove"></i>'
// append the link to li
li.appendChild(link)
// append li to ul
taskList.appendChild(li)
// store li in localStorage
storeTaskInLocalStorage(taskInput.value)
// clear input field on submit
taskInput.value = ''
}
//store in localstorage function
function storeTaskInLocalStorage(task){
let tasks
// check if theres already something there
if(localStorage.getItem('tasks') === null) {
tasks = [] //if not => then set tasks to empty array
} else {
// if there's => then get it and convert it to json
// because localStorage stores only string
tasks = JSON.parse(localStorage.getItem('tasks'))
}
// now we got the data from localStorage => it's time to push it
tasks.push(task)
// now we have to store the data back to localStorage
// remoember the data should be stored as string
localStorage.setItem('tasks', JSON.stringify(tasks))
}
//get tasks function
function getTasks() {
let tasks
// check if theres already something there
if(localStorage.getItem('tasks') === null) {
tasks = [] //if not => then set tasks to empty array
} else {
// if there's => then get it and convert it to json
// because localStorage stores only string
tasks = JSON.parse(localStorage.getItem('tasks'))
}
// loop through the tasks in localStorage and display them in DOM
// we creat li for each task
tasks.forEach(function(task){
// create li element
const li = document.createElement('li')
// add class to li
li.className = 'collection-item'
// create text node & append to li
li.appendChild(document.createTextNode(task))
// create new link to li => it'll be a close btn to remove the li
const link = document.createElement('a')
//add class to a
link.className = 'delete-item secondary-content'
// add icon to link
link.innerHTML = '<i class="fa fa-remove"></i>'
// append the link to li
li.appendChild(link)
// append li to ul
taskList.appendChild(li)
})
}
// remove task function
function removeTask(e) {
if(e.target.parentElement.classList.contains('delete-item')) { //we have to get the parent (link) .. because it gives us the icon (i) by default
// confirm before removing
if(confirm('Are you sure?')) { //if yes => returns true
e.target.parentElement.parentElement.remove() //this will remove it from the DOM
// we have also to remove it from localstorage
removeTaskFromLocalStorage(e.target.parentElement.parentElement)
}
}
}
// remove from localStorage function
function removeTaskFromLocalStorage(taskItem) {
// console.log(taskItem)
// check localStorage
let tasks
// check if theres already something there
if(localStorage.getItem('tasks') === null) {
tasks = [] //if not => then set tasks to empty array
} else {
// if there's => then get it and convert it to json
// because localStorage stores only string
tasks = JSON.parse(localStorage.getItem('tasks'))
}
// loop through the data
tasks.forEach(function(task, index) {
// check if the text of the task matches the one in the ittration
if(taskItem.textContent === task) {
// if so, then this is the one to remove
tasks.splice(index, 1)
}
})
// after we removed the task => let's set (update) the localstorage
localStorage.setItem('tasks', JSON.stringify(tasks))
}
// clear tasks function
function clearTasks() {
// taskList.innerHTML = ''
// another way to do it => faster than the first one
while(taskList.firstChild) { //while there's still a list first child => means theres still something in the list
taskList.removeChild(taskList.firstChild)
}
// we have also to clear everything from localStorage
clearTasksFromLocalStorage()
}
// clear tasks from localStorage function
function clearTasksFromLocalStorage() {
localStorage.clear() //thats clear out everything in local storage
}
//filter tasks function
function filterTasks(e) {
//get what's written in the field
const text = e.target.value.toLowerCase() // convert to lowerCase => so it matches correctly
// we have to get all li items and loop through them
// we can use forEach because querySelectorAll returns nodeList
document.querySelectorAll('.collection-item').forEach(function(task){
const item = task.firstChild.textContent; //get the text of the li item
if(item.toLowerCase().indexOf(text) != -1) { //if there's no match => it gonna equale to -1
task.style.display = 'block' //show the element if it does match
} else { // there's no match
task.style.display = 'none'
}
})
}