-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
287 lines (237 loc) · 7.53 KB
/
main.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
const books = [];
const EVENT_CHANGE = "change-books";
const SAVED_EVENT = "saved-books";
const STORAGE_KEY = "BOOKSELF_APPS";
document.addEventListener("DOMContentLoaded", function () {
const submitForm = document.getElementById("form");
submitForm.addEventListener("submit", function (e) {
e.preventDefault();
addBook();
alert("Buku Ditambahkan");
e.target.reset();
});
if (isStorageExist()) {
loadDataFromStorage();
}
});
function saveData() {
if (isStorageExist()) {
const parsed = JSON.stringify(books);
localStorage.setItem(STORAGE_KEY, parsed);
document.dispatchEvent(new Event(SAVED_EVENT));
}
}
function isStorageExist() {
if (typeof Storage === undefined) {
alert("Browser kamu tidak mendukung local storage");
return false;
}
return true;
}
function addBook() {
const inputTitle = document.getElementById("title").value;
const inputAuthor = document.getElementById("author").value;
const inputYear = document.getElementById("date").value;
const generatedID = generateId();
const newBook = generateNewBook(generatedID, inputTitle, inputAuthor, inputYear, false);
books.push(newBook);
document.dispatchEvent(new Event(EVENT_CHANGE));
saveData();
}
function generateId() {
return +new Date();
}
function generateNewBook(id, bookTitle, inputAuthor, inputYear, isReaded) {
return {
id,
bookTitle,
inputAuthor,
inputYear,
isReaded,
};
}
function makeBook(newBook) {
const bookTitle = document.createElement("h2");
bookTitle.innerText = newBook.bookTitle;
const authorName = document.createElement("p");
authorName.innerText = newBook.inputAuthor;
const bookYear = document.createElement("p");
bookYear.innerText = newBook.inputYear;
const textContainer = document.createElement("div");
textContainer.classList.add("inner");
textContainer.append(bookTitle, authorName, bookYear);
const container = document.createElement("div");
container.classList.add("item", "list-item", "shadow");
container.append(textContainer);
container.setAttribute("id", `book-${newBook.id}`);
if (newBook.isReaded) {
const undoButton = document.createElement("img");
undoButton.setAttribute("src", "assets/undo-outline.svg");
undoButton.classList.add("undo-button");
undoButton.addEventListener("click", function () {
if (confirm("Anda Yakin Mengembalikan Buku ke Daftar Belum DIbaca?")) {
undoBookTitleFromReaded(newBook.id);
alert("Buku Dikembalikan ke List Belum Dibaca");
} else {
}
});
const trashButton = document.createElement("button");
trashButton.classList.add("trash-button");
trashButton.addEventListener("click", function () {
if (confirm("Anda Yakin Menghapus Buku Dari Bookshelf?")) {
removeBookTitleFromReaded(newBook.id);
alert("Buku Dihapus Dari Bookshelf");
} else {
}
});
container.append(undoButton, trashButton);
} else {
const checkButton = document.createElement("button");
checkButton.classList.add("check-button");
checkButton.addEventListener("click", function () {
if (confirm("Anda Yakin Buku Telah Dibaca?")) {
addBookTitleToReadList(newBook.id);
alert("Buku Dipindahkan ke List Sudah Dibaca");
} else {
}
});
const trashButton = document.createElement("button");
trashButton.classList.add("trash-button");
trashButton.addEventListener("click", function () {
if (confirm("Anda Yakin Menghapus Buku Dari Bookshelf?")) {
removeBookTitleFromReaded(newBook.id);
alert("Buku Dihapus Dari Bookshelf");
} else {
}
});
container.append(checkButton, trashButton);
}
return container;
}
document.addEventListener(EVENT_CHANGE, function () {
const list = books.length;
const read = [];
const unRead = [];
const unReadBooksList = document.getElementById("books");
unReadBooksList.innerHTML = "";
const readBookList = document.getElementById("books-items");
readBookList.innerHTML = "";
const unReadBook = document.getElementById("unread-book");
unReadBook.innerText = "";
const readBook = document.getElementById("read-book");
readBook.innerText = "";
for (const bookItem of books) {
const bookList = makeBook(bookItem);
if (bookItem.isReaded) {
readBookList.append(bookList);
read.push(readBookList);
readBook.innerText = read.length;
} else {
unReadBooksList.append(bookList);
unRead.push(bookList);
unReadBook.innerText = unRead.length;
}
}
ifNoList();
totalOfBooks();
});
function ifNoList() {
const list = books.length;
const container = document.querySelector(".no-list");
if (list == 0) {
container.classList.add("picture");
} else {
container.classList.remove("picture");
}
}
const resetList = document.getElementById("btn-reset");
resetList.addEventListener("click", function () {
const reset = books.length;
if (reset == 0) {
alert("List Kosong");
} else {
if (confirm("Anda Yakin Menghapus Semua List?")) {
resetBookList(reset);
alert("Semua List Dihapus");
}
}
});
function totalOfBooks() {
const totalBooks = document.getElementById("total-books");
totalBooks.innerHTML = books.length;
}
function addBookTitleToReadList(bookId) {
const bookTarget = findBook(bookId);
if (bookTarget == null) return;
bookTarget.isReaded = true;
document.dispatchEvent(new Event(EVENT_CHANGE));
saveData();
}
document.getElementById("bookTitle").addEventListener("keyup", function () {
const inputValue = document.getElementById("bookTitle").value;
const listBooks = document.querySelectorAll(".list-item");
for (let i = 0; i < listBooks.length; i++) {
if (!inputValue || listBooks[i].textContent.toLowerCase().indexOf(inputValue) > -1) {
listBooks[i].classList.remove("hide");
} else {
listBooks[i].classList.add("hide");
}
}
});
function findBook(bookId) {
for (const todoItem of books) {
if (todoItem.id === bookId) {
return todoItem;
}
}
return null;
}
function removeBookTitleFromReaded(bookId) {
const bookTarget = findBookIndex(bookId);
if (bookTarget === -1) return;
books.splice(bookTarget, 1);
document.dispatchEvent(new Event(EVENT_CHANGE));
saveData();
}
function resetBookList(newBook) {
const resetAll = bookIndex(newBook);
if (resetAll) return;
books.splice(resetAll);
document.dispatchEvent(new Event(EVENT_CHANGE));
saveData();
}
function undoBookTitleFromReaded(bookId) {
const bookTarget = findBook(bookId);
if (bookTarget == null) return;
bookTarget.isReaded = false;
document.dispatchEvent(new Event(EVENT_CHANGE));
saveData();
}
function findBookIndex(bookId) {
for (const index in books) {
if (books[index].id === bookId) {
return index;
}
}
return -1;
}
function bookIndex(newBook) {
for (const index in books) {
if (books[index] === newBook) {
return index;
}
}
}
document.addEventListener(SAVED_EVENT, function () {
console.log(localStorage.getItem(STORAGE_KEY));
});
function loadDataFromStorage() {
const serializedData = localStorage.getItem(STORAGE_KEY);
let data = JSON.parse(serializedData);
if (data !== null) {
for (const book of data) {
books.push(book);
}
}
document.dispatchEvent(new Event(EVENT_CHANGE));
}