-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
161 lines (154 loc) · 4.54 KB
/
script.js
File metadata and controls
161 lines (154 loc) · 4.54 KB
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
const itemInput = document.getElementById("item-input");
const itemForm = document.getElementById("item-form");
const itemList = document.getElementById("item-list");
const clearBtn = document.getElementById("clear");
const filter = document.getElementById("filter");
formBtn = itemForm.querySelector("button");
let isEditMode = false;
// Functions
function display() {
const itemsFromStorage = getItemsFromStorage();
itemsFromStorage.forEach((item) => addItemToDom(item));
checkui();
}
function addItemtostorage(e) {
e.preventDefault();
// Reset button color to default
formBtn.innerHTML = '<i class="fa-solid fa-plus"></i> Add Item';
formBtn.style.backgroundColor = "#333";
const item = itemInput.value;
if (item === "") {
alert("Please enter an item");
return;
}
if (isEditMode) {
itemText = document.querySelector(".edit-mode");
removeItemFromStorage(itemText.textContent);
itemText.classList.remove("edit-mode");
itemText.remove();
isEditMode = false;
} else {
if (ifItemExist(item)) {
alert("Item already exist");
return;
}
}
// adding items to dom
addItemToDom(item);
addToStorage(item);
itemInput.value = "";
checkui();
}
function addToStorage(listItem) {
//Adding to local storage
let itemFromStorage = getItemsFromStorage();
itemFromStorage.push(listItem);
// set items to local storage
localStorage.setItem("listItems", JSON.stringify(itemFromStorage));
}
function getItemsFromStorage() {
let itemFromStorage;
if (localStorage.getItem("listItems") === null) {
itemFromStorage = [];
} else {
itemFromStorage = JSON.parse(localStorage.getItem("listItems"));
}
return itemFromStorage;
}
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 createButton(classes) {
const button = document.createElement("button");
button.className = classes;
const icon = createIcon("fa-solid fa-xmark");
button.appendChild(icon);
return button;
}
function createIcon(classss) {
const icon = document.createElement("i");
icon.className = classss;
return icon;
}
function removeAll() {
while (itemList.firstChild) {
itemList.removeChild(itemList.firstChild);
localStorage.clear();
}
checkui();
}
function onClickItem(e) {
if (e.target.parentElement.classList.contains("remove-item")) {
removeItem(e.target.parentElement.parentElement);
}
setItemtoEdit(e.target);
}
function ifItemExist(item) {
const itemFromStorage = getItemsFromStorage();
return itemFromStorage.includes(item);
}
function setItemtoEdit(item) {
isEditMode = true;
itemList
.querySelectorAll("li")
.forEach((i) => i.classList.remove("edit-mode"));
item.classList.add("edit-mode");
itemInput.value = item.textContent;
formBtn.innerHTML = '<i class="fa-solid fa-pen"></i> Update Item';
formBtn.style.backgroundColor = "#228B22";
}
function removeItem(item) {
if (confirm("Are you sure?")) {
// remove from dom
item.remove();
// remove from local storage
removeItemFromStorage(item.textContent);
checkui();
// Reset button color to default
formBtn.innerHTML = '<i class="fa-solid fa-plus"></i> Add Item';
formBtn.style.backgroundColor = "#333";
}
}
function removeItemFromStorage(item) {
let itemFromStorage = getItemsFromStorage();
//Filter out the item to be removed
itemFromStorage = itemFromStorage.filter((i) => i !== item);
// set items to local storage
localStorage.setItem("listItems", JSON.stringify(itemFromStorage));
}
function filterItem(e) {
const itemText = e.target.value.toLowerCase();
const items = itemList.querySelectorAll("li");
items.forEach((item) => {
const filterParam = item.firstChild.textContent.toLowerCase();
if (filterParam.indexOf(itemText) != -1) {
item.style.display = "flex";
} else {
item.style.display = "none";
}
});
}
function checkui() {
if (itemList.children.length === 0) {
clearBtn.style.display = "none";
filter.style.display = "none";
} else {
clearBtn.style.display = "block";
filter.style.display = "block";
}
itemInput.value = "";
}
// Events
function init() {
itemForm.addEventListener("submit", addItemtostorage);
itemList.addEventListener("click", onClickItem);
clearBtn.addEventListener("click", removeAll);
filter.addEventListener("input", filterItem);
document.addEventListener("DOMContentLoaded", display);
checkui();
}
init();