-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
250 lines (202 loc) · 7.47 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
//Book class
class Book {
constructor(title, author, pages, read = false) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
toggleRead() {
this.read = !this.read;
}
}
//Set up local storage
let myLibrary = localStorage.getItem('library') ?
JSON.parse(localStorage.getItem('library')) :
[
]
function saveToLocalStorage() {
localStorage.setItem('library', JSON.stringify(myLibrary));
}
//Add book to library array
function addBookToLibrary(title, author, pages, read) {
myLibrary.push(new Book(title, author, pages, read));
}
//Toggle new book form
const addBookBtn = document.querySelector('button#add-book-btn');
const form = document.querySelector('form');
const toggleForm = () => {
form.classList.toggle('hidden');
if (form.className == 'hidden') {
addBookBtn.textContent = '+ Add Book';
} else {
addBookBtn.textContent = '- Add Book';
}
}
addBookBtn.addEventListener('click', toggleForm);
//Display book
function display(book) {
const bookList = document.querySelector('#book-list');
const newRow = document.createElement('tr');
//Set data-index to distinguish rows
newRow.setAttribute('data-index', myLibrary.indexOf(book));
newRow.setAttribute('class', 'visible');
newRow.innerHTML = `
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.pages}</td>
`
//Add status button
const statusCol = document.createElement('td')
const statusBtn = document.createElement('button');
statusBtn.classList.add('statusBtn');
if (book.read) {
statusBtn.classList.add('read');
statusBtn.textContent = 'Read';
} else {
statusBtn.classList.add('unread');
statusBtn.textContent = 'Unread';
}
statusCol.appendChild(statusBtn);
//Add delete icon
const deleteIcon = document.createElement('td');
deleteIcon.innerHTML = `<img class="delete-icon" src="img/delete_icon.png">`
//Putting it all together
newRow.appendChild(statusCol);
newRow.appendChild(deleteIcon);
bookList.appendChild(newRow);
}
document.addEventListener('DOMContentLoaded', myLibrary.forEach(book => display(book)));
//Toggle read and unread
document.querySelector('#book-list').addEventListener('click', (e) => {
const statusBtn = e.target;
if(statusBtn.classList.contains('statusBtn')) {
let tableRow = statusBtn.parentNode.parentNode
let index = +tableRow.getAttribute('data-index');
myLibrary[index].read = !myLibrary[index].read
localStorage.clear();
saveToLocalStorage();
if (statusBtn.classList.contains('statusBtn')) {
statusBtn.classList.toggle('unread');
statusBtn.classList.toggle('read');
switch (statusBtn.textContent) {
case "Unread":
statusBtn.textContent = "Read"
break;
case "Read":
statusBtn.textContent = "Unread"
}
}
}
filterBooks();
})
//Add new book button
const addBtn = document.querySelector('#add-btn');
const titleInput = document.querySelector('#title');
const authorInput = document.querySelector('#author');
const pagesInput = document.querySelector('#pages');
const readCheckbox = document.querySelector('#read-status');
//Validation
function showAlert(message, className) {
const div = document.createElement('div');
div.className = `alert ${className}`
div.appendChild(document.createTextNode(message));
const form = document.querySelector('#new-book-form');
const titleLabel = document.querySelector('label[for="title"]');
form.insertBefore(div, titleLabel);
setTimeout(() => document.querySelector('.alert').remove(), 3000);
}
//Add new book
const addRow = (e) => {
e.preventDefault();
if (titleInput.value === '') {
showAlert('Please fill in a title', 'warning');
} else if (authorInput.value === '') {
showAlert('Please fill in an author', 'warning');
} else {
if (readCheckbox.checked) {
read = true;
} else {
read = false;
}
addBookToLibrary(
titleInput.value,
authorInput.value,
pagesInput.value,
read
);
const newestBook = myLibrary[myLibrary.length - 1];
display(newestBook);
filterBooks();
saveToLocalStorage();
//Empty input again
titleInput.value = '';
authorInput.value = '';
pagesInput.value = '';
readCheckbox.checked = false;
//Refocus on title
titleInput.focus();
showAlert('Book Added', 'success')
}
}
addBtn.addEventListener('click', addRow)
//Remove book
document.querySelector('#book-list').addEventListener('click', (e) => {
if (e.target.classList == 'delete-icon') {
e.target.parentElement.parentElement.remove();
//Remove book from library array
const index = e.target.parentElement.parentElement.getAttribute('data-index');
myLibrary.splice(index, 1);
localStorage.clear();
localStorage.setItem('library', JSON.stringify(myLibrary));
}
})
//Filter books
const optionAll = document.querySelector('#all');
const optionReadOnly = document.querySelector('#read-only');
const optionUnreadOnly = document.querySelector('#unread-only');
function filterBooks() {
const readStatus = document.querySelectorAll('td button')
if (optionAll.selected) {
readStatus.forEach(button => {
const index = button.parentElement.parentElement.getAttribute('data-index');
const tableRow = button.parentElement.parentElement
tableRow.removeAttribute('class')
tableRow.removeAttribute('style')
tableRow.setAttribute('class', 'visible')
});
} else if (optionReadOnly.selected) {
readStatus.forEach(button => {
const index = button.parentElement.parentElement.getAttribute('data-index');
const tableRow = button.parentElement.parentElement
if (myLibrary[index].read) {
tableRow.removeAttribute('class');
tableRow.removeAttribute('style')
tableRow.setAttribute('class', 'visible')
} else if (myLibrary[index].read == false) {
tableRow.removeAttribute('class')
tableRow.removeAttribute('style')
tableRow.setAttribute('class', 'hidden');
}
})
} else if (optionUnreadOnly.selected) {
readStatus.forEach(button => {
const index = button.parentElement.parentElement.getAttribute('data-index');
const tableRow = button.parentElement.parentElement
if (myLibrary[index].read == false) {
tableRow.removeAttribute('class');
tableRow.removeAttribute('style')
tableRow.setAttribute('class', 'visible')
} else if (myLibrary[index].read) {
tableRow.removeAttribute('class');
tableRow.removeAttribute('style')
tableRow.setAttribute('class', 'hidden');
}
})
}
const visibleRows = document.querySelectorAll('.visible');
let lastVisibleRow = visibleRows[visibleRows.length - 1];
lastVisibleRow.style.cssText = 'border-bottom-style: none';
}
const filter = document.querySelector('#filter');
filter.addEventListener('change', filterBooks);