-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
203 lines (160 loc) · 5.04 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
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');
const formBtn = itemForm.querySelector('button');
let isEditMode = false;
function displayItems() {
const itemsFromStorage = getItemsFromStorage();
itemsFromStorage.forEach(item => addItemToDOM(item));
checkUI();
}
function onAddItemSubmit(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 already exists!');
return;
}
}
// Create item DOM element
addItemToDOM(newItem);
// Add item to local storage
addItemToStorage(newItem);
checkUI();
itemInput.value = '';
}
function addItemToDOM (item) {
// Create list item
const li = document.createElement('li');
li.appendChild(document.createTextNode(item));
const button = createButton("remove-item btn-link text-red");
li.appendChild(button);
// Add li to the DOM
itemList.appendChild(li);
itemInput.value = '';
}
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 addItemToStorage(item) {
const itemsFromStorage = getItemsFromStorage();
// Add new item to array
itemsFromStorage.push(item);
// Convert to JSON string and set to local storage
localStorage.setItem('items', JSON.stringify(itemsFromStorage));
}
function getItemsFromStorage() {
let itemsFromStorage;
if (localStorage.getItem('items') === null) {
itemsFromStorage = [];
} else {
itemsFromStorage = JSON.parse(localStorage.getItem('items'));
}
return itemsFromStorage;
}
function onClickItem(e) {
if (e.target.parentElement.classList.contains('remove-item')){
removeItem(e.target.parentElement.parentElement);
} else {
setItemToEdit(e.target);
}
}
function checkIfItemExists(item) {
const itemsFromStorage = getItemsFromStorage();
return itemsFromStorage.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?')){
// Remove item from DOM
item.remove();
// Remove item from storage
removeItemFromStorage(item.textContent);
checkUI();
}
}
function removeItemFromStorage(item){
let itemsFromStorage = getItemsFromStorage();
// Filter out item to be removed
itemsFromStorage = itemsFromStorage.filter((i) => i !== item);
// Re-set to local storage
localStorage.setItem('items', JSON.stringify(itemsFromStorage));
}
function clearItems(e){
while (itemList.firstChild){
itemList.removeChild(itemList.firstChild)
}
// Clear from localStorage
localStorage.removeItem('items');
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';
isEditMode = false;
}
// Initialize app
function init() {
// Event Listeners
itemForm.addEventListener('submit', onAddItemSubmit);
itemList.addEventListener('click', onClickItem);
clearBtn.addEventListener('click', clearItems);
itemFilter.addEventListener('input', filterItems);
document.addEventListener('DOMContentLoaded', displayItems);
checkUI();
}
init();