-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
224 lines (190 loc) · 5.99 KB
/
app.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const container = document.querySelector(".container"); //grid container.
const form = document.querySelector("form");
const inputs = document.querySelectorAll("input");
const submit = document.querySelector(".submit");
const edit = document.querySelector(".edit");
const inProgressRadio = document.getElementById("in_progress");
const fileInput = document.getElementById("cover");
const figures = document.querySelectorAll("figure");
let delButtons = document.getElementsByClassName("fa-trash-can");
let selectedImg = "template";
let currentBook;
let myLibrary = [];
function Book(title, author, pagesProgress, pagesTotal, read, cover) {
this.title = title;
this.author = author;
this.pagesProgress = pagesProgress;
this.pagesTotal = pagesTotal;
this.read = read;
this.cover = cover;
}
fileInput.onchange = () => {
selectedImg = fileInput.files[0];
};
function addBookToLibrary() {
const title = document.getElementById("title_input").value;
const author = document.getElementById("author_input").value;
const pagesProgress = document.getElementById("pages_progress_input").value;
const pagesTotal = document.getElementById("pages_total_input").value;
const read = document.querySelector('input[name="read"]:checked').value;
const newBook = new Book(
title,
author,
pagesProgress,
pagesTotal,
read,
selectedImg
);
showBook(newBook);
moveAddButton();
}
function showBook(book) {
const cardContainer = document.createElement("figure");
const image = document.createElement("img");
const figcaption = document.createElement("figcaption");
const ul = document.createElement("ul");
const buttonContainer = document.createElement("div");
const delButton = document.createElement("button");
const editButton = document.createElement("button");
if (book.cover != "template") {
image.setAttribute("src", URL.createObjectURL(book.cover));
} else image.setAttribute("src", "./images/coverTemplate.jpg");
delButton.classList.add("fa-solid", "fa-trash-can");
editButton.classList.add("fa-sharp", "fa-solid", "fa-pen-to-square");
buttonContainer.classList.add("buttonContainer");
for (const key in book) {
let card = document.createElement("li");
switch (key) {
case "title":
card.innerHTML = book.title;
card.classList.add("title");
break;
case "author":
card.innerHTML = book.author;
card.classList.add("author");
break;
case "pagesProgress":
if (
book.read === "In Progress" &&
book.pagesProgress <= book.pagesTotal
) {
card.innerHTML =
book.read + `(${book.pagesProgress}/${book.pagesTotal})`;
} else card.innerHTML = book.read;
card.classList.add("status_display");
break;
}
ul.appendChild(card);
}
figcaption.appendChild(ul);
cardContainer.appendChild(image);
cardContainer.appendChild(figcaption);
buttonContainer.appendChild(editButton);
buttonContainer.appendChild(delButton);
cardContainer.appendChild(buttonContainer);
container.appendChild(cardContainer); //displays book on the page.
myLibrary.push(cardContainer); //adds book to the array.
}
function editForm(book) {
const pagesProgress = document.getElementById(
"pages_progress_input_edit"
).value;
const pagesTotal = document.getElementById("pages_total_input_edit").value;
const read = document.querySelector('input[name="read_edit"]:checked').value;
const child = book.childNodes;
const fig = child[3];
const figChild = fig.childNodes;
const ul = figChild[1];
const ulChild = ul.childNodes;
const readStatus = ulChild[5];
if (book.read === "In Progress" && pagesProgress <= pagesTotal) {
readStatus.innerHTML = `${read} (${pagesProgress}/${pagesTotal})`;
} else readStatus.innerHTML = read;
}
function moveAddButton() {
const addBookFig = myLibrary.at(-2);
myLibrary.splice(-2, 1);
myLibrary.push(addBookFig);
container.appendChild(addBookFig);
}
function openForm() {
const popUp = document.getElementById("add");
popUp.classList.add("active");
}
function closeForm() {
const popUp = document.getElementById("add");
popUp.classList.remove("active");
}
function clearInputs() {
inputs.forEach((input) => (input.value = ""));
}
function openEditForm() {
const popUp = document.getElementById("editForm");
popUp.classList.add("active");
}
function closeEditForm() {
const popUp = document.getElementById("editForm");
popUp.classList.remove("active");
}
function updateIndex() {
for (let i = 0; i < myLibrary.length; i++) {
myLibrary[i].dataset.index = i;
}
}
function deleteElement(index) {
myLibrary[index].remove();
myLibrary.splice(index, 1);
}
function validate() {
let isValid = true;
const inputs = Array.from(form.elements);
const formInputs = inputs.filter((input) => input.hasAttribute("required"));
formInputs.forEach((input) => {
if (!input.checkValidity()) {
input.classList.add("invalid");
input.reportValidity();
isValid = false;
} else {
input.classList.remove("invalid");
input.reportValidity();
}
});
return isValid;
}
submit.addEventListener("click", function (event) {
event.preventDefault();
if (validate() == true) {
addBookToLibrary();
updateIndex();
closeForm();
clearInputs();
}
});
edit.addEventListener("click", function (event) {
event.preventDefault();
editForm(currentBook);
closeEditForm();
clearInputs();
});
document.addEventListener("click", function (e) {
const target = e.target.closest(".fa-trash-can");
if (target) {
const bookContainer = target.parentNode.parentNode;
deleteElement(bookContainer.dataset.index);
updateIndex();
}
});
document.addEventListener("click", function (e) {
const target = e.target.closest(".fa-pen-to-square");
if (target) {
openEditForm();
const book = target.parentNode.parentNode;
currentBook = book;
}
});
window.onload = (event) => {
figures.forEach((figure) => {
myLibrary.push(figure);
});
updateIndex();
};