-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
279 lines (245 loc) · 10.1 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
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
276
277
278
279
// Default books
function Book(title, author, quote, read) {
this.title = title;
this.author = author;
this.quote = quote;
this.read = read;
}
// const book1 = new Book(
// "Island of the Blue Dolphins",
// "Scott O'Dell",
// "TBD",
// true,
// );
const book2 = new Book(
"Braiding Sweetgrass",
"Robin Wall-Kimmerer",
"Action on behalf of life transforms. Because the relationship between self and the world is reciprocal, it is not a question of first getting enlightened or saved and then acting. As we work to heal the earth, the earth heals us.",
true
);
const book3 = new Book(
"Snow Crash",
"Neal Stephenson",
"See, the world is full of things more powerful than us. But if you know how to catch a ride, you can go places,",
"true"
);
// , They were trying to save their souls- and who but a fool could fail to see that all that was the matter with their souls was that they had not been able to get a decent existence for their bodies?
const book1 = new Book(
"The Jungle",
"Upton Sinclair",
"There is one kind of prison where the man is behind bars, and everything that he desires is outside; and there is another kind where the things are behind the bars, and the man is outside.",
true
);
// Dune, Hitchhiker's guide, our mathematical universe,
// nathaniel hawthorne (birthmark),
// Brave New World,
// "A Clockwork Orange", "Anthony Burgess", "Goodness is something chosen. When a man cannot choose he ceases to be a man."
// "Candide", "Voltaire",
// "Atomic Habits", "James Clear",
const surprise1 = {
title: "The Andromeda Strain ",
author: "Michael Crichton",
quote:
"The rock, for its part, is not even aware of our existence because we are alive for only a brief instant of its lifespan. To it, we are like flashes in the dark.",
read: false,
};
const surprise2 = new Book(
"Flatland, A Romance of Many Dimensions",
"Edwin A. Abbott",
"Distress not yourself if you cannot at first understand the deeper mysteries of Spaceland. By degrees they will dawn upon you.",
"true"
);
const surprise3 = new Book(
"The Metamorphosis",
"Franz Kafka",
"Was he an animal, that music could move him so? He felt as if the way to the unknown nourishment he longed for were coming to light.",
true
);
// , Where you tend a rose my lad, a thistle cannot grow.
const surprise4 = new Book(
"The Secret Garden",
"Frances Hodgson Burnett",
"At first people refuse to believe that a strange new thing can be done, then they begin to hope it can be done, then they see it can be done--then it is done and all the world wonders why it was not done centuries ago.",
"true"
);
// Candide
//"Let us cultivate our garden."
// Secret griefs are more cruel than public calamities.,
// It is demonstrable," said he, "that things cannot be otherwise than as they are; for as all things have been created for some end, they must necessarily be created for the best end. Observe, for instance, the nose is formed for spectacles, therefore we wear spectacles. The legs are visibly designed for stockings, accordingly we wear stockings. Stones were made to be hewn and to construct castles, therefore My Lord has a magnificent castle; for the greatest baron in the province ought to be the best lodged. Swine were intended to be eaten, therefore we eat pork all the year round: and they, who assert that everything is right, do not express themselves correctly; they should say that everything is best.",
const surprise5 = new Book(
"Candide",
"Voltaire",
`It is demonstrable," said he, "that things cannot be otherwise than as they are; for as all things have been created for some end, they must necessarily be created for the best end. Observe, for instance, the nose is formed for spectacles, therefore we wear spectacles.
The legs are visibly designed for stockings, accordingly we wear stockings.
Stones were made to be hewn and to construct castles...
and they, who assert that everything is right, do not express themselves correctly;
they should say that everything is best.`,
"true"
);
const mySurpriseLibrary = [
surprise1,
surprise2,
surprise3,
surprise4,
surprise5,
];
const myLibrary = [];
const bookshelf = document.querySelector(".bookshelf");
function addBook(newBook) {
myLibrary.push(newBook);
}
function addSurprise() {
let surpriseBook;
console.log("length", mySurpriseLibrary.length);
if (mySurpriseLibrary.length > 0) {
let surpriseNum = Math.floor(Math.random() * mySurpriseLibrary.length);
surpriseBook = mySurpriseLibrary[surpriseNum];
console.log(surpriseBook.title);
mySurpriseLibrary.splice(surpriseNum, 1);
console.log(mySurpriseLibrary);
} else {
surpriseBook = null; //No more suprises =,[
console.log("No more surprises left =,[");
}
return surpriseBook;
}
// function deleteBook(book) {
// myLibrary.pop(book);
// //are you sure? modal
// }
myLibrary.push(book1, book2, book3);
// console.log(myLibrary)
// display book
function displayBook(bookNum) {
let book = document.createElement("div");
let bookTitle = document.createElement("div");
let bookAuthor = document.createElement("div");
let bookQuote = document.createElement("div");
let bookRead = document.createElement("button");
let deleteBookBtn = document.createElement("button");
let modal = document.createElement("div");
// let closeModal = document.createElement("span");
let modalText = document.createElement("div");
let confirmDeleteBtn = document.createElement("button");
let cancelModalBtn = document.createElement("button");
// book.setAttribute("class", "test");
book.setAttribute("class", "book");
book.setAttribute("id", bookNum);
bookTitle.setAttribute("class", "title");
bookAuthor.setAttribute("class", "author");
bookQuote.setAttribute("class", "quote");
bookRead.setAttribute("class", "read");
// deleteBookBtn.setAttribute("class", "deleteBookBtn fa fa-trash");
deleteBookBtn.setAttribute("class", "deleteBookBtn material-symbols-outlined");
modal.setAttribute("class", "modal");
// closeModal.setAttribute("class", "closeModal");
modalText.setAttribute("class", "modalText");
confirmDeleteBtn.setAttribute("class", "confirmDeleteBtn");
cancelModalBtn.setAttribute("class", "cancelModalBtn");
bookshelf.prepend(book); // make new book the first one displayed after the input form
book.appendChild(bookTitle);
book.appendChild(bookAuthor);
book.appendChild(bookQuote);
book.appendChild(bookRead);
book.appendChild(deleteBookBtn);
book.appendChild(modal);
// modal.appendChild(closeModal);
modal.appendChild(modalText);
modal.appendChild(cancelModalBtn);
modal.appendChild(confirmDeleteBtn);
bookTitle.innerText = myLibrary[bookNum].title;
bookAuthor.innerText = myLibrary[bookNum].author;
bookQuote.innerText = myLibrary[bookNum].quote;
bookRead.innerText = myLibrary[bookNum].read;
deleteBookBtn.innerText = "delete";
// modal.innerText = "modal"; //this overwrites appended children
modal.style.display = "none";
// closeModal.innerText = "x";
modalText.innerText = "Are you sure you want to delete this book?";
cancelModalBtn.innerText = "CANCEL";
confirmDeleteBtn.innerText = "DELETE";
bookRead.addEventListener("click", (event) => {
// true/false was converted to string
if (bookRead.innerText == "false") {
bookRead.innerText = "true";
} else {
bookRead.innerText = "false";
}
});
// closeModal.innerText = "x";
cancelModalBtn.addEventListener("click", (event) => {
// hide modal
console.log("event.target.parentNode", event.target.parentNode);
event.target.parentNode.style.display = "none";
// display delete button again
console.log(
"find deleteBook btn",
event.target.parentNode.parentNode.querySelector(".deleteBookBtn")
);
event.target.parentNode.parentNode.querySelector(
".deleteBookBtn"
).style.display = "inline-block";
});
confirmDeleteBtn.addEventListener("click", (event) => {
console.log(
"event.target.parentNode.parentNode.id",
event.target.parentNode.parentNode.id
);
event.target.parentNode.parentNode.style.display = "none";
myLibrary.splice(`${event.target.parentNode.id}, 1`);
});
deleteBookBtn.addEventListener("click", (event) => {
// hide the deleteBookBtn element
event.target.style.display = "none";
// display the modal element
console.log(
"event.target.parentNode.lastChild",
event.target.parentNode.lastChild
);
event.target.parentNode.lastChild.style.display = "block";
});
// When the user clicks anywhere outside of the modal, close it
// window.onclick = function(event) {
// if (event.target != modal && modal.style.display != "none") {
// modal.style.display = "none";
// }
// }
}
// load existing books when window opens
window.addEventListener("load", () => {
console.log("page load to display existing library books");
for (i = 0; i < myLibrary.length; i++) {
displayBook(i);
}
});
// make new book
function makeBook() {
let newTitle = document.getElementById("newTitle").value;
let newAuthor = document.getElementById("newAuthor").value;
let newQuote = document.getElementById("newQuote").value;
let newRead = document.getElementById("newRead").value;
let newBook = new Book(newTitle, newAuthor, newQuote, newRead);
return newBook;
}
// enable add book button
const addBookBtn = document.querySelector(".addBookBtn");
addBookBtn.addEventListener("click", (e) => {
e.preventDefault(); // prevent submit button from reloading pg bc no data is actually getting back to server
// preventDefault seems to mess up required property
// add check to prevent repeat books, bookExistsCheck()
addBook(makeBook());
displayBook(myLibrary.length - 1);
document.getElementById("bookForm").reset();
});
// surpriseMe button
const surpriseMeBtn = document.querySelector(".surpriseMeBtn");
surpriseMeBtn.addEventListener("click", () => {
let surpriseBook = addSurprise();
if (surpriseBook != null) {
//probably not the most error proof way to do this
addBook(surpriseBook);
displayBook(myLibrary.length - 1);
} else {
alert("No more surprises =,[");
}
});