-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
234 lines (164 loc) · 5.07 KB
/
script.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
224
225
226
227
228
229
230
231
232
233
234
const itemForm = document.getElementById('item-form');
const itemInput = document.getElementById('item-input');
const itemList = document.getElementById('item-list');
const clearBtn = document.getElementById('clear');
const itemFilter = document.getElementById('filter');
let isEditMode = false;
const formBtn = itemForm.querySelector('button');
function displayItems(){
const itemFromStorage = getItemfromStorage();
itemFromStorage.forEach((item) => addItemToDOM(item));
checkUI();
}
function onAddItem(e){
e.preventDefault();
const newItem = itemInput.value;
// Validate input
if (newItem === '')
{
alert('Please add an item');
return;
}
// Check for edit mode
if (isEditMode){
const itemToEdit = itemList.querySelector('.edit-mode');
removeItemFromStorage(itemToEdit.textContent);
itemToEdit.classList.remove('edit-mode');
itemToEdit.remove();
isEditMode = false;
}else {
if(checkIfItemExists(newItem)){
alert('That item is already exists!');
return;
}
}
//Create item DOM element
//addItemToDOM(newItem);
//Add item to locaal storage
addItemToStorage(newItem);
//Create list item
const li = document.createElement('li');
li.appendChild(document.createTextNode(newItem));
const button = createButton('remove-item btn-link text-red');
li.appendChild(button);
itemList.appendChild(li);
checkUI();
itemInput.value ='';
}
function addItemToDOM(item){
const li = document.createElement('li');
li.appendChild(document.createTextNode(item));
const button = createButton('remove-item btn-link text-red');
li.appendChild(button);
itemList.appendChild(li);
}
function addItemToStorage(item){
const itemFromStorage=getItemfromStorage();
//add new item to array
itemFromStorage.push(item);
//Convert to JSON string and set to locl storage
localStorage.setItem('items', JSON.stringify(itemFromStorage));
}
function getItemfromStorage(item){
let itemFromStorage;
if(localStorage.getItem('items') === null){
itemFromStorage =[];
}else{
itemFromStorage =JSON.parse(localStorage.getItem('items'));
}
return itemFromStorage;
}
function createButton(classes){
const button = document.createElement('button');
button.className = classes;
const icon =createIcon('fa-solid fa-xmark')
button.appendChild(icon);
return button;
}
function createIcon(classes)
{
const icon = document.createElement('i');
icon.className = classes;
return icon;
}
function onClickItem(e) {
if(e.target.parentElement.classList.contains('remove-item'))
{
removeItem(e.target.parentElement.parentElement);
}else{
setItemToEdit(e.target);
}
}function checkIfItemExists(item){
const itemFromStorage = getItemfromStorage();
return itemFromStorage.includes(item);
}
function setItemToEdit(item){
isEditMode = true;
itemList.querySelectorAll('li').forEach((i) => i.classList.remove('edit-mode'));
item.classList.add('edit-mode');
formBtn.innerHTML ='<i class = "fa-solid fa-pen"></i> Update Item ';
formBtn.style.backgroundColor ='#228B22';
itemInput.value = item.textContent;
}
function removeItem(item)
{
if(confirm('Are you sure ?'))
{
item.remove();
// Remove item from storage
removeItemFromStorage(item.textConent);
checkUI();
}
}
function removeItemFromStorage(item)
{
let itemFromStorage = getItemfromStorage();
//Filter out item to be removed
itemFromStorage = itemFromStorage.filter((i) => i !== item);
//Re-set to localstorage
localStorage.setItem('items', JSON.stringify(itemFromStorage));
}
function clearItems(){
while (itemList.firstChild){
itemList.removeChild(itemList.firstChild);
}
//Clear from localStorage
localStorage.removeItem('item');
checkUI();
}
function filterItems(e) {
const items = itemList.querySelectorAll('li');
const text = e.target.value.toLowerCase();
items.forEach((item) => {
const itemName = item.firstChild.textContent.toLowerCase();
if (itemName.indexOf(text) != -1)
{
item.style.display = 'flex';
} else {
item.style.display ='none';
}
});
}
function checkUI()
{
itemInput.value ='';
const items = itemList.querySelectorAll('li');
if(items.length === 0){
clearBtn.style.display = 'none';
itemFilter.style.display = 'none';
}else{
clearBtn.style.display ='block';
itemFilter.style.display = 'block';
}
formBtn.innerHTML = '<i class = "fa-solid fa-plus"></i> Add Item';
formBtn.style.backgroundColor = '#333';
}
function init(){
itemForm.addEventListener('submit',onAddItem);
itemList.addEventListener('click',onClickItem);
clearBtn.addEventListener('click',clearItems);
itemFilter.addEventListener('input',filterItems);
document.addEventListener('DOMContentLoaded',displayItems);
checkUI();
}
init();