-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
466 lines (416 loc) · 14.9 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
let myLibrary = [];
const myCurLibrary = [];
const content = document.querySelector('.content');
const addBook = document.querySelector('.plus');
const select = document.querySelector('.select');
const modal = document.querySelector('.modal');
const cancel = document.querySelector('.cancel');
const modalTitle = document.querySelector('#title');
const modalAuthor = document.querySelector('#author');
const modalPages = document.querySelector('#pages');
const modalDesc = document.querySelector('#desc');
const modalTags = document.querySelectorAll('.modaltag');
const addButton = document.querySelector('.add');
const optionsButton = document.querySelectorAll('.fa-ellipsis');
const deleteButton = document.querySelector('.options-delete');
const editButton = document.querySelector('.options-edit');
const errorTitle = document.querySelector('.error-title');
const errorPage = document.querySelector('.error-page')
const tColor = 'rgb(105, 102, 92);'
let curFilter = null;
let curFilterElement = null;
// handleFilter handles when a filter is clicked
function handleFilter(e) {
let filterID = Number(e.id.slice(0, -1));
if (filterID == curFilter) {
e.style = '';
e.firstChild.style = '';
for (let i = 0; i < content.childElementCount; i++) {
content.children[i].style = "opacity: 0; transform: translateY(-0.5rem); transition: ease 0.15s all";
}
curFilter = null;
setTimeout(function () {
content.innerHTML = null;
for (let i = 0; i < myLibrary.length; i++) {
if ((myLibrary[i] != null && !(myLibrary[i].read && hideReadInput.checked))) {
displayBook(myLibrary[i], i);
}
}
}, 150);
} else {
if (curFilterElement) {
curFilterElement.style = '';
curFilterElement.firstChild.style = '';
}
e.style = 'background-color: rgba(0,0,0,0.1);';
e.firstChild.style.border = '2px solid rgb(105, 102, 92)';
for (let i = 0; i < content.childElementCount; i++) {
content.children[i].style = "opacity: 0; transform: translateY(-0.5rem); transition: ease 0.15s all";
}
setTimeout(function () {
content.innerHTML = null;
for (let i = 0; i < myLibrary.length; i++) {
if ((myLibrary[i] != null) && myLibrary[i].tags.includes(filterID) && !(myLibrary[i].read && hideReadInput.checked)) {
displayBook(myLibrary[i], i);
}
}
}, 150);
curFilter = filterID;
curFilterElement = e;
}
}
// handleHideRead Hides all read books
function handleHideRead(e) {
for (let i = 0; i < content.childElementCount; i++) {
content.children[i].style = "opacity: 0; transform: translateY(-0.5rem); transition: ease 0.1s all";
}
setTimeout(function () {
content.innerHTML = null;
for (let i = 0; i < myLibrary.length; i++) {
if ((myLibrary[i] != null) && !(myLibrary[i].read && e.checked)) {
if (curFilter == null) {
displayBook(myLibrary[i], i);
} else if (myLibrary[i].tags.includes(curFilter)) {
displayBook(myLibrary[i], i);
}
}
}
}, 100);
}
// handleRead toggles a books read / unread status
function handleRead(e) {
const index = e.parentElement.parentElement.parentElement.id;
myLibrary[index].read = !myLibrary[index].read;
if (hideReadInput.checked && myLibrary[index].read) {
const remove = e.parentElement.parentElement.parentElement;
remove.style = "opacity: 0; transform: translateY(-0.5rem); transition: ease 0.2s all";
setTimeout(function () {
remove.remove();
}, 300);
}
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
}
// MENU CONTROLS
// Controls the opening and closing of the menu on the cards
let menuOpen = false;
let curMenu = null;
function handleOptionsButton(e) {
const optionsSibling = e.nextElementSibling;
if (curMenu != null) {
closeMenu();
}
if (!optionsSibling.style.opacity) {
optionsSibling.style = "visibility: visible; opacity: 1; transform: scaleY(1); translateY(0)";
menuOpen = true;
curMenu = optionsSibling;
}
}
window.addEventListener('click', e => {
if (!e.target.classList.contains('menu') && menuOpen) {
closeMenu();
}
})
function closeMenu() {
menuOpen = false;
curMenu.style = "transition: all 0.3s ease";
curMenu = null;
}
// handles the delete button on the menu
function handleDeleteButton(e) {
const index = e.parentElement.parentElement.parentElement.parentElement.id;
const remove = e.parentElement.parentElement.parentElement.parentElement;
remove.style = "opacity: 0; transform: translateY(-0.5rem); transition: ease 0.2s all";
setTimeout(function () {
remove.remove();
}, 300);
myLibrary[index] = null;
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
}
let curEdit = -1;
// handles the edit button on the menu
function handleEditButton(e){
closeMenu();
addButton.innerHTML = "Edit";
const index = e.parentElement.parentElement.parentElement.parentElement.id;
const edit = e.parentElement.parentElement.parentElement.parentElement;
curEdit = index;
let updateBook = myLibrary[curEdit];
// render modal with current values
modalTitle.value = updateBook.title;
modalAuthor.value = updateBook.author;
modalPages.value = updateBook.pages;
modalDesc.value = updateBook.desc;
const tags = updateBook.tags;
for (let i = 0; i < tags.length; i++) {
modalTags[i].classList.toggle('filterselect');
modalTags[i].firstChild.style = "border: 2px solid " + tColor;
}
select.style = "visibility: visible; opacity: 1;";
modal.style = "";
}
// MODAL CONTROLS
// Adds hovering effect on each modal when selected
modalTags.forEach(item => {
item.addEventListener('click', e => {
e.target.firstChild.style = e.target.firstChild.style.border == "" ? "border: 2px solid " + tColor : null;
e.target.classList.toggle('filterselect');
})
})
addButton.addEventListener('click', handleAddButton);
// handleAddButton handles when the user clicks the add button on the modal
function handleAddButton() {
const tags = [];
for (let i = 0; i < modalTags.length; i++) {
if (modalTags[i].classList.contains('filterselect')) {
tags.push(i + 1);
}
}
if (Number.isInteger(Number(modalPages.value)) && modalPages.value >= 0 && modalTitle.value != "") {
if(curEdit == -1){
console.log("ADDING NEW BOOK");
newBook = new Book(modalTitle.value, modalAuthor.value, modalPages.value, modalDesc.value, tags, false);
addBookToLibrary(newBook);
displayBooks(myLibrary);
handleCancel();
} else {
console.log("UPDATING BOOK");
let updateBook = myLibrary[curEdit];
// update library item
updateBook.title = modalTitle.value;
updateBook.author = modalAuthor.value;
updateBook.pages = modalPages.value;
updateBook.desc = modalDesc.value;
updateBook.tags = tags;
const editItem = document.getElementById(curEdit);
const title = displayTitle(updateBook);
const top = displayTop(updateBook);
const desc = document.createElement("p");
desc.innerText = updateBook.desc;
const bottom = displayBottom(updateBook);
editItem.innerHTML = '';
editItem.appendChild(title);
editItem.appendChild(top);
editItem.appendChild(desc);
editItem.appendChild(bottom);
editItem.style = "opacity: 0; transform: translateY(-0.5rem); transition: ease 0.2s all";
setTimeout(function () {
editItem.style = "transition: ease 0.2s all";
}, 100);
curEdit = -1;
console.log("MY LIBRARY");
console.log(myLibrary);
console.log("MY CUR LIBRARY")
console.log(myCurLibrary);
handleCancel();
}
} else {
handleError();
}
}
// resetModal resets the modal to an empty state;
function resetModal() {
modalTitle.classList.remove("error");
errorTitle.style.display = "none";
modalPages.classList.remove("error");
errorPage.style.display = "none";
modalTitle.value = "";
modalAuthor.value = "";
modalPages.value = "";
modalDesc.value = "";
for (let i = 0; i < modalTags.length; i++) {
if (modalTags[i].classList.contains('filterselect')) {
modalTags[i].classList.toggle('filterselect');
}
modalTags[i].firstChild.style.border = "";
}
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
}
// handleError handles the displaying of error when a user inputs an invalid input
function handleError() {
if (modalTitle.value == "") {
console.log(modalTitle);
modalTitle.classList.add("error");
errorTitle.style.display = "inline";
} else {
modalTitle.classList.remove("error");
errorTitle.style.display = "none";
}
if (!Number.isInteger(Number(modalPages.value)) || modalPages.value < 0) {
modalPages.classList.add("error");
errorPage.style.display = "inline";
} else {
modalPages.classList.remove("error");
errorPage.style.display = "none";
}
}
// Handle clicking plus sign and cancel on modal
select.style = "visibility: hidden; opacity: 0;";
modal.style = " transform: translateY(-3rem);";
addBook.addEventListener('click', handleAddBook);
function handleAddBook() {
addButton.innerHTML = "Add";
select.style = "visibility: visible; opacity: 1;";
modal.style = "";
}
select.addEventListener('mousedown', e => handleSelectUp(e));
let selectvalid = false;
function handleSelectUp(e) {
if (e.target.classList.contains('select')) {
selectvalid = true;
setTimeout(function () {
selectvalid = false;
}, 1000);
}
}
select.addEventListener('mouseup', e => handleSelect(e));
function handleSelect(e) {
if (e.target.classList.contains('select') && selectvalid) {
selectvalid = false;
handleCancel();
}
}
cancel.addEventListener('click', handleCancel);
function handleCancel() {
select.style = "visibility: hidden; opacity: 0;";
modal.style = "transform: translateY(-3rem);";
setTimeout(function () {
resetModal()
}, 400);
}
function Book(title, author, pages, desc, tags, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.desc = desc;
this.tags = tags.sort();
this.read = read;
}
// Book.prototype.info = function(){
// read = this.read ? "read" : "not read yet";
// return(this.title + " by " + this.author + ", " + this.pages + " pages, " + read);
// }
// hello = new Book("The Hobbit","Hello","Hello","true");
// console.log(hello.info());
function addBookToLibrary(myBook) {
myLibrary.push(myBook);
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
}
function displayBooks(myLibrary) {
for (let i = 0; i < myLibrary.length; i++) {
if (!myCurLibrary.includes(myLibrary[i]) && myLibrary[i]) {
displayBook(myLibrary[i], i);
myCurLibrary.push(myLibrary[i]);
}
}
}
function displayBook(myBook, num) {
const title = displayTitle(myBook);
const top = displayTop(myBook);
const desc = document.createElement("p");
if(myBook.read){
desc.innerHTML = myBook.desc;
} else { // default option unless I want to add links on card
desc.innerText = myBook.desc;
}
const bottom = displayBottom(myBook);
const item = document.createElement("div");
item.classList.add("item");
item.appendChild(title);
item.appendChild(top);
item.appendChild(desc);
item.appendChild(bottom);
item.style = "opacity: 0; transform: translateY(-0.5rem); transition: ease 0.2s all";
item.id = num;
content.appendChild(item);
setTimeout(function () {
item.style = "transition: ease 0.2s all";
}, 100);
}
// todo: support super long title
const sanitizeHTML = function (str) {
return str.replace(/[^\w. ]/gi, function (c) {
return '&#' + c.charCodeAt(0) + ';';
});
};
function displayTitle(myBook) {
const title = document.createElement("h3");
let titleText = sanitizeHTML(myBook.title);
if (titleText.length > 30) {
titleText = titleText.slice(0, 27);
titleText += "...";
}
title.innerHTML = titleText;
const wrapper = document.createElement("div");
wrapper.classList.add("title");
wrapper.appendChild(title);
const insertWrapper = `
<div class="options-container">
<i onclick={handleOptionsButton(this)} class="fa-solid fa-ellipsis menu"></i>
<div class="options menu">
<p onclick={handleEditButton(this)} class="options-edit menu">Edit ...</p>
<hr class="menu">
<p onclick={handleDeleteButton(this)} class="options-delete menu">Delete</p>
</div>
</div>
`
wrapper.innerHTML += insertWrapper;
return wrapper;
}
function displayTop(myBook) {
const top = document.createElement("div");
top.classList.add("top");
if (myBook.author) {
const author = document.createElement("p");
author.classList.add("author");
author.innerHTML = `By <b>${sanitizeHTML(myBook.author)}</b>`;
top.append(author);
}
if (myBook.pages) {
const pages = document.createElement("p");
pages.classList.add("pages");
pages.innerHTML = ` <b>${sanitizeHTML(myBook.pages)}</b> Pages `;
top.appendChild(pages);
}
return top;
}
function displayBottom(myBook) {
const labels = document.createElement("div");
for (let i = 0; i < myBook.tags.length; i++) {
labels.innerHTML += `<div class="color color${myBook.tags[i]}"></div>`;
}
labels.classList.add("labels");
const bottom = document.createElement("bottom");
bottom.classList.add("bottom");
bottom.appendChild(labels);
if (myBook.read) {
bottom.innerHTML += '<div class="read"><input onclick={handleRead(this)} type="checkbox" class="read-box" style ="border: 0.15em solid rgb(105, 102, 92)"checked><div style="color: rgb(105, 102, 92)">Finished</div></div>';
} else {
bottom.innerHTML += '<div class="read"><input onclick={handleRead(this)} type="checkbox" class="read-box"><div>Finished</div></div>';
}
return bottom;
}
var storedBooks = JSON.parse(localStorage.getItem("myLibrary"));
var firstTime = localStorage.getItem("first_instance");
if(!firstTime) {
localStorage.setItem("first_instance","1");
let firstBookString = `
Library is a great website for keeping track of all the books you've read!
It helps you keep track of your reading progress, including the title, author, a small description, and number of pages for each book. You can add new books and edit existing entries at any time, as well as filter your books by genre.
Finally, Library allows you to hide finished books from your main view.
`;
firstBook = new Book("Welcome to Library!", "JusGu", "", firstBookString, [], false);
secondBook = new Book("Visit my GitHub", "JusGu", "", `If you're interested in seeing more projects like this, please visit my GitHub at <a style='text-decoration: none; color: ${tColor}' target = '_blank'href='https://github.com/JusGu'> https://github.com/JusGu</a> `, [], true);
myLibrary.unshift(secondBook);
myLibrary.unshift(firstBook);
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
} else {
if (storedBooks) {
for (let i = 0; i < storedBooks.length; i++) {
if (storedBooks[i] !== null) {
myLibrary.push(storedBooks[i]);
}
}
}
}
console.log()
displayBooks(myLibrary);