-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (106 loc) · 3.35 KB
/
app.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
const bookshelf = document.getElementById('bookshelf');
const bookform = document.querySelector('form');
const addBookBtn = document.querySelector('[data-modal-target]');
const overlay = document.getElementById('overlay')
let myLibrary = [];
bookform.addEventListener('submit', e => {
const modal = document.querySelector('.modal.active');
e.preventDefault();
addBookToLibrary();
displayBooks();
resetForm();
closeModal(modal);
});
addBookBtn.addEventListener('click', () => {
const modal = document.querySelector(addBookBtn.dataset.modalTarget)
openModal(modal);
});
overlay.addEventListener('click', () => {
const modal = document.querySelector('.modal.active');
closeModal(modal);
});
function openModal(modal) {
if (modal == null) return
modal.classList.add('active');
overlay.classList.add('active');
}
function closeModal(modal) {
if (modal == null) return
modal.classList.remove('active');
overlay.classList.remove('active');
}
/**
* Constructor for Book
* @param {String} title Title of book
* @param {String} author Author of book
* @param {Number} pages Number of pages in book
* @param {String} status
*/
function Book(title, author, pages, status) {
this.title = title;
this.author = author;
this.pages = pages;
this.status = status;
}
// /**
// * Adds a book to the library
// * @param {Book}} book
// */
// function addBookToLibrary(book){
// myLibrary.push(book);
// }
/**
* Adds a book to the library
* @param {Book}} book
*/
function addBookToLibrary() {
title = bookform.elements["title"].value;
author = bookform.elements["author"].value;
pages = bookform.elements["pages"].value;
book_status = bookform.elements["status"].checked ? "Read": "Not Read Yet";
new_book = new Book(title, author, pages, book_status);
myLibrary.push(new_book);
}
/**
* Displays all the books currently in myLibrary
*/
function displayBooks() {
book = myLibrary[myLibrary.length - 1];
card = document.createElement('div');
card.classList.add('card')
card.dataset.book = book;
title = document.createElement('div');
title.textContent = book.title
title.classList.add('title');
author = document.createElement('div');
author.textContent = book.author
author.classList.add('author');
pages = document.createElement('div');
pages.textContent = `${book.pages} pages`
pages.classList.add('pages');
book_status = document.createElement('button');
book_status.textContent = book.status
book_status.classList.add('status-btn');
if (book.status == "Read") {
book_status.classList.add('read');
}
book_status.addEventListener('click', (e) => {
e.target.classList.toggle('read');
e.target.textContent === "Read" ? e.target.textContent = "Not Read Yet": e.target.textContent = "Read";
})
removebtn = document.createElement('button');
removebtn.classList.add('remove-btn');
removebtn.textContent = 'Remove';
removebtn.addEventListener('click', (e) => {
myLibrary = myLibrary.filter(other_book => other_book.title !== book.title)
bookshelf.removeChild(e.target.parentNode);
})
card.append(title, author, pages, book_status, removebtn)
bookshelf.appendChild(card);
}
/**
* Resets Add Book Form
*/
function resetForm() {
bookform.reset();
}