-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
275 lines (254 loc) · 8.24 KB
/
library.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const darkThemeBtn = document.querySelector(".dark-toggle");
const lightThemeBtn = document.querySelector(".light-toggle");
const deleteAllBtn = document.querySelector(".delete-all-btn");
const displayFormBtn = document.querySelector(".display-popup-button");
const form = document.querySelector("form");
const formSection = document.querySelector(".form-section");
const booksContainer = document.querySelector(".grid");
const hideFormBtn = document.querySelector(".remove-form");
const addBookBtn = document.querySelector(".add-book-btn");
const clearFormBtn = document.querySelector(".clear-form-btn");
let myLibrary = [
{
title: "The Hunger Games: Catching Fire",
author: "Suzanne Collins",
pageCount: 439,
language: "English",
date: "1 9 2009",
read: true,
},
{
title: "Harry Potter And The Order Of The Phoenix",
author: "J.K. Rowling",
pageCount: 802,
language: "English",
date: "21 6 2003",
read: true,
},
{
title: "A Game Of Thrones",
author: "George R.R. Martin",
pageCount: 694,
language: "English",
date: "1 7 1996",
read: true,
},
];
let switchNumber = 1;
let totalBooksNumber = 0;
let booksRead = 0;
let booksNotRead = 0;
darkThemeBtn.addEventListener("click", () => {
document.body.classList.add("dark-mode");
document.body.classList.remove("light-mode");
});
lightThemeBtn.addEventListener("click", () => {
document.body.classList.add("light-mode");
document.body.classList.remove("dark-mode");
});
function useCheckboxFunctions() {
const checkboxes = document.querySelectorAll(".reading-status-checkbox");
checkboxes.forEach((checkbox) => {
checkbox.addEventListener("click", (e) => {
toggleColor(checkbox);
displayReadingStatus(e.target);
changeReadCount(e.target);
displayReadingCount();
});
});
}
function deleteBook() {
const deleteBookBtn = Array.from(document.querySelectorAll(".delete-book"));
deleteBookBtn.forEach((button) =>
button.addEventListener("click", () => {
button.parentElement.remove();
totalBooksNumber -= 1;
if (button.parentElement.lastElementChild.children[1].checked) {
booksRead -= 1;
} else {
booksNotRead -= 1;
}
for (let i in myLibrary) {
if (
button.parentElement.querySelector(".author-name").textContent ===
myLibrary[i].author
) {
const index = myLibrary.indexOf(myLibrary[i]);
myLibrary.splice(index, 1);
}
}
displayReadingCount();
})
);
}
deleteAllBtn.addEventListener("click", deleteAllBooks);
displayFormBtn.addEventListener("click", () => {
formSection.style.display = "grid";
});
hideFormBtn.addEventListener("click", () => {
formSection.style.display = "none";
});
form.addEventListener("submit", (e) => {
e.preventDefault();
addBook();
useCheckboxFunctions();
deleteBook();
clearForm();
formSection.style.display = "none";
});
clearFormBtn.addEventListener("click", clearForm);
function toggleColor(checkbox) {
if (checkbox.checked) {
checkbox.parentElement.parentElement.style.backgroundImage =
"linear-gradient(-45deg, #209b87, #80cf7f)";
} else {
checkbox.parentElement.parentElement.style.backgroundImage =
"linear-gradient(135deg, #e3e3e3, #5d6874)";
}
}
function displayReadingStatus(checkbox) {
if (checkbox.checked) {
checkbox.previousElementSibling.textContent = "Read";
} else {
checkbox.previousElementSibling.textContent = "Not Read Yet";
}
}
function changeReadCount(checkbox) {
if (checkbox.checked) {
booksRead += 1;
booksNotRead !== 0 ? (booksNotRead -= 1) : booksNotRead;
} else {
booksNotRead += 1;
booksRead !== 0 ? (booksRead -= 1) : booksRead;
}
}
function displayReadingCount() {
const totalBooks = document.querySelector(".total-books");
const readBooksDisplay = document.querySelector(".read-books-number");
const unreadBooksDisplay = document.querySelector(".unread-books-number");
readBooksDisplay.textContent = booksRead;
unreadBooksDisplay.textContent = booksNotRead;
totalBooks.textContent = totalBooksNumber;
}
function deleteAllBooks() {
const main = document.querySelector("main");
let shouldDelete = confirm("Do you really want to delete all the books");
if (shouldDelete) {
while (booksContainer.lastElementChild) {
booksContainer.removeChild(booksContainer.lastElementChild);
}
deleteAllBtn.disabled = true;
deleteAllBtn.classList.add("disabled");
totalBooksNumber = 0;
booksRead = 0;
booksNotRead = 0;
displayReadingCount();
}
}
function clearForm() {
form.reset();
}
class Book {
constructor(title, author, pageCount, language, publishingDate, read) {
this.title = title;
this.author = author;
this.pageCount = pageCount;
this.language = language;
this.date = publishingDate;
this.read = read;
}
}
function addBook() {
totalBooksNumber = 0;
booksNotRead = 0;
booksRead = 0;
let bookTitle = document.querySelector("#title").value;
let bookAuthor = document.querySelector("#author").value;
let numberOfPages = document.querySelector("#page-number").value;
let language = document.querySelector("#language").value;
let publishingDateInput = document.querySelector("#publishing-date");
let publishingDate = publishingDateInput.value.split("-").reverse().join(" ");
let readingStatus = document.querySelector("#reading-status").checked;
let newBookObj = new Book(
bookTitle,
bookAuthor,
numberOfPages,
language,
publishingDate,
readingStatus
);
myLibrary.push(newBookObj);
displayBook();
displayReadingCount();
}
function displayBook() {
while (booksContainer.firstChild) {
booksContainer.removeChild(booksContainer.firstChild);
}
for (let i in myLibrary) {
const book = document.createElement("div");
const deleteButton = document.createElement("span");
const bookTitle = document.createElement("h2");
const author = document.createElement("p");
const authorName = document.createElement("span");
const pages = document.createElement("p");
const pageNumberDisplay = document.createElement("span");
const lang = document.createElement("p");
const langDisplay = document.createElement("span");
const published = document.createElement("p");
const publishedDate = document.createElement("span");
const toggle = document.createElement("div");
const readingStatusDisplay = document.createElement("span");
const checkbox = document.createElement("input");
const label = document.createElement("label");
book.classList.add("grid-item");
deleteButton.classList.add("delete-btn", "delete-book");
toggle.classList.add("reading-toggle");
readingStatusDisplay.classList.add("reading-status-display");
checkbox.classList.add("reading-status-checkbox");
label.classList.add("checkbox-label");
authorName.classList.add("author-name");
checkbox.setAttribute("type", "checkbox");
checkbox.setAttribute("id", `switch${switchNumber}`);
label.setAttribute("for", checkbox.getAttribute("id"));
switchNumber += 1;
deleteButton.textContent = "+";
bookTitle.textContent = myLibrary[i].title;
author.textContent = "By: ";
authorName.textContent = myLibrary[i].author;
pages.textContent = "Number of pages: ";
pageNumberDisplay.textContent = myLibrary[i].pageCount;
lang.textContent = "Language: ";
langDisplay.textContent = myLibrary[i].language;
published.textContent = "Published: ";
publishedDate.textContent = myLibrary[i].date;
readingStatusDisplay.textContent = "Read";
if (myLibrary[i].read) {
checkbox.setAttribute("checked", "");
} else {
checkbox.setAttribute("unchecked", "");
}
checkbox.checked ? (booksRead += 1) : (booksNotRead += 1);
author.appendChild(authorName);
pages.appendChild(pageNumberDisplay);
lang.appendChild(langDisplay);
published.appendChild(publishedDate);
toggle.append(readingStatusDisplay, checkbox, label);
book.appendChild(toggle);
book.append(
deleteButton,
bookTitle,
author,
pages,
lang,
published,
toggle
);
booksContainer.appendChild(book);
toggleColor(checkbox);
totalBooksNumber += 1;
}
}
displayBook();
useCheckboxFunctions();
deleteBook();