-
Notifications
You must be signed in to change notification settings - Fork 57
/
script.js
694 lines (620 loc) · 20.4 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
displayBooks(); //As the browser open it show all the stored books
let libraryForm = document.getElementById("libraryForm");
let name = document.getElementById("bookName");
let author = document.getElementById("author");
let isbn = document.getElementById("isbnno");
let edition = document.getElementById("edition");
let publicationD = document.getElementById("publicationdate");
let read = document.getElementById("read-toggle");
let url = document.getElementById("bookurl");
let favorite = document.getElementById("fav-toggle");
let type;
console.log(favorite);
let fiction = document.getElementById("fiction");
let programming = document.getElementById("programming");
let science = document.getElementById("science");
let others = document.getElementById("other");
let editIndex = -1;
// Adding Books
libraryForm.addEventListener("submit", (e) => {
e.preventDefault();
const isValidUrl = (urlString) => {
var urlPattern = new RegExp(
"^(https?:\\/\\/)?" + // validate protocol
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // validate domain name
"((\\d{1,3}\\.){3}\\d{1,3}))" + // validate OR ip (v4) address
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // validate port and path
"(\\?[;&a-z\\d%_.~+=-]*)?" + // validate query string
"(\\#[-a-z\\d_]*)?$",
"i"
); // validate fragment locator
return !!urlPattern.test(urlString);
};
if (!isValidUrl(url.value)) {
alert("Invalid URL ");
url.value = "";
return;
}
const isValidIsbn = (subject) => {
subject = subject.replaceAll("-", "");
console.log(subject.length);
if (subject.length == 10) {
let sum = 0;
for (let i = 9; i >= 0; i--) {
sum += parseInt(subject[i], 10) * (i + 1);
}
if (sum % 11 == 0) {
return true;
} else {
return false;
}
} else if (subject.length == 13) {
subject = subject.replaceAll("-", "");
let sum = 0;
for (let i = 0; i < 13; i++) {
if (i % 2 == 0) {
sum += parseInt(subject[i], 10) * 1;
} else {
sum += parseInt(subject[i], 10) * 3;
}
}
console.log(sum);
if (sum % 10 == 0) {
return true;
} else {
return false;
}
} else {
return false;
}
};
if (!isValidIsbn(isbn.value)) {
alert("Invalid ISBN!");
return;
}
// Checking different types of books
if (fiction.checked) {
type = fiction.value;
programming.unchecked;
science.unchecked;
} else if (programming.checked) {
type = programming.value;
science.unchecked;
anime.unchecked;
fiction.unchecked;
} else if (science.checked) {
type = science.value;
fiction.unchecked;
programming.unchecked;
anime.unchecked;
} else if (anime.checked) {
type = anime.value;
fiction.unchecked;
programming.checked;
science.unchecked;
} else if (others.checked) {
type = newType.value ? newType.value : others.value;
fiction.unchecked;
programming.unchecked;
science.unchecked;
anime.unchecked;
} else {
type = "other";
}
let shelf = localStorage.getItem("shelfOfBooks");
let objOfBook; //object which stores books
let alreadyAdded = false;
// Check if the book is already in the library
if (shelf == null) {
objOfBook = [];
} else {
//We might have multiple books
objOfBook = JSON.parse(shelf); //By using JSON we convert it into Object
objOfBook.every((bookObj) => {
if (author === "") author = "Unknown";
let curBook = name === bookObj.book;
let curAuthor = author === bookObj.bookauthor;
let curBookType = type === bookObj.bookType;
if (curBook && curAuthor && curBookType) {
console.log("already added!");
alreadyAdded = true;
return false;
}
return true;
});
}
if (alreadyAdded === true) {
alreadyAddedMessage();
return;
} else {
// Book Name is mandatory field
if (name.value == "") {
errorMessage();
} else {
let myObj;
if (author.value != "") {
myObj = {
book: name.value,
bookauthor: author.value,
bookType: type,
bookurl: url.value == "" ? "/" : url.value,
bookisbn: isbn.value,
bookedition: edition.value,
bookpublication: publicationD.value,
readStatus: read.checked,
favorite: favorite.checked,
};
} else {
// Book Author not entered then set it to Unknown
myObj = {
book: name.value,
bookauthor: "Unknown",
bookType: type,
bookurl: url.value == "" ? "/" : url.value,
bookisbn: isbn.value,
bookedition: edition.value,
bookpublication: publicationD.value,
readStatus: read.checked,
favorite: favorite.checked,
};
}
if (editIndex != -1) {
objOfBook.splice(editIndex, 1, myObj);
addMessage(true);
editIndex = -1;
} else {
objOfBook.push(myObj);
addMessage();
UpdateBook();
}
localStorage.setItem("shelfOfBooks", JSON.stringify(objOfBook));
name.value = "";
author.value = "";
type = "";
isbn.value = "";
edition.value = "";
publicationD.value = "";
}
displayBooks();
}
});
function editBook(index) {
let bookDetails = JSON.parse(localStorage.getItem("shelfOfBooks"))[index];
console.log(bookDetails);
name.value = bookDetails.book;
author.value = bookDetails.bookauthor;
switch (bookDetails.bookType) {
case "other":
others.checked = true;
break;
case "fiction":
fiction.checked = true;
break;
case "programming":
programming.checked = true;
break;
case "science":
science.checked = true;
break;
}
editIndex = index;
name.focus();
}
//Function to show elements(books) from LocalStorage
function displayBooks() {
let books = localStorage.getItem("shelfOfBooks");
let clearBtn = document.getElementById("clear");
let objOfBook;
if (books == null) {
objOfBook = [];
} else {
objOfBook = JSON.parse(books);
}
let html = "";
let index = 0;
objOfBook.forEach((books) => {
//index is the length of the array
if (index == 0) {
html += `
<tr class="rows">
<th scope="row">1</th>
<td class="name"><a class="bookurl" href=${books.bookurl}> ${
books.book
} </a></td>
<td class="author">${books.bookauthor}</td>
<td class="type">${books.bookType}</td>
<td class="isbn">${books.bookisbn}</td>
<td class="edition">${books.bookedition}</td>
<td class="publicationdate">${books.bookpublication}</td>
<td class="type">${
books.readStatus
? `<label class="switch">
<input type="checkbox" checked disabled>
<span class="slider round"></span>
</label>`
: `<label class="switch">
<input type="checkbox" disabled>
<span class="slider round"></span>
</label>`
}</td>
<td class="fav">${
books.favorite
? `<label class="switch">
<input type="checkbox" checked disabled>
<span class="slider round"></span>
</label>`
: `<label class="switch">
<input type="checkbox" disabled>
<span class="slider round"></span>
</label>`
}</td>
<td class="icon"><i class="fa fa-times" aria-hidden="true" onclick="removeBook(${index})"></i></td>
<td class="icon"><i class="fa fa-edit" aria-hidden="true" onclick="editBook(${index})"></i></td>
</tr>
`;
} else {
html += `
<tr class="rows">
<th scope="row">${index + 1}</th>
<td class="name"><a class="bookurl" href=${books.bookurl}> ${
books.book
} </a></td>
<td class="author">${books.bookauthor}</td>
<td class="type">${books.bookType}</td>
<td class="isbn">${books.bookisbn}</td>
<td class="edition">${books.bookedition}</td>
<td class="publicationdate">${books.bookpublication}</td>
<td class="type">${
books.readStatus
? `<label class="switch">
<input type="checkbox" checked disabled>
<span class="slider round"></span>
</label>`
: `<label class="switch">
<input type="checkbox" disabled>
<span class="slider round"></span>
</label>`
}</td>
<td class="fav">${
books.favorite
? `<label class="switch">
<input type="checkbox" checked disabled>
<span class="slider round"></span>
</label>`
: `<label class="switch">
<input type="checkbox" disabled>
<span class="slider round"></span>
</label>`
}</td>
<td class="icon"><i class="fa fa-times" aria-hidden="true" onclick="removeBook(${index})"></i></td>
<td class="icon"><i class="fa fa-edit" aria-hidden="true" onclick="editBook(${index})"></i></td>
</tr>
`;
}
index++;
console.log("count " + index);
});
let table = document.getElementById("tableBody");
let noDisplayMsg = document.getElementById("emptyMsg");
if (objOfBook.length != 0) {
table.innerHTML = html;
clearBtn.style.display = "block";
noDisplayMsg.innerHTML = "";
} else {
noDisplayMsg.innerHTML = `Nothing to display! Use "Add book" above to add books`;
}
let libraryForm = document.getElementById("libraryForm");
libraryForm.reset();
}
//Show adding message
function addMessage(edited = false) {
let message = document.getElementById("message");
let navbar = document.getElementById("navbar");
navbar.style.display = "none";
message.innerHTML = !edited
? `<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Message:</strong> Your book has been successfully added.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`
: `<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Message:</strong> Your book has been successfully edited.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`;
setTimeout(() => {
navbar.style.display = "flex";
message.innerHTML = ``;
}, 2000);
}
//Show error message
function errorMessage() {
let message = document.getElementById("message");
let navbar = document.getElementById("navbar");
navbar.style.display = "none";
message.innerHTML = `<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Error:</strong> To add book, add name of book.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`;
setTimeout(() => {
navbar.style.display = "flex";
message.innerHTML = ``;
}, 2000);
}
//Show alreadyAdded message
function alreadyAddedMessage() {
let message = document.getElementById("message");
let navbar = document.getElementById("navbar");
navbar.style.display = "none";
message.innerHTML = `<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Error:</strong> Book already present!
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`;
// clear the library form
let libraryForm = document.getElementById("libraryForm");
libraryForm.reset();
setTimeout(() => {
navbar.style.display = "flex";
message.innerHTML = ``;
}, 2000);
}
//Show clear message
function clearMessage() {
let message = document.getElementById("message");
let navbar = document.getElementById("navbar");
navbar.style.display = "none";
message.innerHTML = `<div class="alert alert-info alert-dismissible fade show" role="alert">
<strong>Message:</strong> Your book shelf is clear! To add more books refresh the browser.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`;
setTimeout(() => {
navbar.style.display = "flex";
message.innerHTML = ``;
}, 2000);
}
// Refresh the page
function refreshPage() {
setTimeout(() => {
window.location.reload();
}, 2050);
}
// Update the display of books on deletion
function updateDisplayAfterDelete() {
localStorage.removeItem("shelfOfBooks");
localStorage.removeItem("getBookNumber");
document.getElementById("books").innerHTML = "No. of Books: " + 0;
let table = document.getElementById("tableBody");
table.style.display = "none";
clearBtn.style.display = "none";
let noDisplayMsg = document.getElementById("emptyMsg");
noDisplayMsg.innerHTML = `Nothing to display! Use "Add book" above to add books`;
clearMessage();
refreshPage();
}
// Clearning shelf (Deleting all books)
let clearBtn = document.getElementById("clear");
clearBtn.addEventListener("click", () => {
updateDisplayAfterDelete();
});
// Remove specific book from shelf
function removeBook(index) {
console.log("Delete book " + index);
// Decrementing in total number of books
let getBookNumber = localStorage.getItem("getBookNumber");
getBookNumber = parseInt(getBookNumber);
if (getBookNumber) {
localStorage.setItem("getBookNumber", getBookNumber - 1);
document.getElementById("books").innerHTML =
"No. of Books: " + (getBookNumber - 1);
} else {
localStorage.setItem("getBookNumber", 0);
document.getElementById("books").innerHTML = "No. of Books: 0" + 0;
}
// Removing book from shelf
let notes = localStorage.getItem("shelfOfBooks");
let objOfBook = [];
if (notes == null) {
objOfBook = [];
} else {
objOfBook = JSON.parse(notes);
}
if (getBookNumber == 1) {
updateDisplayAfterDelete();
} else {
objOfBook.splice(index, 1);
localStorage.setItem("shelfOfBooks", JSON.stringify(objOfBook));
displayBooks();
}
}
//Searching book by bookname, author and type
let searchNote = document.getElementById("searchText");
searchNote.addEventListener("input", function () {
let search = searchNote.value.toLowerCase();
let tableRows = document.getElementsByClassName("rows");
Array.from(tableRows).forEach(function (element) {
let bookName = element
.getElementsByClassName("name")[0]
.innerText.toLowerCase();
let authorName = element
.getElementsByClassName("author")[0]
.innerText.toLowerCase();
let type = element
.getElementsByClassName("type")[0]
.innerText.toLowerCase();
let isbnNo = element.getElementsByClassName("isbn")[0].innerText;
if (bookName.includes(search)) {
element.style.display = "table-row";
} else if (authorName.includes(search)) {
element.style.display = "table-row";
} else if (type.includes(search)) {
element.style.display = "table-row";
} else if (isbnNo.includes(search)) {
element.style.display = "table-row";
} else {
element.style.display = "none";
}
});
});
// Update Number of books in Shelf section
function UpdateBook() {
const bookNumber = parseInt(localStorage.getItem("getBookNumber"));
const booksCount = bookNumber ? bookNumber + 1 : 1;
const updateBooksCountSentence = `No. of books: ${booksCount ?? 1}`;
localStorage.setItem("getBookNumber", booksCount);
document.getElementById("books").innerHTML = updateBooksCountSentence;
}
//Show Number of books in Shelf section
const showNumberOfBooks = () => {
const getBookNumber = parseInt(localStorage.getItem("getBookNumber"));
document.getElementById("books").innerHTML = `No. of books: ${
getBookNumber || 0
}`;
};
// Filter books based on selected attributes from dropdown
let filterDropdown = document.getElementById("filter-books");
function filterBooks() {
let books = JSON.parse(localStorage.getItem("shelfOfBooks"));
// let numOfBooks = document.getElementById("books");
let emptyMsg = document.getElementById("emptyMsg");
let filterBy = filterDropdown.value;
let html = "";
let index = 0;
let filteredBooks;
if (filterBy === "all") {
filteredBooks = books.filter((book) => {
return (
book.book.toLowerCase().includes(searchNote.value.toLowerCase()) ||
book.bookauthor
.toLowerCase()
.includes(searchNote.value.toLowerCase()) ||
book.bookType.toLowerCase().includes(searchNote.value.toLowerCase())
);
});
} else {
filteredBooks = books.filter((book) => {
return book[filterBy]
.toLowerCase()
.includes(searchNote.value.toLowerCase());
});
}
if (filteredBooks.length > 0) {
filteredBooks.forEach((filteredBook) => {
html += `
<tr class="rows">
<th scope="row">${index + 1}</th>
<td class="name">${filteredBook.book}</td>
<td class="author">${filteredBook.bookauthor}</td>
<td class="type">${filteredBook.bookType}</td>
<td class="icon"><i class="fa fa-times" aria-hidden="true" onclick="removeBook(${index})"></i></td>
</tr>
`;
index++;
});
emptyMsg.innerHTML = "";
} else {
let bookAttr;
switch (filterBy) {
case "all":
bookAttr = "";
break;
case "book":
bookAttr = "name";
break;
case "bookauthor":
bookAttr = "author";
break;
case "bookType":
bookAttr = "type";
break;
}
emptyMsg.innerHTML = `No book ${
bookAttr !== "" ? "with" : ""
} ${bookAttr} "${searchNote.value}" found`;
}
// ? Does the number of books depends on the search results?
// numOfBooks.innerHTML = "No. of Books: " + filteredBooks.length;
let table = document.getElementById("tableBody");
table.innerHTML = html;
}
// filterDropdown.addEventListener("change", filterBooks);
searchNote.addEventListener("input", filterBooks);
showNumberOfBooks();
const radioButtons = document.querySelectorAll("input[type=radio]");
radioButtons.forEach((btn) => {
btn.addEventListener("click", (e) => {
const newType = document.getElementById("newType");
if (e.target.id === "other") {
newType.hidden = false;
} else {
newType.hidden = true;
}
});
});
// dark mode
var icon = document.querySelector("#icon");
var head1 = document.getElementById("subHead1");
var head2 = document.getElementById("subHead2");
var tables = document.getElementsByTagName("table");
icon.onclick = () => {
document.body.classList.toggle("dark-theme");
if (document.body.classList == "dark-theme") {
icon.innerHTML = `<i class='fas fa-sun'></i>`;
head1.style.color = "white";
head2.style.color = "white";
for (let i = 0; i < tables.length; i++) {
tables[i].style.color = "white";
}
} else {
icon.innerHTML = `<i class='fas fa-moon'></i>`;
head1.style.color = "black";
head2.style.color = "black";
for (let i = 0; i < tables.length; i++) {
tables[i].style.color = "black";
}
}
console.log(document.body.classList);
};
var acc = document.getElementsByClassName("accordion");
var i;
var len = acc.length;
for (i = 0; i < len; i++) {
acc[i].addEventListener("click", function () {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
//Get the button
let mybutton = document.getElementById("btn-back-to-top");
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function () {
scrollFunction();
};
function scrollFunction() {
if (
document.body.scrollTop > 20 ||
document.documentElement.scrollTop > 20
) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
mybutton.addEventListener("click", backToTop);
function backToTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}