-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
186 lines (148 loc) · 5.55 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
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
class Book {
constructor(title, author, pages, read) {
this.title = title,
this.author = author,
this.pages = pages,
this.read = read
}
toggleRead() {
if (this.read === false) {
this.read = true;
}
else if (this.read === true) {
this.read = false;
}
}
}
let myLibrary = [];
function addBookToLibrary(title, author, pages, read) {
let newBook = new Book(title, author, pages, read);
myLibrary.push(newBook);
}
function displayMyLibrary() {
const booksContainer = document.querySelector("#books-container");
if(myLibrary.length === 0) {
booksContainer.textContent = "No books yet!";
}
else {booksContainer.textContent = ""}
let index = 0;
myLibrary.forEach(bookObj => {
let bookDiv = document.createElement("div");
bookDiv.classList.add("book");
let bookInfoDiv = document.createElement("div");
bookInfoDiv.id = "bookObjInfoDiv";
let bookTitle = document.createElement("p");
bookTitle.textContent = `"${bookObj.title}"`;
bookInfoDiv.appendChild(bookTitle);
let bookAuthor = document.createElement("p");
bookAuthor.textContent = bookObj.author;
bookInfoDiv.appendChild(bookAuthor);
let bookPages = document.createElement("p");
bookPages.textContent = `${bookObj.pages} pages`;
bookInfoDiv.appendChild(bookPages);
bookDiv.appendChild(bookInfoDiv);
let buttonsDiv = document.createElement("div");
buttonsDiv.id = "bookObjButtonsDiv";
let readButton = document.createElement("button");
readButton.textContent = "NOT READ";
readButton.style.backgroundColor = "orange";
readButton.style.border = "none";
readButton.style.borderRadius = "10px";
readButton.style.fontSize = "16px";
let delButton = document.createElement("button");
delButton.textContent = "x";
delButton.id = index;
delButton.style.fontSize = "26px";
delButton.style.border = "none";
delButton.style.borderRadius = "10px";
delButton.style.backgroundColor = "pink";
buttonsDiv.appendChild(readButton);
buttonsDiv.appendChild(delButton);
bookDiv.appendChild(buttonsDiv);
booksContainer.appendChild(bookDiv);
index++;
readButton.addEventListener("click", () => {
bookObj.toggleRead();
if (bookObj.read === false) {
readButton.style.backgroundColor = "orange";
readButton.textContent = "NOT READ";
readButton.style.fontSize = "16px";
}
else if (bookObj.read === true) {
readButton.style.backgroundColor = "#90ee90";
readButton.textContent = "READ";
readButton.style.fontSize = "16px";
}
});
delButton.addEventListener("click", () => {
myLibrary.splice(delButton.id, 1);
displayMyLibrary();
});
})
}
function showForm() {
const body = document.querySelector("body");
let formContainer = document.createElement("form")
formContainer.id = "form-container";
let titleLabelContainer = document.createElement("div");
titleLabelContainer.classList.add("form-labels");
let titleLabel = document.createElement("div");
titleLabel.textContent = "Title:";
let titleInput = document.createElement("input");
titleInput.id = "title-input";
titleInput.required = true;
titleInput.style.margin = "20px 20px 20px 0px";
titleInput.style.textAlign = "center";
titleInput.style.fontSize = "24px";
titleLabelContainer.appendChild(titleLabel);
formContainer.appendChild(titleLabelContainer);
formContainer.appendChild(titleInput);
let authorLabelContainer = document.createElement("div");
authorLabelContainer.classList.add("form-labels");
let authorLabel = document.createElement("div");
authorLabel.textContent = "Author:";
let authorInput = document.createElement("input");
authorInput.required = true;
authorInput.style.margin = "20px 20px 20px 0px";
authorInput.style.textAlign = "center";
authorInput.style.fontSize = "24px";
authorLabelContainer.appendChild(authorLabel);
formContainer.appendChild(authorLabelContainer);
formContainer.appendChild(authorInput);
let pagesLabelContainer = document.createElement("div");
pagesLabelContainer.classList.add("form-labels");
let pagesLabel = document.createElement("div");
pagesLabel.textContent = "Pages:";
let pagesInput = document.createElement("input");
pagesInput.setAttribute("type", "number")
pagesInput.required = true;
pagesInput.style.margin = "20px 20px 20px 0px";
pagesInput.style.textAlign = "center";
pagesInput.style.fontSize = "24px";
pagesLabelContainer.appendChild(pagesLabel);
formContainer.appendChild(pagesLabelContainer);
formContainer.appendChild(pagesInput);
let cancelButton = document.createElement("button");
cancelButton.style.backgroundColor = "pink";
cancelButton.style.border = "none";
cancelButton.textContent = "Cancel";
let submitButton = document.createElement("button");
submitButton.setAttribute("type", "submit");
submitButton.style.backgroundColor = "lightgreen";
submitButton.style.border = "none";
submitButton.textContent = "Add";
formContainer.appendChild(cancelButton);
formContainer.appendChild(submitButton);
body.appendChild(formContainer);
cancelButton.addEventListener("click", event => {
body.removeChild(formContainer);
});
formContainer.addEventListener("submit", () => {
addBookToLibrary(titleInput.value, authorInput.value, pagesInput.value, false);
body.removeChild(formContainer);
displayMyLibrary();
});
}
const addBookButton = document.querySelector("#add-book-button");
addBookButton.addEventListener("click", () => {showForm()});
displayMyLibrary();