-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
136 lines (123 loc) · 4.24 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
const bookContainer = document.querySelector('.main');
const modal = document.querySelector('.modal');
const openBtn = document.querySelector('.new-book');
const closeBtn = document.querySelector('#close-btn');
const submitBtn = document.querySelector('#submit-btn');
const titleSelector = document.querySelector('#title');
const authorSelector = document.querySelector('#author');
const pagesSelector = document.querySelector('#pages');
const statusCheck = document.querySelector('#read-check');
let readStatus = '';
let statusContent = '';
const myLibrary = [];
class Book {
constructor(title, author, pages, status) {
this.title = title;
this.author = author;
this.pages = pages;
this.status = status;
}
}
// eslint-disable-next-line func-names
Book.prototype.toggleRead = function (status) {
this.status = status;
};
// open and close module
openBtn.onclick = () => {
modal.style.display = 'flex';
};
closeBtn.onclick = () => {
modal.style.display = 'none';
};
window.onclick = (event) => {
if (event.target === modal) {
modal.style.display = 'none';
}
};
// display books objects when created and add event listeners
// to remove or toggle read status
function printBook(book) {
const newDiv = document.createElement('div');
newDiv.classList.add('card');
bookContainer.appendChild(newDiv);
const titleDiv = document.createElement('div');
const authorDiv = document.createElement('div');
const pagesDiv = document.createElement('div');
const buttonsContainer = document.createElement('div');
const removeButton = document.createElement('button');
const statusButton = document.createElement('button');
newDiv.appendChild(titleDiv);
newDiv.appendChild(authorDiv);
newDiv.appendChild(pagesDiv);
newDiv.appendChild(buttonsContainer);
buttonsContainer.appendChild(removeButton);
buttonsContainer.appendChild(statusButton);
buttonsContainer.classList.add('btns-container');
removeButton.classList.add('remove-btn', 'card-btn');
statusButton.classList.add('status-btn', 'card-btn');
if (readStatus === 'read') {
statusButton.classList.add('read');
statusButton.textContent = 'Read';
} else {
statusButton.classList.add('unread');
statusButton.textContent = 'Unread';
}
removeButton.textContent = 'Remove';
titleDiv.textContent = book.title;
titleDiv.style.fontWeight = 'bold';
authorDiv.textContent = `Author: ${book.author}`;
pagesDiv.textContent = `Page Count: ${book.pages}`;
function removeCard() {
newDiv.dataset.indexNumber = myLibrary.indexOf(book);
myLibrary.splice(newDiv.dataset.indexNumber, 1);
newDiv.remove();
}
function toggleStatus() {
newDiv.dataset.indexNumber = myLibrary.indexOf(book);
if (myLibrary[newDiv.dataset.indexNumber].status === 'unread') {
statusButton.classList.remove('unread');
statusButton.classList.add('read');
readStatus = 'read';
statusButton.textContent = 'Read';
myLibrary[newDiv.dataset.indexNumber].toggleRead(readStatus);
} else if (myLibrary[newDiv.dataset.indexNumber].status === 'read') {
statusButton.classList.remove('read');
statusButton.classList.add('unread');
readStatus = 'unread';
statusButton.textContent = 'Unread';
myLibrary[newDiv.dataset.indexNumber].toggleRead(readStatus);
}
}
removeButton.addEventListener('click', removeCard);
statusButton.addEventListener('click', toggleStatus);
}
// create new object from form and then call print function
function submitted(event) {
if (document.querySelector('.modal-form').checkValidity()) {
const titleContent = titleSelector.value;
const authorContent = authorSelector.value;
const pagesContent = Number(pagesSelector.value);
if (statusCheck.checked === true) {
statusContent = 'read';
} else {
statusContent = 'unread';
}
const newBook = new Book(
titleContent,
authorContent,
pagesContent,
statusContent
);
myLibrary.push(newBook);
const latestItem = myLibrary[myLibrary.length - 1];
if (statusCheck.checked === true) {
readStatus = 'read';
} else {
readStatus = 'unread';
}
modal.style.display = 'none';
printBook(latestItem);
event.preventDefault();
}
}
submitBtn.addEventListener('click', submitted);