-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
178 lines (119 loc) · 3.37 KB
/
main.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
var listId = "todoList"
var localAdapter = {
saveList: function (object) {
var stringified = JSON.stringify(object);
localStorage.setItem(listId, stringified);
return true;
},
getList: function() {
return JSON.parse(localStorage.getItem(listId));
},
clearList: function() {
localStorage.removeItem(listId);
}
};
var storage = localAdapter;
function saveLocalTodos(todos) {
let todosList;
if(localStorage.getItem('todos') === null) {
todosList = [];
} else{
todosList = JSON.parse(localStorage.getItem('todos'));
}
todosList.push(todos);
localStorage.setItem(todos, JSON.stringify(todosList));
}
function getTodos(){
let todos;
if(localStorage.getItem('todos') === null){
todos = [];
}else{
todos = JSON.parse(localStorage.getItem('todos'));
}
todos.forEach(function(todo){
});
}
var storage = localAdapter;
//button to add new task
let addButton = document.querySelector('#add')
//input field to add new task
let addInput = document.querySelector('#item')
//font awesome icons for buttons
let removeI = '<i class="fa-solid fa-trash"></i>'
let completeI = '<i class="fa-solid fa-check"></i>'
//add task clicking add button
addButton.addEventListener('click', function() {
let newItem = document.getElementById('item').value
if(document.getElementById('item').value.length == 0) {
alert("Please enter a task")
}
//add to local storage
saveLocalTodos(addInput.value);
if (newItem) {
addItemTodo(newItem)
document.getElementById('item').value = ''
}
})
//add task clicking enter key
addInput.addEventListener('keypress', function(e) {
if (13 === e.keyCode) {
let newItem = document.getElementById('item').value
if (newItem) {
addItemTodo(newItem)
document.getElementById('item').value = ''
}
}
})
let todoId = 0;
function addItemTodo(text) {
//grab 'ul'
let list = document.getElementById('todo')
//create 'li'
let item = document.createElement('li')
item.innerText = text
todoId += 1;
item.id = "todo" + todoId;
//create container for buttons remove and complete
let buttons = document.createElement('div')
buttons.classList.add('buttons')
//create two buttons
let remove = document.createElement('button')
remove.classList.add('remove')
//Font Awesome icon added to button
remove.innerHTML = removeI
remove.addEventListener('click', removeItem)
let complete = document.createElement('button')
complete.classList.add('complete')
//Font Awesome icon added to button
complete.innerHTML = completeI
complete.addEventListener('click', completeItem)
//append buttons to 'div'
buttons.appendChild(remove)
buttons.appendChild(complete)
//append 'div' to 'li'
item.appendChild(buttons)
//prepend 'li' to 'ul' - adds new tasks to top automatically
list.insertBefore(item, list.childNodes[0])
}
function completeItem(event) {
const target = event.target
//grab 'li'
let item = target.parentNode.parentNode
//grab 'ul'
let parent = item.parentNode
//grab parent id
let id = parent.id
//remove item from current 'ul'
parent.parentNode.removeChild(parent)
const completed = document.getElementById("completed")
//add item to new 'ul'
completed.insertBefore(parent, completed.firstChild)
}
function removeItem() {
//grab 'li'
let item = this.parentNode.parentNode
//grab 'ul'
let parent = item.parentNode
//remove 'li' from 'ul'
parent.removeChild(item)
}