-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
376 lines (282 loc) · 11 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
function generateId() {
if (myLibrary.length === 0) {
return 1;
}
else {
let max = 0;
myLibrary.forEach(book => max = book.id > max ? book.id : max)
return max+1;
}
}
class Book {
constructor(title, author, pages, pagesRead, isRead, url = '') {
this.id = generateId();
console.log(this.id);
this.title = title;
this.author = author;
this.pages = pages;
this.pagesRead = pagesRead;
this.isRead = isRead;
this.url = url
}
}
function createImgDiv(book) {
let imgDiv = document.createElement('div');
imgDiv.classList.add('img-container');
img = document.createElement('img');
img.classList.add('book-img');
if (book.url) {
url = book.url
}
else {
title = book.title;
terms = String(title.split(' '));
url = `https://source.unsplash.com/500x250?${terms}`;
}
img.src = url;
imgDiv.appendChild(img);
return imgDiv;
}
function createMenuDiv(book) {
const menuDiv = document.createElement('div')
menuDiv.classList.add('book-menu')
const addButton = document.createElement('span')
addButton.innerText = "add"
addButton.classList.add("material-icons", "book-button", 'modifier', 'add')
const subtractButton = document.createElement('span')
subtractButton.innerText = "remove"
subtractButton.classList.add("material-icons", "book-button", 'modifier' ,'subtract')
const pagesReadDiv = document.createElement('div')
pagesReadDiv.innerText = `${book.pagesRead} pages read`
pagesReadDiv.classList.add('pages-read')
menuDiv.appendChild(subtractButton)
menuDiv.appendChild(pagesReadDiv)
menuDiv.appendChild(addButton)
return menuDiv
}
function createContentDiv(book) {
const contentDiv = document.createElement('div');
contentDiv.classList.add('book-content');
const titleContent = document.createElement('h3');
titleContent.innerText=book.title;
titleContent.classList.add('title');
const authorContent = document.createElement('p');
authorContent.innerText = `by ${book.author}`;
authorContent.classList.add('author', 'sub-book-content');
const pagesContent = document.createElement('p');
pagesContent.innerText = `${book.pages} pages`;
pagesContent.classList.add('pages', 'sub-book-content');
contentDiv.appendChild(titleContent);
contentDiv.appendChild(authorContent);
contentDiv.appendChild(pagesContent);
return contentDiv;
}
function createFooterDiv() {
const footerDiv = document.createElement('div')
footerDiv.classList.add('book-footer')
const progressBar = document.createElement('progress')
const progressBarDiv = document.createElement('div')
const editButton = document.createElement('span')
const deleteButton = document.createElement('span')
progressBarDiv.classList.add('progress-bar')
progressBarDiv.appendChild(progressBar)
editButton.innerText= 'edit';
editButton.classList.add('book-button','edit-button', 'material-icons');
deleteButton.innerText = 'delete';
deleteButton.classList.add('book-button','delete-button', 'material-icons')
footerDiv.appendChild(progressBarDiv);
footerDiv.appendChild(editButton);
footerDiv.appendChild(deleteButton);
return footerDiv;
}
function submitForm() {
document.querySelector('.add-book').style.display='flex';
const title = document.getElementById("title-form").value
const author = document.getElementById('author-form').value
const pages = document.getElementById('pages-form').value
const pagesRead = document.getElementById('pages-read-form').value
const url = document.getElementById('url-form').value
const book = new Book(title, author, pages, pagesRead, true, url)
myLibrary.push(book)
localStorage.setItem('myLibrary', JSON.stringify(myLibrary))
myLibrary.forEach(book => addBookToLibrary(book))
document.querySelector('body').style.justifyContent='space-evenly';
resetForm();
closeForm();
}
function alreadyAdded(newBook) {
const bookshelf = document.querySelector('.book-shelf');
const nodes = bookshelf.childNodes;
const books = Array.from(nodes)
books.shift()
if (books.length === 0) {
return false;
}
for (let book in books) {
if (books[book].id == newBook.id) {
return true;
}
}
return false;
}
function removeBookMyLibrary(id) {
myLibrary = myLibrary.filter(book => book.id != id);
localStorage.setItem('myLibrary', JSON.stringify(myLibrary))
}
function deleteCard(e) {
const bookDiv = e.target.parentNode.parentNode
const bookshelf = bookDiv.parentNode
bookshelf.removeChild(bookDiv)
removeBookMyLibrary(bookDiv.id);
if (myLibrary.length === 0) {
document.querySelector('body').style.justifyContent = 'center'
}
}
function createDeleteListeners() {
const deleteButtons = document.querySelectorAll('.delete-button');;
deleteButtons.forEach(button => button.addEventListener('click', deleteCard));
}
function createPagesReadListeners() {
const modifierButtons = document.querySelectorAll('.modifier')
modifierButtons.forEach(button => button.addEventListener('click', modifyPagesRead))
}
function modifyPagesRead(e) {
const bookDiv = e.target.parentNode.parentNode
pagesReadDiv = bookDiv.querySelector('.pages-read')
pagesRead = pagesReadDiv.innerText.split(' ')[0]
console.log(Number(pagesRead))
if (e.target.innerText == 'add') {
pagesRead = Number(pagesRead) + 1
pagesReadDiv.innerText = `${pagesRead} pages read`
}
else if (e.target.innerText === 'remove') {
pagesRead = Number(pagesRead) - 1
pagesReadDiv.innerText = `${pagesRead} pages read`
}
const book = getBook(bookDiv.id)
book.pagesRead = pagesRead
const progressBar = bookDiv.querySelector('progress');
progressBar.value = pagesRead;
localStorage.setItem('myLibrary', JSON.stringify(myLibrary))
}
function getBook(id) {
const book = myLibrary.find(book => book.id == id);
return book;
}
function commitChanges(e) {
const bookDiv = document.querySelector('.modifying');
const title = document.getElementById("title-form").value;
const author = document.getElementById('author-form').value;
const pages = document.getElementById('pages-form').value;
const pagesRead = document.getElementById('pages-read-form').value;
const url = document.getElementById('url-form').value;
bookDiv.querySelector('.title').innerText = title;;
bookDiv.querySelector('.author').innerText = `by ${author}`;
bookDiv.querySelector('.pages').innerText = `${pages} pages`;
bookDiv.querySelector('.pages-read').innerText = `${pagesRead} pages read`;
bookDiv.querySelector('img').src = url;
bookDiv.classList.remove('.modifying');
const progressBar = bookDiv.querySelector('progress');
progressBar.value = pagesRead;
progressBar.max = pages;
const book = getBook(bookDiv.id)
book.title = title
book.author = author
book.pages = pages
book.pagesRead = pagesRead
book.url = url
localStorage.setItem('myLibrary', JSON.stringify(myLibrary))
resetForm();
closeForm();
}
function modifyBook(e) {
const form = document.querySelector('.form-container')
form.querySelector('h2').innerText='Edit Book';
form.style.display='block'
const submitButton = document.querySelector('.submit')
submitButton.removeEventListener('click', submitForm)// = 'submitForm()' // Toggles event listener off
submitButton.addEventListener('click', commitChanges)
submitButton.innerText = "Save Changes"
const bookDiv = e.target.parentNode.parentNode
const id = bookDiv.id
const book = getBook(id);
bookDiv.classList.add('modifying')
form.querySelector('#title-form').value = book.title
form.querySelector('#author-form').value = book.author
form.querySelector('#pages-form').value = book.pages
form.querySelector('#pages-read-form').value = book.pagesRead
form.querySelector('#url-form').value = book.url
}
function closeForm() {
const form = document.querySelector('.form-container')
form.style.display='none';
form.querySelector('h2').innerText='Add a Book';
form.querySelector('.submit').innerText = 'Submit'
form.querySelector('.submit').addEventListener('click', submitForm)
resetForm();
}
function createEditListeners() {
const editButtons = document.querySelectorAll('.edit-button')
editButtons.forEach(button => button.addEventListener('click', modifyBook))
}
function createBookEventListeners() {
console.log('create listeners')
createDeleteListeners();
createEditListeners();
createPagesReadListeners();
}
function addBookToLibrary(book) {
let flag = alreadyAdded(book);
if (!flag) {
const mainContainer = document.querySelector('.book-shelf');
const bookDiv = document.createElement('div');
bookDiv.classList.add('book');
bookDiv.id = book.id;
bookDiv.style.display='flex';
bookDiv.style.flexDirection = 'column';
const imgDiv = createImgDiv(book);
const contentDiv = createContentDiv(book);
const menuDiv = createMenuDiv(book);
const footerDiv = createFooterDiv();
const progressBar = footerDiv.querySelector('progress')
progressBar.value = book.pagesRead
progressBar.max = book.pages
bookDiv.appendChild(imgDiv);
bookDiv.appendChild(contentDiv);
bookDiv.appendChild(menuDiv);
bookDiv.appendChild(footerDiv);
mainContainer.appendChild(bookDiv);
createBookEventListeners();
}
}
function resetForm() {
console.log('resetting form')
const formDiv = document.querySelector('.form-container')
document.querySelector('.submit').onclick = 'submitForm()' // Toggles event listener off
console.log(formDiv)
formDiv.querySelector('#title-form').value = '';
formDiv.querySelector('#author-form').value = '';
formDiv.querySelector('#pages-form').value = '';
formDiv.querySelector('#pages-read-form').value = '';
formDiv.querySelector('#url-form').value = '';
}
if (!localStorage.getItem('myLibrary')) {
myLibrary = []
document.querySelector('body').style.justifyContent='center'
}
else {
myLibrary = JSON.parse(localStorage.getItem('myLibrary'))
document.querySelector('body').style.justifyContent='space-evenly';
}
if (myLibrary.length === 0) {
document.querySelector('body').style.justifyContent='center'
}
const addBookButton = document.querySelector('.add-book')
myLibrary.forEach(book => addBookToLibrary(book));
document.querySelector('.submit').addEventListener('click', submitForm);
addBookButton.addEventListener('click', () => {
document.querySelector('.form-container').style.display='block';
// document.querySelector('.add-book').style.display='none';
// const book = new Book(title, author, pages, pagesRead, true)
// addBookToLibrary(book)
})