forked from Abenezer-Tilahun/Awesome-books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
110 lines (88 loc) · 2.77 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
// Initialize the books
let books;
// create the book class
class UpdateDisplay {
constructor(author, title) {
this.title = title;
this.author = author;
}
static listSection = document.querySelector('.list-section');
static bookTitle = document.querySelector('#title');
static formBtn = document.querySelector('.btn-submit');
static bookAuthor = document.querySelector('#author');
static listBtn = document.querySelectorAll('.listBtn');
static addActive= document.querySelectorAll('.sec');
// create new book
static addBooks() {
const bookItem = new UpdateDisplay(
UpdateDisplay.bookTitle.value,
UpdateDisplay.bookAuthor.value,
);
books.push(bookItem);
localStorage.setItem('books', JSON.stringify(books));
UpdateDisplay.bookAuthor.value = '';
UpdateDisplay.bookTitle.value = '';
UpdateDisplay.addBookItem(bookItem, books.length - 1);
}
static delBook(bookItem, pos) {
const bookBlock = document.getElementById(pos);
books = books.filter((item) => item !== bookItem);
localStorage.setItem('books', JSON.stringify(books));
UpdateDisplay.listSection.removeChild(bookBlock);
}
static updateUi() {
if (localStorage.getItem('books')) {
books = JSON.parse(localStorage.getItem('books'));
books.forEach((bookItem, pos) => {
UpdateDisplay.addBookItem(bookItem, pos);
});
} else {
localStorage.setItem('books', '');
books = [];
}
}
static addBookItem(bookItem, pos) {
const bookBlock = document.createElement('div');
bookBlock.classList.add('bookBlock');
bookBlock.id = pos;
const removeBtn = document.createElement('button');
removeBtn.classList.add('remove-btn');
bookBlock.innerHTML = `
<p class='book-title'>'${bookItem.author}' by ${bookItem.title} </p>`;
removeBtn.innerText = 'Remove';
removeBtn.onclick = () => {
UpdateDisplay.delBook(bookItem, pos);
};
bookBlock.appendChild(removeBtn);
UpdateDisplay.listSection.appendChild(bookBlock);
}
}
UpdateDisplay.listBtn.forEach((btn, i) => {
btn.onclick = () => {
UpdateDisplay.addActive.forEach((sec, index) => {
if (i === index) {
sec.classList.add('active');
} else {
sec.classList.remove('active');
}
});
};
});
const timeBox = document.querySelector('.date_time');
function time() {
const date = new Date();
const locale = navigator.language;
const options = {
month: 'long',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: 'false',
};
timeBox.textContent = `${date.toLocaleTimeString(locale, options)}`;
}
setInterval(time, 1000);
UpdateDisplay.updateUi();
UpdateDisplay.formBtn.addEventListener('click', UpdateDisplay.addBooks);