-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
219 lines (183 loc) · 5.87 KB
/
javascript.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
const books = [];
const bookChanged = "render-book";
const SAVED_EVENT = "saved-book";
const STORAGE_KEY = "BOOKSHELF_APPS";
document.addEventListener("DOMContentLoaded", function () {
const submitForm = document.getElementById("inputBuku");
submitForm.addEventListener("submit", function (event) {
event.preventDefault();
addBook();
});
});
// membuat ID
function generateId() {
return +new Date();
}
// membuat object
function generateBookObject(id, title, author, year, isComplete) {
return {
id,
title,
author,
year: Number(year),
isComplete,
};
}
// menambahkan buku
function addBook() {
const inputBukuTitle = document.getElementById("inputBukuTitle").value;
const inputBukuAuthor = document.getElementById("inputBukuAuthor").value;
const inputBukuYear = document.getElementById("inputBukuYear").value;
const inputBukuIsComplete = document.getElementById("inputBukuIsComplete");
if (inputBukuIsComplete.checked == true) {
const book = generateBookObject(generateId(), inputBukuTitle, inputBukuAuthor, inputBukuYear, true);
books.push(book);
} else {
const book = generateBookObject(generateId(), inputBukuTitle, inputBukuAuthor, inputBukuYear, false);
books.push(book);
}
document.dispatchEvent(new Event(bookChanged));
saveData();
}
// membuat Item Book
function makeBook(bookObject) {
const textInputBukuTitle = document.createElement("h2");
textInputBukuTitle.innerText = bookObject.title;
const textInputBukuAuthor = document.createElement("p");
textInputBukuAuthor.innerText = "Penulis : " + bookObject.author;
const textInputBukuYear = document.createElement("p");
textInputBukuYear.innerText = "Tahun : " + bookObject.year;
const textContainer = document.createElement("div");
textContainer.classList.add("inner");
textContainer.append(textInputBukuTitle, textInputBukuAuthor, textInputBukuYear);
const container = document.createElement("div");
container.classList.add("item");
container.append(textContainer);
container.setAttribute("id", `book-${bookObject.id}`);
if (bookObject.isComplete) {
const undoButton = document.createElement("button");
undoButton.classList.add("undo-button");
undoButton.addEventListener("click", function () {
undoTask(bookObject.id);
});
const sampahButton = document.createElement("button");
sampahButton.classList.add("sampah-button");
sampahButton.addEventListener("click", function () {
deleteTask(bookObject.id);
});
container.append(undoButton, sampahButton);
} else {
const cekButton = document.createElement("button");
cekButton.classList.add("cek-button");
cekButton.addEventListener("click", function () {
addTaskToComplete(bookObject.id);
});
const sampahButton = document.createElement("button");
sampahButton.classList.add("sampah-button");
sampahButton.addEventListener("click", function () {
deleteTask(bookObject.id);
});
container.append(cekButton, sampahButton);
}
return container;
}
// menampilkan Buku yang tersimpan pada Array.
document.addEventListener(bookChanged, function () {
const incompleteBookshelfList = document.getElementById("incompleteBookshelfList");
incompleteBookshelfList.innerHTML = "";
const completeBookshelfList = document.getElementById("completeBookshelfList");
completeBookshelfList.innerHTML = "";
for (bookItem of books) {
const bookElement = makeBook(bookItem);
if (bookItem.isComplete == false) incompleteBookshelfList.append(bookElement);
else completeBookshelfList.append(bookElement);
}
console.log(books);
});
// menghapus buku
function deleteTask(bookId) {
const bookTarget = findBookIndex(bookId);
if (bookTarget === -1) return;
books.splice(bookTarget, 1);
document.dispatchEvent(new Event(bookChanged));
saveData();
}
// mengembalikan buku
function undoTask(bookId) {
const bookTarget = findBook(bookId);
if (bookTarget == null) return;
bookTarget.isComplete = false;
document.dispatchEvent(new Event(bookChanged));
saveData();
}
function findBookIndex(bookId) {
for (index in books) {
if (books[index].id === bookId) {
return index;
}
}
return -1;
}
// cek button
function addTaskToComplete(bookId) {
const bookTarget = findBook(bookId);
if (bookTarget == null) return;
bookTarget.isComplete = true;
document.dispatchEvent(new Event(bookChanged));
saveData();
}
function findBook(bookId) {
for (bookItem of books) {
if (bookItem.id === bookId) {
return bookItem;
}
}
return null;
}
// search buku
function searchBook() {
const searchBookTitle = document.getElementById("searchBookTitle");
const filterBook = searchBookTitle.value.toLowerCase();
const bookItem = document.getElementsByClassName("item");
for (let i = 0; i < bookItem.length; i++) {
const title = bookItem[i].getElementsByTagName("h2")[0];
if (title.innerHTML.toLowerCase().indexOf(filterBook) > -1) {
bookItem[i].style.display = "";
} else {
bookItem[i].style.display = "none";
}
}
}
// penyimpanan data
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("Maaf, browser kamu tidak mendukung local storage!");
return false;
}
return true;
}
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 (book of data) {
books.push(book);
}
}
document.dispatchEvent(new Event(bookChanged));
}
document.addEventListener("DOMContentLoaded", function () {
if (isStorageExist()) {
loadDataFromStorage();
}
});