-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
223 lines (197 loc) · 5.87 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
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
/*TO DO ITEM = {name: string, check: boolean}*/
//Model
class ToDo {
constructor(items = []) {
this.listItems = items;
}
//Add new list item
addNewItem(nameStr, index) {
//List item object containing name and check status of item
let listItem = {
id: index,
name: nameStr,
check: 0,
};
//Add new item to the list
this.listItems.push(listItem);
return this.listItems;
}
//Delete list item
deleteItem(count) {
//Delete item with certain index from the list
this.listItems.splice(count, 1);
return this.listItems;
}
//Check list item
checkItem(index) {
//Toggle boolean check index
this.listItems[index].check = this.listItems[index].check ? 0 : 1;
return this.listItems;
}
}
//UI handling
//Create new List
let list = new ToDo();
//Render list
function renderList(list) {
//In the beginning, list is empty
let myNode = document.getElementById('uList');
myNode.innerHTML = '';
//Create element for every list item
for (let i = 0; i < list.length; i++) {
//For every item, elements li and span are created, and 'x' (\uOOD7) sign for delete is added
let li = document.createElement('li');
let span = document.createElement('span');
let text = document.createTextNode('\u00D7');
//Text Node with list name is appended to li element
li.appendChild(document.createTextNode(list[i].name));
//'Reset' the input
document.getElementById('uList').appendChild(li);
document.getElementById('inputContent').value = '';
//Add class name 'delete' to span, and 'x' sign, then add everything to li element
span.className = 'delete';
span.appendChild(text);
li.appendChild(span);
//Check if list item has boolean index for checked
if (list[i].check === 1) {
li.classList.add('checked');
}
}
//Add event listeners for deletion and checking of items
deleteElement();
checkedElement();
}
//Adding new element
function addNewElement() {
//Get input value
let inputElement = document.getElementById('inputContent').value;
//Check if input value is empty string
if (inputElement === '') {
alert('You must enter something');
//If not, add new item to the list, and render the list
} else {
let index = list.listItems.length;
let id;
if (index === 0) {
id = 1;
} else {
id = list.listItems[index - 1].id;
}
list.addNewItem(inputElement, id + 1);
renderList(list.listItems);
//Persist Data
persistItems(index);
}
}
//Event Listener for Adding New Element
function addingListener() {
//Select the button and add click event listener to it, which adds new item to the list
let addBtn = document.getElementById('add-btn');
addBtn.addEventListener('click', addNewElement);
//Select input field and add keypress event listener to it, which adds new item to the list if 'Enter' is pressed
let input = document.getElementById('inputContent');
input.addEventListener('keypress', function (event) {
if (event.key === 'Enter') {
event.preventDefault();
addNewElement();
}
});
}
//Delete element
function deleteElement() {
//Select element with 'delete' class
let deleteBtn = document.getElementsByClassName('delete');
//Go through all elements with 'delete' class, and add click event listener to them, which deletes item from the list, then call function to render the list
for (let i = 0; i < deleteBtn.length; i++) {
deleteBtn[i].addEventListener('click', function (event) {
event.stopPropagation();
let id = list.listItems[i].id;
list.deleteItem(i);
renderList(list.listItems);
//Persist Data
persistDeletion(id);
});
}
}
//Add checked to list item
function checkedElement() {
//Select li element
let checkElement = document.getElementsByTagName('li');
//Go through all li elements, toggle their 'check' index on click event, and render the list
for (let i = 0; i < checkElement.length; i++) {
checkElement[i].onclick = function () {
let id = list.listItems[i].id;
list.checkItem(i);
renderList(list.listItems);
//Persist Data
persistChecking(i, id);
};
}
}
//Persist Adding
function persistItems(index) {
fetch('http://localhost:3000/todos', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(list.listItems[index]),
})
.then((response) => {
response.json();
})
.catch((error) => {
console.error('Error:', error);
});
}
//Persist Deletion
function persistDeletion(id) {
fetch(`http://localhost:3000/todos/${id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => {
response.json();
})
.catch((error) => {
console.error('Error:', error);
});
}
//Persist Checking
function persistChecking(index, id) {
fetch(`http://localhost:3000/todos/${id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(list.listItems[index]),
})
.then((response) => {
response.json();
})
.catch((error) => {
console.error('Error:', error);
});
}
//Get JSON data
function getItems() {
fetch('http://localhost:3000/todos')
//Check if there is Stored Data on Server
.then((response) => response.json())
.then((result) => {
list.listItems = result;
renderList(list.listItems);
addingListener();
})
.catch((error) => {
console.error(error);
});
}
//Add REST API and AXIOS
window.addEventListener('load', (event) => {
getItems();
let loader = document.getElementById('loader');
loader.style.display = 'none';
});