-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.js
More file actions
125 lines (94 loc) · 3.36 KB
/
Copy pathlibrary.js
File metadata and controls
125 lines (94 loc) · 3.36 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
const modal = document.getElementById('modal');
const btnOpenModal = document.getElementById('openModal');
const span = document.getElementsByClassName('close')[0];
// When the user clicks on the button, open the modal
btnOpenModal.onclick = function() {
modal.style.display = 'block';
}
//closes modal when user clicks x
span.onclick = function() {
modal.style.display = 'none';
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
}
// Close modal , use when user clicks submit to close form
function closeForm() {
modal.style.display = 'none';
}
const form = document.getElementById('form');
const library = document.getElementById('library');
const formTitle = document.getElementById('title');
const formAuthor = document.getElementById('author');
const formPages = document.getElementById('pages');
let readStatus = document.getElementById('read');
const btnDel = document.getElementById('delete');
const formAddBook = document.getElementById('addBook');
let myLibrary = [];
class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = Number(pages);
this.read = read;
this.id = title.toUpperCase() + pages;
}
}
const addBook = (e) => {
e.preventDefault();
title = formTitle.value;
author = formAuthor.value;
pages = formPages.value;
read = readStatus.checked;
id = this.id;
let newBook = new Book (title, author, pages, read);
myLibrary.push(newBook);
createBook(newBook);
closeForm();
document.getElementById('form').reset();
}
const createBook = (item) => {
const bookCard = document.createElement('div')
const cardTitle = document.createElement('h3');
const cardAuthor = document.createElement('p');
const cardPages = document.createElement('p');
const cardRead = document.createElement('button');
const cardDelete = document.createElement('button');
bookCard.classList.add('bookCard');
bookCard.setAttribute('id', item.id);
library.appendChild(bookCard);
cardTitle.innerText = item.title;
cardTitle.style.textAlign = 'center';
bookCard.appendChild(cardTitle);
cardAuthor.innerText = `By: ${item.author}`;
cardAuthor.style.textAlign = 'center';
bookCard.appendChild(cardAuthor);
cardPages.innerText = `${item.pages} pages`;
cardPages.style.textAlign = 'center'
bookCard.appendChild(cardPages);
cardRead.classList.add('readBtn');
cardRead.addEventListener('click', () => { // the value of book's read value will toggle between true/false
item.read = !item.read;
toggleRead(); // button text will change corresponding to book's read value
})
toggleRead(); //will display read status if read checkbox is checked or not.
bookCard.appendChild(cardRead);
function toggleRead() {
if(item.read === false) {
cardRead.innerText = 'Unread'
} else cardRead.innerText = 'Read'
}
cardDelete.innerText = 'Remove';
cardDelete.classList.add('removeBtn');
cardDelete.addEventListener('click', () => {
let div = document.getElementById(item.id);
div.parentElement.removeChild(div); // removes this bookCard from DOM
myLibrary.splice(myLibrary.indexOf(item), 1) // removes this object from myLibrary array
}
)
bookCard.appendChild(cardDelete);
}
form.addEventListener('submit', addBook);