-
Notifications
You must be signed in to change notification settings - Fork 2
/
scripts.js
162 lines (153 loc) · 5.17 KB
/
scripts.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
let myLibrary = [];
class Book {
constructor(title, author, pages, status) {
this.title = title;
this.author = author;
this.pages = pages;
this.status = status;
}
}
// GET BOOKS FROM LOCAL STORAGE
if (localStorage.getItem('books') === null) {
myLibrary = [];
} else {
const booksFromStorage = JSON.parse(localStorage.getItem('books'));
myLibrary = booksFromStorage;
}
function showLibraryInfo() {
const booksRead = document.querySelector('#books-read');
const booksUnread = document.querySelector('#books-unread');
const totalBooks = document.querySelector('#total-books');
let readCounter = 0;
let unreadCounter = 0;
booksRead.textContent = 0;
booksUnread.textContent = 0;
for (let i = 0; i < myLibrary.length; i += 1) {
if (myLibrary[i].status === true) {
readCounter += 1;
booksRead.textContent = readCounter;
} else if (myLibrary[i].status === false) {
unreadCounter += 1;
booksUnread.textContent = unreadCounter;
}
}
totalBooks.textContent = myLibrary.length;
}
function showBooksInLibrary() {
// SAVE TO LOCAL STORAGE
localStorage.setItem('books', JSON.stringify(myLibrary));
showLibraryInfo();
const bookList = document.querySelector('#table-body');
bookList.textContent = '';
for (let i = 0; i < myLibrary.length; i += 1) {
const bookRow = document.createElement('tr');
bookRow.classList.add('book-info');
bookList.appendChild(bookRow);
// BOOK TITLE
const bookTitle = document.createElement('td');
bookTitle.textContent = myLibrary[i].title;
bookRow.appendChild(bookTitle);
// BOOK AUTHOR
const bookAuthor = document.createElement('td');
bookAuthor.textContent = myLibrary[i].author;
bookRow.appendChild(bookAuthor);
// BOOK PAGES
const bookPages = document.createElement('td');
bookPages.textContent = myLibrary[i].pages;
bookRow.appendChild(bookPages);
// BOOK STATUS BUTTON
const bookStatus = document.createElement('td');
const statusSymbol = document.createElement('i');
if (myLibrary[i].status === false) {
statusSymbol.classList.add('fas', 'fa-times');
} else {
statusSymbol.classList.add('fas', 'fa-check');
}
bookStatus.appendChild(statusSymbol);
bookRow.appendChild(bookStatus);
// BOOK REMOVAL BUTTON
const bookDelete = document.createElement('td');
const deleteSymbol = document.createElement('i');
deleteSymbol.classList.add('fas', 'fa-trash-alt');
bookDelete.appendChild(deleteSymbol);
bookRow.appendChild(bookDelete);
}
}
function addBookToLibrary(title, author, pages, status) {
const book = new Book(title, author, pages, status);
myLibrary.push(book);
showBooksInLibrary();
}
// FORM VALIDATION
function validateForm(event) {
event.preventDefault();
const form = document.querySelector('form');
const titleInput = document.querySelector('#title');
const titleErr = document.querySelector('.title');
const nameInput = document.querySelector('#name');
const nameErr = document.querySelector('.name');
const numberInput = document.querySelector('#number');
const numberErr = document.querySelector('.number');
const checkbox = document.querySelector('input[name="checkbox"]');
if (titleInput.value === '') {
titleErr.style.display = 'block';
} else {
titleErr.style.display = 'none';
}
if (nameInput.value === '') {
nameErr.style.display = 'block';
} else {
nameErr.style.display = 'none';
}
if (numberInput.value === '' || numberInput.value.match(/[^1-9]/) || numberInput.value <= 0) {
numberErr.style.display = 'block';
} else {
numberErr.style.display = 'none';
}
if (titleInput.value !== '' && nameInput.value !== '' && numberInput.value !== '' && numberInput.value > 0) {
if (checkbox.checked) {
addBookToLibrary(titleInput.value, nameInput.value, numberInput.value, true);
} else {
addBookToLibrary(titleInput.value, nameInput.value, numberInput.value, false);
}
form.reset();
}
}
// MODAL FOR BOOKS REMOVAL
function manipulateModal() {
const modal = document.querySelector('#modal');
modal.style.display = 'block';
modal.addEventListener('click', (event) => {
const { target } = event;
if (target.classList.contains('close')) {
modal.style.display = 'none';
} else if (target.classList.contains('confirm-removal')) {
myLibrary = [];
modal.style.display = 'none';
}
});
}
function listenClicks() {
document.addEventListener('click', (event) => {
const { target } = event;
const tr = target.parentNode.parentNode.rowIndex - 1;
if (target.id === 'add-book') {
validateForm(event);
} else if (target.id === 'delete-all-btn') {
manipulateModal();
} else if (target.classList.contains('fa-trash-alt')) {
myLibrary.splice(tr, 1);
} else if (target.classList.contains('fa-check')) {
target.classList.remove('fa-check');
target.classList.add('fa-times');
myLibrary[tr].status = false;
} else if (target.classList.contains('fa-times')) {
target.classList.remove('fa-times');
target.classList.add('fa-check');
myLibrary[tr].status = true;
}
showBooksInLibrary();
});
}
showBooksInLibrary();
listenClicks();