-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
233 lines (214 loc) · 6.94 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
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
const content = document.getElementById('content');
const listHeader = {
read: document.getElementById('read'),
title: document.getElementById('title'),
author: document.getElementById('author'),
pages: document.getElementById('pages'),
id: document.getElementById('id'),
};
const popup = {
showBtn: document.getElementById('show'),
overlay: document.getElementById('overlay'),
closeBtn: document.getElementById('close'),
addBtn: document.getElementById('add'),
input: {
title: document.getElementById('book-title'),
author: document.getElementById('book-author'),
pages: document.getElementById('book-pages'),
checkbox: document.getElementById('book-read'),
},
};
let idCounter = 0;
let myLibrary = [];
let sortBy = ['id', 'desc']; // read, title, author, pages, id || asc and desc
// book -constructor- class
const Book = class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.id = idCounter;
idCounter += 1;
}
};
// checks if all the values of one type are the same
const checkAllTheSame = function checkIfAllTheSame() {
let allTheSame = true;
myLibrary.reduce((previous, current) => {
if (previous === 'init') return current[sortBy[0]];
if (current[sortBy[0]] !== previous) allTheSame = false;
return current[sortBy[0]];
}, 'init');
return allTheSame;
};
// sort myLibrary
const sortLibrary = function sortLibrary() {
if (checkAllTheSame()) return;
if (sortBy[0] === 'id') { // ascending
myLibrary.sort((a, b) => b.id - a.id);
} else if (sortBy[0] === 'pages') {
myLibrary.sort((a, b) => a.pages - b.pages);
} else if (sortBy[0] === 'author') {
myLibrary.sort((a, b) => (
(a.author.toLowerCase() > b.author.toLowerCase()) ? 1 : -1
));
} else if (sortBy[0] === 'title') {
myLibrary.sort((a, b) => (
(a.title.toLowerCase() > b.title.toLowerCase()) ? 1 : -1
));
} else if (sortBy[0] === 'read') {
myLibrary.sort((a, b) => ((a.read < b.read) ? -1 : 1));
}
if (sortBy[1] === 'desc') myLibrary.reverse();
};
// remove all checked classes
const rmvAll = function removeAllCheckedClasses(id) {
document.querySelectorAll('#content label + button').forEach((btn) => {
if (btn.id !== id) btn.className = '';
});
};
// display library if something changed
const displayLibrary = function displayLibraryOnPage() {
// update localStorage
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
localStorage.setItem('sortBy', JSON.stringify(sortBy));
// clear content for new build
document.querySelectorAll('.book').forEach((book) => book.remove());
document.querySelectorAll('#content hr:not(:first-of-type)').forEach(
(hr) => hr.remove(),
);
// loop through myLibrary and display each book
myLibrary.forEach((book) => {
const div = document.createElement('div');
div.classList.add('book');
div.id = book.id;
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = book.read;
const pTitle = document.createElement('p');
pTitle.textContent = book.title;
const pAuthor = document.createElement('p');
pAuthor.textContent = book.author;
const pPages = document.createElement('p');
pPages.textContent = book.pages;
const btnDel = document.createElement('button');
btnDel.textContent = 'Delete';
div.append(checkbox, pTitle, pAuthor, pPages, btnDel);
const hr = document.createElement('hr');
content.append(div, hr);
});
// checkbox listener
const readCheckbox = [...document.querySelectorAll('.book input')];
readCheckbox.forEach((checkbox) => {
checkbox.addEventListener('change', (e) => {
myLibrary = myLibrary.map((book) => {
if (book.id === Number(e.target.parentNode.id)) {
book.read = e.target.checked;
}
return book;
});
sortLibrary();
displayLibrary();
});
});
// delete book button listener
const deleteBtn = [...document.querySelectorAll('.book button')];
deleteBtn.forEach((btn) => btn.addEventListener('click', (e) => {
myLibrary = myLibrary.filter(
(book) => book.id !== Number(e.target.parentElement.id),
);
displayLibrary();
}));
};
// sort list header
Object.values(listHeader).forEach((header) => {
header.addEventListener('click', (e) => {
rmvAll(e.target.id);
if (!e.target.classList.value) {
e.target.className = 'checked-1';
sortBy = [e.target.id, 'desc'];
} else if (e.target.className === 'checked-1') {
e.target.className = 'checked-2';
sortBy = [e.target.id, 'asc'];
} else if (e.target.className === 'checked-2') {
e.target.className = 'checked-1';
sortBy = [e.target.id, 'desc'];
}
sortLibrary();
displayLibrary();
});
});
// add new book to library
popup.showBtn.addEventListener('click', () => {
popup.overlay.style.display = 'grid';
});
popup.closeBtn.addEventListener('click', () => {
popup.overlay.style.display = null;
});
popup.addBtn.addEventListener('click', () => {
let valid = true;
if (!popup.input.title.value) {
popup.input.title.parentElement.dataset.msg = 'Please enter a title';
valid = false;
} else {
popup.input.title.parentElement.dataset.msg = '';
}
if (!popup.input.author.value) {
popup.input.author.parentElement.dataset.msg = 'Please enter a author';
valid = false;
} else {
popup.input.author.parentElement.dataset.msg = '';
}
if (!popup.input.pages.value) {
popup.input.pages.parentElement.dataset.msg = 'Please enter a page number';
valid = false;
} else if (isNaN(popup.input.pages.value)) {
popup.input.pages.parentElement.dataset.msg = 'Please enter valid number';
valid = false;
} else if (popup.input.pages.value < 0) {
popup.input.pages.parentElement.dataset.msg = 'Please enter positive number';
valid = false;
} else {
popup.input.pages.parentElement.dataset.msg = '';
}
if (valid) {
const book = new Book(
popup.input.title.value,
popup.input.author.value,
popup.input.pages.value,
popup.input.checkbox.checked,
);
myLibrary.push(book);
displayLibrary();
Object.values(popup.input).forEach((x) => {
if (x !== popup.input.checkbox) {
x.value = '';
x.parentElement.dataset.msg = '';
} else {
x.checked = false;
}
});
popup.overlay.style.display = null;
}
});
// on page load
const tmp1 = localStorage.getItem('myLibrary');
const tmp2 = localStorage.getItem('sortBy');
if (tmp1) {
myLibrary = JSON.parse(tmp1);
} else {
myLibrary.push(
new Book('The Hobbit', 'J.R.R. Tolkien', 295, true),
);
myLibrary.push(
new Book('The Catcher in the Rye', 'J.D. Salinger', 220, false),
);
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
}
if (tmp2) sortBy = JSON.parse(tmp2);
listHeader[sortBy[0]].classList.add(
(sortBy[1] === 'desc') ? 'checked-1' : 'checked-2',
);
sortLibrary();
displayLibrary();