Repository navigation
Expand file tree
/
Copy pathjavascript.js
More file actions
179 lines (136 loc) · 5.22 KB
/
Copy pathjavascript.js
File metadata and controls
179 lines (136 loc) · 5.22 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
document.addEventListener('DOMContentLoaded', function () {
const completeBookshelfList = [];
const RENDER_EVENT = 'render-toread';
const submitForm = document.getElementById('inputbook');
submitForm.addEventListener('submit', function (event) {
event.preventDefault();
addToread();
});
function addToread() {
const titleBook = document.getElementById('inputBookTitle').value;
const authorBook = document.getElementById('inputBookAuthor').value;
const yearBook = document.getElementById('inputBookYear').value;
const generatedID = generateId();
const toreadObject = generateToreadObject(generatedID, titleBook, authorBook, yearBook, false);
completeBookshelfList.push(toreadObject);
document.dispatchEvent(new Event(RENDER_EVENT));
saveData();
}
function generateId() {
return +new Date();
}
function generateToreadObject(id, titleBook, authorBook, yearBook, isCompleted) {
return {
id,
titleBook,
authorBook,
yearBook,
isCompleted
}
}
document.addEventListener(RENDER_EVENT, function () {
const uncompletedTOREADList = document.getElementById('incompleteBookshelfList');
uncompletedTOREADList.innerHTML = '';
const completedTOREADList = document.getElementById('completeBookshelfList');
completedTOREADList.innerHTML = '';
for (const toreadItem of completeBookshelfList) {
const toreadElement = makeToread(toreadItem);
if (!toreadItem.isCompleted)
uncompletedTOREADList.append(toreadElement);
else
completedTOREADList.append(toreadElement);
}
});
function makeToread(toreadObject) {
const textTitleBook = document.createElement('h3');
textTitleBook.innerText = toreadObject.titleBook;
const textAuthorBook = document.createElement('p');
textAuthorBook.innerText = toreadObject.authorBook;
const textYearBook = document.createElement('p');
textYearBook.innerText = toreadObject.yearBook;
const textbook_list = document.createElement('article');
textbook_list.classList.add('inner');
textbook_list.append(textTitleBook, textAuthorBook, textYearBook);
const book_list = document.createElement('article');
book_list.classList.add('item', 'shadow');
book_list.append(textbook_list);
book_list.setAttribute('id', `toread-${toreadObject.id}`);
if (toreadObject.isCompleted) {
const doneButton = document.createElement('button');
doneButton.classList.add('done-button');
doneButton.innerText = "Sudah Selesai Baca"
doneButton.addEventListener('click', function () {
addReadToCompleted(toreadObject.id);
});
book_list.append(doneButton);
const trashButton = document.createElement('button');
trashButton.classList.add('trash-button');
trashButton.addEventListener('click', function () {
removeTaskFromCompleted(todoObject.id);
});
container.append(undoButton, trashButton);
}
else {
const checkButton = document.createElement('button');
checkButton.classList.add('check-button');
checkButton.addEventListener('click', function () {
addTaskToCompleted(todoObject.id);
});
container.append(checkButton);
}
return container;
}
function addReadToCompleted (toreadId) {
const toreadTarget = findToread(toreadId);
if (todoTarget == null) return;
todoTarget.isCompleted = true;
document.dispatchEvent(new Event(RENDER_EVENT));
saveData();
}
function findTodo(todoId) {
for (const todoItem of todos) {
if (todoItem.id === todoId) {
return todoItem;
}
}
return null;
}
function removeTaskFromCompleted(todoId) {
const todoTarget = findTodoIndex(todoId);
if (todoTarget === -1) return;
todos.splice(todoTarget, 1);
document.dispatchEvent(new Event(RENDER_EVENT));
saveData();
}
function undoTaskFromCompleted(todoId) {
const todoTarget = findTodo(todoId);
if (todoTarget == null) return;
todoTarget.isCompleted = false;
document.dispatchEvent(new Event(RENDER_EVENT));
saveData();
}
function findTodoIndex(todoId) {
for (const index in todos) {
if (todos[index].id === todoId) {
return index;
}
}
return -1;
}
function loadDataFromStorage() {
const serializedData = localStorage.getItem(STORAGE_KEY);
let data = JSON.parse(serializedData);
if (data !== null) {
for (const todo of data) {
todos.push(todo);
}
}
document.dispatchEvent(new Event(RENDER_EVENT));
}
document.addEventListener('DOMContentLoaded', function () {
// ...
if (isStorageExist()) {
loadDataFromStorage();
}
});
});