-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
155 lines (133 loc) · 4.13 KB
/
script.js
File metadata and controls
155 lines (133 loc) · 4.13 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
let myLibrary = [
{ title: "Harry Potter", author: "JK Rowling", pages: 3000, isRead: true },
{
title: "Lord of the Rings",
author: "J.R.R. Tolkien",
pages: 500,
isRead: true,
},
];
let btnOpen = document.querySelector(".newBook");
let btnClose = document.querySelector(".cancel");
let form = document.querySelector(".formPopup");
let container = document.querySelector(".container");
let btnSubmit = document.querySelector(".submit");
let titleInput = document.querySelector("#title");
let authorInput = document.querySelector("#author");
let pagesInput = document.querySelector("#pages");
let isReadInput = document.querySelector("#read");
// let header = document.querySelector('.header');
// header.addEventListener('mouseenter', function(e){
// header.style.color = "#" + Math.floor(Math.random()*16777215).toString(16);
// })
btnOpen.addEventListener("click", popupWindow);
function popupWindow() {
form.style.display = "block";
}
btnClose.addEventListener("click", () => (form.style.display = "none"));
btnSubmit.addEventListener("click", function () {
if (validateForm()) {
addBookToLibrary(
titleInput.value,
authorInput.value,
pagesInput.value,
isReadInput.checked
);
form.style.display = "none";
}
});
myLibrary.forEach((book) => {
displayBook(book);
});
function validateForm() {
if (titleInput.validity.valueMissing) {
titleInput.setCustomValidity("Enter a Title");
titleInput.reportValidity();
return false;
} else if (authorInput.validity.valueMissing) {
authorInput.setCustomValidity("Enter an Author");
authorInput.reportValidity();
return false;
} else if (pagesInput.validity.valueMissing) {
pagesInput.setCustomValidity("Enter # of Pages");
pagesInput.reportValidity();
return false;
} else {
return true;
}
}
function displayBook(book) {
let card = document.createElement("div");
let titleText = document.createElement("h3");
let authorText = document.createElement("p");
let pagesText = document.createElement("p");
let readButton = document.createElement("button");
let delButton = document.createElement("input");
let box = document.createElement("div");
let span = document.createElement("span");
card.id = book.title;
box.className = "box";
delButton.type = "image";
delButton.src = "../Library/red-cross.png";
delButton.width = "50";
delButton.height = "50";
delButton.addEventListener("click", () => {
deleteBook(book);
});
span.textContent = book.title;
titleText.textContent = "Title: ";
titleText.appendChild(span);
authorText.textContent = "Author: " + book.author;
pagesText.textContent = "Pages: " + book.pages;
if (book.isRead) {
readButton.className = "btn-status btn-read";
readButton.textContent = "READ!";
card.className = "card read";
} else {
readButton.className = "btn-status btn-notread";
readButton.textContent = "NOT READ!";
card.className = "card not-read";
}
readButton.addEventListener("click", () => {
toggleButton(readButton, book, card);
});
box.append(readButton, delButton);
card.append(titleText, authorText, pagesText, box);
container.appendChild(card);
}
function toggleButton(button, book, card) {
if (button.className.includes("btn-read")) {
button.className = "btn-status btn-notread";
button.textContent = "NOT READ!";
book.isRead = false;
card.className = "card not-read";
} else {
button.className = "btn-status btn-read";
button.textContent = "READ!";
book.isRead = true;
card.className = "card read";
}
}
function deleteBook(book) {
container.removeChild(document.getElementById(book.title));
}
function Book(title, author, pages, isRead) {
this.title = title;
this.author = author;
this.pages = pages;
this.isRead = isRead;
}
function addBookToLibrary(title, author, pages, isRead) {
let exists = false;
myLibrary.forEach((book) => {
if (book.title === title) {
alert("THAT BOOK ALREADY EXISTS");
exists = true;
}
});
if (exists == false) {
let newbook = new Book(title, author, pages, isRead);
myLibrary.push(newbook);
displayBook(newbook);
}
}