-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
215 lines (185 loc) · 6.22 KB
/
main.js
File metadata and controls
215 lines (185 loc) · 6.22 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
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
204
205
206
207
208
209
210
211
212
213
214
215
const form = document.querySelector("#add-form");
const todoList = document.querySelector("#todo-list");
const alertText = document.querySelector("#alert");
const search = document.querySelector("#search");
const title = document.querySelector("#title");
const description = document.querySelector("#description");
// ADD ITEMS
//getting it from localStorage
let itemsArray = localStorage.getItem("items")
? JSON.parse(localStorage.getItem("items"))
: [];
//saving it locally
localStorage.setItem("items", JSON.stringify(itemsArray));
let data = JSON.parse(localStorage.getItem("items"));
function addItem(e) {
e.preventDefault();
if (title.value != "" && description.value != "") {
createItem(title.value, description.value);
itemsArray.push([title.value, description.value]);
data.push([title.value, description.value]);
localStorage.setItem("items", JSON.stringify(itemsArray));
alertText.style.display = "none";
} else {
alertText.style.display = "block";
}
title.value = "";
description.value = "";
}
// this is for creating title and description
function createItem(title, description) {
const li = document.createElement("li");
const h3 = document.createElement("h3");
const p = document.createElement("p");
const div = document.createElement("div");
li.classList.add("list-group-item");
h3.appendChild(document.createTextNode(title));
p.appendChild(document.createTextNode(description));
h3.classList.add("item-name");
div.appendChild(h3);
div.appendChild(p);
li.appendChild(div);
createButton(li);
todoList.appendChild(li);
}
// this is for creating complete button, undo button, and delete button
function createButton(li) {
const complete = document.createElement("button");
const remove = document.createElement("button");
const undo = document.createElement("button");
const div = document.createElement("div");
complete.classList.add("complete");
undo.classList.add("undo");
remove.classList.add("remove");
div.classList.add("button-group");
complete.setAttribute("type", "button");
undo.setAttribute("type", "button");
remove.setAttribute("type", "button");
complete.appendChild(document.createTextNode("Complete"));
undo.appendChild(document.createTextNode("Undo"));
remove.appendChild(document.createTextNode("Delete"));
div.appendChild(complete);
div.appendChild(undo);
div.appendChild(remove);
li.appendChild(div);
}
// REMOVE ITEMS
function removeItem(e) {
if (e.target.classList.contains("remove")) {
//this is for taking the li => parentElement of div => parentElement of e.target
const li = e.target.parentElement.parentElement;
const titleName = e.target.parentElement.previousElementSibling.firstChild;
const descriptionName = [
...e.target.parentElement.previousElementSibling.children,
][1];
if (confirm("Are you sure?")) {
todoList.removeChild(li);
}
let newData = JSON.parse(localStorage.getItem("items"));
for (let i of data) {
if (
i[0] === titleName.textContent &&
i[1] === descriptionName.textContent
) {
// console.log("i data", i, data.indexOf(i));
newData.splice(data.indexOf(i), 1);
data = newData;
}
}
localStorage.setItem("items", JSON.stringify(data));
console.log("DATA: ", data);
}
}
// COMPLETED ITEMS
function completeAndUndo(e) {
const li = e.target.parentElement.parentElement;
const titleAndDescription =
e.target.parentElement.previousElementSibling.children;
if (e.target.classList.contains("complete")) {
li.style.backgroundColor = "#ab8d90";
[...titleAndDescription].forEach(
(item) => (item.style.textDecoration = "line-through")
);
e.target.style.display = "none";
e.target.nextElementSibling.style.display = "inline";
}
if (e.target.classList.contains("undo")) {
li.style.backgroundColor = "#f5cdff";
[...titleAndDescription].forEach(
(item) => (item.style.textDecoration = "none")
);
e.target.style.display = "none";
e.target.previousElementSibling.style.display = "inline";
}
}
// SEARCH ITEM
function searchItem(e) {
const itemNames = document.querySelectorAll(".item-name");
const inputText = e.target.value.toLowerCase();
itemNames.forEach((itemName) => {
const itemList = itemName.parentElement.parentElement;
if (itemName.textContent.toLowerCase().indexOf(inputText) != -1) {
itemList.style.display = "flex";
} else {
itemList.style.display = "none";
}
});
}
data.forEach((item) => createItem(item[0], item[1]));
form.addEventListener("submit", addItem);
todoList.addEventListener("click", removeItem);
todoList.addEventListener("click", completeAndUndo);
search.addEventListener("change", searchItem);
// ANOTHER WAY TO DO THE COMPLETE AND UNDO BUTTONS
// function completeAndUndo(e) {
// const undoButtons = document.querySelectorAll(".undo");
// const completeButtons = document.querySelectorAll(".complete");
// completeButtons.forEach((completeButton, i) => {
// completeButton.addEventListener("click", (e) =>
// changeBackground(
// e,
// completeButton,
// i,
// "complete",
// "#ab8d90",
// "line-through",
// undoButtons
// )
// );
// });
// undoButtons.forEach((undoButton, i) => {
// undoButton.addEventListener("click", (e) =>
// changeBackground(
// e,
// undoButton,
// i,
// "undo",
// "#f5cdff",
// "none",
// completeButtons
// )
// );
// });
// }
// function changeBackground(
// e,
// buttonItem,
// i,
// buttonClassName,
// backgroundColor,
// textDecoration,
// buttonLists
// ) {
// const listGroupItems = document.querySelectorAll(".list-group-item");
// if (e.target.classList.contains(buttonClassName)) {
// [...listGroupItems][i].style.backgroundColor = backgroundColor;
// //this is for taking the h3, p => child of first div => sibling of second div => parentElement of e.target
// [...e.target.parentElement.previousElementSibling.children].forEach(
// (item) => (item.style.textDecoration = textDecoration)
// );
// //hide the complete
// buttonItem.style.display = "none";
// //button changed
// [...buttonLists][i].style.display = "inline";
// }
// }