-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
114 lines (97 loc) · 2.93 KB
/
script.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
//Select DOM
const bookGrid = document.querySelector(".book-grid");
const form = document.querySelector(".formContainer");
let checkboxForm = document.getElementById("read");
//Create empty library array
let myLibrary = [];
//Object constructor
class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
}
//Old contructor
// function Book(title, author, pages, read){
// this.title = title;
// this.author = author;
// this.pages = pages;
// this.read = read;
// }
function addBookToLibrary() {
form.addEventListener("submit", (e) => {
e.preventDefault();
myLibrary.unshift(
new Book(title.value, author.value, pages.value, checkboxForm.checked)
);
closeTheForm();
displayBooks();
form.reset();
});
}
addBookToLibrary();
function displayBooks() {
bookGrid.innerHTML = "";
for (let i = 0; i < myLibrary.length; i++) {
createBookCard(myLibrary[i]);
}
}
function createBookCard(book) {
const element = document.createElement("div");
element.classList.add("bookCard", "bookCard");
const removeCard = document.createElement("button");
removeCard.classList.add("btn", "btnRemove");
element.appendChild(removeCard);
const closeIcon = document.createElement("span");
closeIcon.classList.add("material-symbols-outlined");
closeIcon.textContent = "close";
removeCard.appendChild(closeIcon);
const title = document.createElement("h2");
title.className = "title";
title.textContent = book.title;
element.appendChild(title);
const author = document.createElement("h3");
author.className = "author";
author.textContent = `by ${book.author}`;
element.appendChild(author);
const pages = document.createElement("h3");
pages.className = "pages";
pages.textContent = `${book.pages} pages`;
element.appendChild(pages);
const readStatusBox = document.createElement("div");
readStatusBox.className = "readStatusBox";
const readStatus = document.createElement("input");
readStatus.setAttribute("id", "readDisplay");
readStatus.setAttribute("type", "checkbox");
readStatus.checked = book.read;
const readLabel = document.createElement("label");
readLabel.setAttribute("for", "readDisplay");
readLabel.textContent = "Finished reading";
element.appendChild(readStatusBox);
readStatusBox.appendChild(readStatus);
readStatusBox.appendChild(readLabel);
bookGrid.appendChild(element);
removeCard.addEventListener("click", function () {
let index = myLibrary.indexOf(book);
myLibrary.splice(index, 1);
element.remove();
displayEmpty();
});
}
function openTheForm() {
document.getElementById("popupForm").style.display = "block";
}
function closeTheForm() {
document.getElementById("popupForm").style.display = "none";
displayEmpty();
}
function displayEmpty() {
if (myLibrary.length === 0) {
document.getElementById("empty").style.display = "block";
} else {
document.getElementById("empty").style.display = "none";
}
}
displayEmpty();