-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (126 loc) · 3.76 KB
/
Copy pathscript.js
File metadata and controls
140 lines (126 loc) · 3.76 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
const clear = document.querySelector("#clear");
const uList = document.querySelector("#items");
const filter = document.querySelector("#filter");
const form = document.querySelector("#form");
const input = document.querySelector("#input");
//clear button Activate
function onClear() {
confirm("are you sure?");
uList.innerHTML = ``;
localStorage.clear();
check();
}
clear.addEventListener("click", onClear);
//remove each item by click the x icon
function onClickItem(e) {
if (e.target.parentElement.classList.contains("remove-item")) {
removeItem(e.target.parentElement.parentElement);
}
}
function removeItem(item) {
// remove from dom
item.remove();
//remove from LocalStorage
removeItemFromStorage(item.textContent);
check();
}
function removeItemFromStorage(item) {
let itemsFromStorage = fromStorageToDom();
// filter to remove
itemsFromStorage = itemsFromStorage.filter((i) => i !== item);
//reset to localstorage
localStorage.setItem("items", JSON.stringify(itemsFromStorage));
check();
}
uList.addEventListener("click", onClickItem);
// ---------------add item when submit-------------------
function onSubmit(e) {
const value = input.value;
e.preventDefault();
if (input.value == ``) {
alert("Enter a Task first");
return;
}
addToTheDom(value);
addToStorage(value);
check();
}
//------------------add To The DOM-------------------
function addToTheDom(item) {
const li = document.createElement("li");
li.appendChild(document.createTextNode(item));
// add button
const button = createButton("remove-item btn-link text-red");
// append icon to button
const icon = createIcon("fa-solid fa-xmark");
button.appendChild(icon);
// append to to li
li.appendChild(button);
// append li to ul
uList.appendChild(li);
input.value = ""; // clear input
}
//-----------------add to the LocalSTORAGE-------------------
function addToStorage(item) {
const itemsFromStorage = fromStorageToDom();
itemsFromStorage.push(item);
// convert to json n stringify----------
localStorage.setItem("items", JSON.stringify(itemsFromStorage));
}
// ---------FROM--STORAGE-------TO---DOM ---------------
function fromStorageToDom() {
let itemsFromStorage;
if (localStorage.getItem("items") === null) {
itemsFromStorage = [];
} else {
itemsFromStorage = JSON.parse(localStorage.getItem("items"));
}
return itemsFromStorage;
}
function storageToDom(item) {
const itemsFromStorage = fromStorageToDom();
itemsFromStorage.forEach((item) => addToTheDom(item));
check();
}
window.addEventListener("DOMContentLoaded", storageToDom);
// ---------------create button and Icon
function createButton(classes) {
const button = document.createElement("button");
button.classList = classes;
return button;
}
function createIcon(classes) {
const icon = document.createElement("i");
icon.classList = classes;
return icon;
}
form.addEventListener("submit", onSubmit);
// filter function
function onFilter(e) {
const li = uList.querySelectorAll("li");
const text = e.target.value.toLowerCase();
li.forEach((item) => {
const taskName = item.firstChild.textContent.toLocaleLowerCase();
console.log(taskName);
if (taskName.indexOf(text) != -1) {
item.style.display = "flex";
} else {
item.style.display = "none";
}
});
check();
}
filter.addEventListener("input", onFilter);
//--------check if there is no tasks delete clear n filter----
function check() {
const li = uList.querySelectorAll("li");
if (li.length == 0) {
clear.style.display = "none";
filter.style.display = "none";
filter.value = "";
} else {
clear.style.display = "block";
filter.style.display = "block";
}
}
check();