-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
184 lines (146 loc) · 4.53 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
const itemform = document.querySelector('#item-form');
const iteminput = document.querySelector('#item-input');
const itemlist = document.querySelector('#item-list');
const clearbtn = document.querySelector('#clear');
const formbtn = itemform.querySelector('button');
const filter = document.querySelector('#filter');
let isEditMode = false;
function displayitems(){
const itemsFromStorage = getItemsFromStorage();
itemsFromStorage.forEach(n => addItemToDOM(n));
checkUI();
}
function addItemToDOM(item){
const li = document.createElement('li');
li.appendChild(document.createTextNode(item))
const button = createButton("remove-item btn-link text-red");
button.appendChild(createIcon('fa-solid fa-xmark'));
li.appendChild(button);
itemlist.appendChild(li);
}
function createButton(classes){
const btn = document.createElement('button');
btn.className = classes;
return btn;
}
function addItemToStorage(item){
let itemsFromStorage = getItemsFromStorage();
if(localStorage.getItem('items') === null){
itemsFromStorage = [];
} else {
itemsFromStorage = JSON.parse(localStorage.getItem('items'));
}
itemsFromStorage.push(item);
localStorage.setItem('items',JSON.stringify(itemsFromStorage));
}
function createIcon(classes){
const icn = document.createElement('i');
icn.className = classes;
return icn;
}
function addItem(e){
e.preventDefault();
const newItem = iteminput.value;
// Validate input
if(newItem === ''){
alert('please add an item');
return
}
if(isEditMode){
const itemtoedit = itemlist.querySelector('.edit-mode');
removeItemFromStorage(itemtoedit.innerText);
itemtoedit.classList.remove('edit-mode');
itemtoedit.remove();
isEditMode = false;
}
else {
if (checkIfItemExists(newItem)){
alert('that item already exists!');
return;
};
}
// create list item
addItemToDOM(newItem);
addItemToStorage(newItem);
checkUI();
iteminput.value = '';
}
function getItemsFromStorage(){
let itemsFromStorage;
if(localStorage.getItem('items') === null){
itemsFromStorage = [];
} else {
itemsFromStorage = JSON.parse(localStorage.getItem('items'));
}
return itemsFromStorage;
}
function onClickRemoveItem(e){
if(e.target.parentElement.classList.contains('remove-item')){
removeItem(e.target.parentElement.parentElement);
}
else {
setItemToEdit(e.target);
}
}
function setItemToEdit(item){
isEditMode = true;
itemlist.querySelectorAll('li').forEach(n => n.classList.remove('edit-mode'))
item.classList.add('edit-mode');
formbtn.innerHTML = '<i class="fa-solid fa-pen"></i> Update Items';
formbtn.style.backgroundColor = '#228B22'
iteminput.value = item.innerText;
}
function removeItem(item){
item.remove();
removeItemFromStorage(item.textContent);
checkUI();
}
function removeItemFromStorage(item) {
let itemsFromStorage = getItemsFromStorage();
itemsFromStorage = itemsFromStorage.filter(n => n !== item);
localStorage.setItem('items',JSON.stringify(itemsFromStorage));
}
function clearItems(e){
if(confirm('Are you sure?')){
while(itemlist.firstChild){
itemlist.removeChild(itemlist.firstChild);
}
}
localStorage.removeItem('items');
checkUI();
}
function checkUI(){
iteminput.value = '';
const items = itemlist.querySelectorAll('li');
if(items.length === 0){
filter.style.display = 'none';
clearbtn.style.display = 'none';
} else {
filter.style.display = 'block';
clearbtn.style.display = 'block';
}
formbtn.innerHTML = '<i class = "fa-solid fa-plus"></i> Add Item';
formbtn.style.backgroundColor = '#333';
isEditMode = false;
}
function filteritems(e){
let value = filter.value.toLowerCase();
itemlist.querySelectorAll('li').forEach(n => {
if(n.innerText.toLowerCase().indexOf(value) === -1){
n.style.display = 'none';
}else{
n.style.display = 'flex';
}
})
}
function checkIfItemExists(item){
const itemfromlocal = getItemsFromStorage();
return itemfromlocal.includes(item);
}
// Event Listeners
itemform.addEventListener('submit', addItem);
itemlist.addEventListener('click', onClickRemoveItem);
clearbtn.addEventListener('click', clearItems);
filter.addEventListener('input', filteritems)
document.addEventListener('DOMContentLoaded', displayitems)
checkUI();