-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
295 lines (251 loc) · 8.16 KB
/
Copy pathscript.js
File metadata and controls
295 lines (251 loc) · 8.16 KB
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
const books = [];
const SUBMIT_EVENT = "submit-book";
const submitForm = document.getElementById("inputBook");
const submitSearchForm = document.getElementById("searchBook");
const SAVED_EVENT = "saved-book";
const STORAGE_KEY = "BOOKSHELFS";
document.addEventListener("DOMContentLoaded", function () {
bookIsCompleteCheckBox();
submitSearchForm.addEventListener("submit", function (event) {
event.preventDefault();
searchButton();
});
submitForm.addEventListener("submit", function (event) {
event.preventDefault();
addBook();
});
if (isStorageExist()) {
loadStorage();
}
});
function searchButton() {
const inputSearch = document
.getElementById("searchBookInput")
.value.toLowerCase();
for (const book of books) {
const bookItem = document.getElementById(`book-${book.id}`);
const isVisible =
book.title.toLowerCase().includes(inputSearch) ||
book.author.toLowerCase().includes(inputSearch) ||
book.year.includes(inputSearch);
bookItem.classList.toggle("hide", !isVisible);
}
}
function isStorageExist() {
if (typeof Storage === undefined) {
alert("Browser yang anda pakai tidak mendukung local storage.");
return false;
}
return true;
}
function saveToStorage() {
if (isStorageExist()) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(books));
document.dispatchEvent(new Event(SAVED_EVENT));
}
}
function loadStorage() {
let data = JSON.parse(localStorage.getItem(STORAGE_KEY));
if (data !== null) {
for (const book of data) {
books.push(book);
}
}
document.dispatchEvent(new Event(SUBMIT_EVENT));
}
function generateBookData(id, title, author, year, isComplete) {
return {
id,
title,
author,
year,
isComplete,
};
}
function newId() {
return +new Date();
}
function addBook() {
const newID = newId();
const bookTitle = document.getElementById("inputBookTitle").value;
const authorName = document.getElementById("inputBookAuthor").value;
const bookYear = document.getElementById("inputBookYear").value;
const isBookComplete = document.getElementById("inputBookIsComplete").value;
if (isBookComplete === "no") {
const isCompleted = false;
const newBook = generateBookData(
newID,
bookTitle,
authorName,
bookYear,
isCompleted
);
books.push(newBook);
} else {
const isCompleted = true;
const newBook = generateBookData(
newID,
bookTitle,
authorName,
bookYear,
isCompleted
);
books.push(newBook);
}
document.dispatchEvent(new Event(SUBMIT_EVENT));
saveToStorage();
}
function putBook(bookData) {
const { id, title, author, year, isComplete } = bookData;
const titleOfBook = document.createElement("h3");
titleOfBook.innerText = title;
const nameOfAuthor = document.createElement("p");
nameOfAuthor.innerText = `Penulis : ${author}`;
const yearOfBook = document.createElement("p");
yearOfBook.innerText = `Tahun : ${year}`;
const bookContainer = document.createElement("div");
bookContainer.classList.add("action");
bookContainer.setAttribute("id", "actions");
const container = document.createElement("div");
container.classList.add("book_item");
container.append(titleOfBook, nameOfAuthor, yearOfBook, bookContainer);
container.setAttribute("id", `book-${id}`);
if (isComplete) {
const undoButton = document.createElement("button");
undoButton.classList.add("undobtn");
undoButton.innerText = "Belum Dibaca";
undoButton.addEventListener("click", function () {
undoBookFromCompletedShelf(id);
});
const removeButton = document.createElement("button");
removeButton.classList.add("removebtn");
removeButton.innerText = "Hapus Buku";
removeButton.addEventListener("click", function () {
removeBook(id);
});
const editButton = document.createElement("button");
const inputFormTitle = document.getElementById("input_section_title");
editButton.classList.add("editbtn");
editButton.innerText = "Edit Buku";
editButton.addEventListener("click", function () {
editBook(id);
});
inputFormTitle.innerText = "Masukkan Buku Baru";
bookContainer.append(undoButton, removeButton, editButton);
} else {
const checkButton = document.createElement("button");
checkButton.classList.add("checkbtn");
checkButton.innerText = "Selesai Dibaca";
checkButton.addEventListener("click", function () {
addBookToCompletedShelf(id);
});
const removeButton = document.createElement("button");
removeButton.classList.add("removebtn");
removeButton.innerText = "Hapus Buku";
removeButton.addEventListener("click", function () {
removeBook(id);
});
const editButton = document.createElement("button");
const inputFormTitle = document.getElementById("input_section_title");
editButton.classList.add("editbtn");
editButton.innerText = "Edit Buku";
editButton.addEventListener("click", function () {
editBook(id);
});
inputFormTitle.innerText = "Masukkan Buku Baru";
bookContainer.append(checkButton, removeButton, editButton);
}
return container;
}
function bookIsCompleteCheckBox() {
const isBookComplete = document.getElementById("inputBookIsComplete");
const changeSubmitText = document.getElementById("submitText");
isBookComplete.addEventListener("click", () => {
if (isBookComplete.value === "no") {
isBookComplete.value = "yes";
changeSubmitText.innerText = "Selesai dibaca";
} else if (isBookComplete.value === "yes") {
isBookComplete.value = "no";
changeSubmitText.innerText = "Belum selesai dibaca";
}
});
}
function findBook(id) {
for (const book of books) {
if (id == book.id) {
return book;
}
}
return null;
}
function findBookIndex(id) {
for (const i in books) {
if (id == books[i].id) {
return i;
}
}
}
function addBookToCompletedShelf(id) {
const foundBook = findBook(id);
if (foundBook == null) return;
foundBook.isComplete = true;
document.dispatchEvent(new Event(SUBMIT_EVENT));
saveToStorage();
}
function undoBookFromCompletedShelf(id) {
const foundBook = findBook(id);
if (foundBook == null) return;
foundBook.isComplete = false;
document.dispatchEvent(new Event(SUBMIT_EVENT));
saveToStorage();
}
function removeBook(id) {
const foundBook = findBookIndex(id);
const bookTitle = findBook(id);
const confirmation = confirm(
`Buku "${bookTitle.title}" akan hilang selamanya setelah dihapus. Apakah anda yakin menghapusnya?`
);
if (confirmation) {
books.splice(foundBook, 1);
alert("Hapus telah berhasil");
}
if (foundBook === -1) return;
document.dispatchEvent(new Event(SUBMIT_EVENT));
saveToStorage();
}
function editBook(id) {
const foundBook = findBook(id);
const bookIndex = findBookIndex(id);
const bookTitle = document.getElementById("inputBookTitle");
const authorName = document.getElementById("inputBookAuthor");
const bookYear = document.getElementById("inputBookYear");
const inputFormTitle = document.getElementById("input_section_title");
if (foundBook == null) {
return null;
} else {
bookTitle.value = foundBook.title;
authorName.value = foundBook.author;
bookYear.value = foundBook.year;
inputFormTitle.innerText = `Edit Buku "${foundBook.title}"`;
books.splice(bookIndex, 1);
if (findBookIndex === -1) return;
}
}
document.addEventListener(SUBMIT_EVENT, function () {
const completeBookshelfList = document.getElementById(
"completeBookshelfList"
);
const incompleteBookshelfList = document.getElementById(
"incompleteBookshelfList"
);
completeBookshelfList.innerHTML = "";
incompleteBookshelfList.innerHTML = "";
for (const book of books) {
const bookObject = putBook(book);
if (book.isComplete) {
completeBookshelfList.append(bookObject);
} else {
incompleteBookshelfList.append(bookObject);
}
}
});