-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
170 lines (144 loc) · 4.12 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
const itemForm = document.getElementById("item-form");
const itemInput = document.getElementById("item-input");
const itemList = document.getElementById("item-list");
const clearBtn = document.getElementById("clear");
const filter = document.getElementById("filter");
const submitBtn = itemForm.querySelector("button");
let isEditMode = false;
function createButton(classes) {
const button = document.createElement("button");
button.className = classes;
button.appendChild(createIcon("fa-solid fa-xmark"));
return button;
}
function createIcon(classes) {
const icon = document.createElement("i");
icon.className = classes;
return icon;
}
function displayItems() {
const storageItems = getStorageItems();
storageItems.forEach((item) => addtoDOM(item));
checkUI();
}
function addItem(e) {
e.preventDefault();
const newItem = itemInput.value;
if (newItem == "") {
alert("Please add an item");
return;
}
if (isEditMode) {
const editItem = itemList.querySelector(".edit-mode");
removeStorageItem(editItem.textContent);
editItem.remove();
isEditMode = false;
} else {
if (itemExists(newItem)) {
alert("Item already exists!");
return;
}
}
addtoDOM(newItem);
addtoStorage(newItem);
checkUI();
itemInput.value = "";
}
function addtoDOM(newItem) {
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);
}
function addtoStorage(newItem) {
const storageItems = getStorageItems();
storageItems.push(newItem);
localStorage.setItem("items", JSON.stringify(storageItems));
}
function getStorageItems() {
let storageItems;
if (localStorage.getItem("items") === null) {
storageItems = [];
} else {
storageItems = JSON.parse(localStorage.getItem("items"));
}
return storageItems;
}
function clickItem(e) {
if (e.target.parentElement.classList.contains("remove-item")) {
deleteItem(e.target.parentElement.parentElement);
} else {
editMode(e.target);
}
}
function itemExists(item) {
const storageItems = getStorageItems();
return storageItems.includes(item);
}
function editMode(item) {
isEditMode = true;
itemList
.querySelectorAll("li")
.forEach((i) => i.classList.remove("edit-mode"));
item.classList.add("edit-mode");
submitBtn.innerHTML = '<i class="fa-solid fa-pen"></i> Edit Item';
submitBtn.style.backgroundColor = "#228B22";
itemInput.value = item.textContent;
}
function deleteItem(item) {
if (confirm("Are you sure?")) {
item.remove();
removeStorageItem(item.textContent);
checkUI();
}
}
function removeStorageItem(item) {
let storageItems = getStorageItems();
storageItems = storageItems.filter((storageItem) => storageItem !== item);
localStorage.setItem("items", JSON.stringify(storageItems));
}
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";
}
submitBtn.innerHTML = '<i class="fa-solid fa-plus"></i> Add Item';
submitBtn.style.backgroundColor = "#333";
isEditMode = false;
}
function filterItems(e) {
const text = e.target.value.toLowerCase();
const items = itemList.querySelectorAll("li");
items.forEach((item) => {
const itemName = item.firstChild.textContent.toLowerCase();
if (itemName.indexOf(text) != -1) {
item.style.display = "flex";
} else {
item.style.display = "none";
}
});
}
function init() {
itemForm.addEventListener("submit", addItem);
itemList.addEventListener("click", clickItem);
clearBtn.addEventListener("click", clearItems);
filter.addEventListener("input", filterItems);
document.addEventListener("DOMContentLoaded", displayItems);
checkUI();
}
init();