-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
275 lines (212 loc) · 7.89 KB
/
script.js
File metadata and controls
275 lines (212 loc) · 7.89 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
const books = [] // Array untuk menyimpan data buku
const generateBookList = ('generateBookList'); // Costum Event
const SAVED_EVENT = 'saved_book'; // Costum Event
const STORAGE_KEY = 'BUKUKU'; // Nama key di local storage
// Membuat ID dengan fungsi new Date,
// agar mendapatkan angka unik untuk setiap list buku.
function generateId() {
return +new Date();
}
// Fungsi untuk membuat data buku, yang di simpan dalam object.
function generateBookObject(id, title, author, year, isCompleted) {
return {
id,
title,
author,
year,
isCompleted
}
}
// fungsi untuk mencari buku yang sesuai dengan ID di array Books
function findBook(bookId) {
for (const bookItem of books) {
if (bookItem.id === bookId) {
return bookItem;
}
}
return null;
}
// Fungsi untuk mendapatkan nilai index dari array books
function findBookIndex(bookId) {
for (const i in books) {
if (books[i].id === bookId) {
return i;
}
}
return -1;
}
// Fungsi untuk memriksa local storage suport di browser.
function isStorageExist(){
if (typeof (Storage) === undefined) {
alert('Browser kamu tidak mendukung local storage');
return false;
}
return true;
}
// Fungsi untuk menyimpan data ke local storage.
function saveData() {
if (isStorageExist()) {
const parsed = JSON.stringify(books); //untuk mengubah data object mmenjadi string
localStorage.setItem(STORAGE_KEY, parsed);
document.dispatchEvent(new Event(SAVED_EVENT));
}
}
// Fungsi untuk memuat data dari local storage ke variabel books
function loadDataFromStorage() {
const serializedData /* string */ = localStorage.getItem(STORAGE_KEY);
let data = JSON.parse(serializedData);
if (data !== null) {
for (const book of data) {
books.push(book);
}
}
document.dispatchEvent(new Event(generateBookList));
}
// Fungsi untuk menambahkan list buku baru
function addBook() {
const bookTitle = document.getElementById('inputBookTitle').value;
const bookAuthor = document.getElementById('inputBookAuthor').value;
const bookYear = document.getElementById('inputBookYear').value;
const checkButton = document.querySelector('#inputBookIsComplete').checked;
const generatedID = generateId();
const bookObject = generateBookObject(generatedID, bookTitle, bookAuthor,bookYear, checkButton);
books.push(bookObject);
document.dispatchEvent(new Event(generateBookList));
saveData();
}
// Fungsi untuk membuat elemen - elemen yang akan menampilkan informasi buku di dalam list
function makeBook(bookObject){
const {id, title, author, year, isCompleted} = bookObject;
const titleText = document.createElement('h3');
titleText.innerText = bookObject.title;
const textAuthor = document.createElement('p');
textAuthor.innerText = `Penulis : ${bookObject.author}`;
const textYear = document.createElement('p');
textYear.innerText = `Tahun : ${bookObject.year}`;
const bookContainer = document.createElement('article');
bookContainer.classList.add('book_item');
bookContainer.append(titleText, textAuthor, textYear);
bookContainer.setAttribute('id', `book-${bookObject.id}`);
if (isCompleted) {
const undoButton = document.createElement('button');
undoButton.innerText = 'Belum Selesai di Baca';
undoButton.classList.add('green');
undoButton.addEventListener('click', function () {
undoBookFromCompleted(id);
});
const trashButton = document.createElement('button');
trashButton.innerHTML = '<i class="fa-solid fa-trash"></i>';
trashButton.classList.add('red');
trashButton.addEventListener('click', function () {
Swal.fire({
title: 'Hapus Buku?',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Iya, hapus',
cancelButtonText: 'Batal'
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Dihapus!',
'Buku berhasil di hapus.',
'success'
)
removeBookFromCompleted(id);
}
})
});
bookContainer.append(undoButton, trashButton);
} else {
const checkButton = document.createElement('button');
checkButton.innerText = 'Selesai';
checkButton.classList.add('green');
checkButton.addEventListener('click', function () {
addBookToCompleted(id);
});
const trashButton = document.createElement('button');
trashButton.innerHTML = '<i class="fa-solid fa-trash"></i>';
trashButton.classList.add('red');
trashButton.addEventListener('click', function () {
Swal.fire({
title: 'Hapus Buku?',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Iya, hapus',
cancelButtonText: 'Batal'
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Dihapus!',
'Buku berhasil di hapus.',
'success'
)
removeBookFromCompleted(id);
}
})
});
bookContainer.append(checkButton, trashButton);
}
return bookContainer;
}
// Menambahkan buku ke dalam list yang sudah di baca
function addBookToCompleted(bookId) {
const bookTarget = findBook(bookId);
if (bookTarget == null) return;
bookTarget.isCompleted = true;
document.dispatchEvent(new Event(generateBookList));
saveData();
}
// Fungsi untuk menghapus buku dari list
function removeBookFromCompleted(bookId) {
const bookTarget = findBookIndex(bookId);
if (bookTarget === -1) return;
books.splice(bookTarget, 1);
document.dispatchEvent(new Event(generateBookList));
saveData();
}
// Fungsi untuk mengembalikan buku dari sudah di baca ke rak belum selesai di baca
function undoBookFromCompleted(bookId) {
const bookTarget = findBook(bookId);
if (bookTarget == null) return;
bookTarget.isCompleted = false;
document.dispatchEvent(new Event(generateBookList));
saveData();
}
// Fungsi untuk mendapatkan nilai ketika form di submmit dan,
// menjalankan fungsi untuk menambahkan buku baru
document.addEventListener('DOMContentLoaded', function () {
const submitForm = document.getElementById('inputBook');
submitForm.addEventListener('submit', function (event) {
Swal.fire({
title: 'Berhasil Menambahkan Buku',
icon: 'success',
confirmButtonText: 'Oke'
});
event.preventDefault();
addBook();
});
if (isStorageExist()) {
loadDataFromStorage();
}
});
document.addEventListener(SAVED_EVENT, () => {
});
// Costum event untuk menampilkan buku ke list
document.addEventListener(generateBookList, function () {
const incompleteBookshelfList = document.getElementById('incompleteBookshelfList');
const completeBookshelfList = document.getElementById('completeBookshelfList');
incompleteBookshelfList.innerText = '';
completeBookshelfList.innerText = '';
for (const bookItem of books) {
const bookElement = makeBook(bookItem);
if (bookItem.isCompleted) {
completeBookshelfList.append(bookElement);
}else {
incompleteBookshelfList.append(bookElement);
}
}
});