-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
242 lines (218 loc) · 8.28 KB
/
index.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
var bookList;
var current_list;
var dark;
var choosen;
window.onload = function () {
bookList = [];
current_list = [];
dark = false;
choosen = false;
getJsonObject('data.json',
function (data) {
bookList = data; // store the book list into bookList
current_list = bookList;
// console.log(bookList); // print it into console (developer tools)
// console.log(bookList[0]); // print the first book object into console
// here you can call methods to load or refresh the page
// loadBooks() or refreshPage()
loadBooks(bookList);
},
function (xhr) {
console.error(xhr);
}
);
var search = document.getElementById("search_button");
search.onclick = function () {
var input = document.getElementById("search").value;
var found = false;
if (input != "") {
removeHighlight();
for (let i = 0; i < current_list.length; i++) {
var book = current_list[i];
if (book.title.toLowerCase().includes(input.toLowerCase())) {
var element = document.getElementById(book.title);
element.style.backgroundColor = "green";
found = true;
}
}
if (!found) {
removeHighlight();
alert("No book found!");
}
} else {
removeHighlight();
}
}
var filter = document.getElementById("filter_button");
filter.onclick = function () {
var category_input = document.getElementById("category").value;
var hasBooks = false;
if (category_input != "Category") {
current_list = [];
for (let i = 0; i < bookList.length; i++) {
if (bookList[i].category == category_input) {
current_list.push(bookList[i]);
hasBooks = true;
}
}
document.getElementById("list_body").innerHTML = "";
loadBooks(current_list);
if (!hasBooks) {
alert("No book in this category!")
}
} else {
document.getElementById("list_body").innerHTML = "";
loadBooks(bookList);
current_list = bookList;
}
var input = document.getElementById("search").value;
if (input != "") {
removeHighlight();
for (let i = 0; i < current_list.length; i++) {
var book = current_list[i];
if (book.title.toLowerCase().includes(input.toLowerCase())) {
var element = document.getElementById(book.title);
element.style.backgroundColor = "green";
// console.log(element);
}
}
} else {
removeHighlight();
}
}
document.getElementById("dark_checkbox").onclick = function () {
var element = document.body;
element.classList.toggle("dark-mode");
dark = !dark;
var li = document.getElementById("list_body").getElementsByTagName("tr");
for (let index = 0; index < li.length; index++) {
var element = li[index];
if (element.style.backgroundColor != "green") {
element.style.backgroundColor = dark ? "#DEA254" : "#E3E3E3";
}
}
}
document.getElementById("add_button").onclick = function () {
var checkboxes = document.getElementsByClassName("book_checkbox");
choosen = false;
[...checkboxes].forEach(elem => {
if (elem.checked) {
choosen = true;
}
})
if (!choosen) {
alert("Please choose one book to add!");
} else {
var quantity = prompt("Please enter the quantity:", "1");
if (quantity != null && !isNaN(quantity) && quantity >= 1 && quantity % 1 === 0) {
var cart_quantity = parseInt(document.getElementById("cart_quantity").innerHTML);
cart_quantity = parseInt(cart_quantity) + parseInt(quantity);
document.getElementById("cart_quantity").innerHTML = cart_quantity;
[...checkboxes].forEach(elem => {
elem.checked = false;
})
choosen = false;
} else {
alert("Please enter a valid number and click confirm!");
}
}
}
document.getElementById("reset_button").onclick = function () {
var reset_confirm = confirm("Is it okay to reset the cart?");
if (reset_confirm) {
var cart_quantity = parseInt(document.getElementById("cart_quantity").innerHTML);
cart_quantity = 0;
document.getElementById("cart_quantity").innerHTML = cart_quantity;
}
}
}
function getJsonObject(path, success, error) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
if (success) success(JSON.parse(xhr.responseText));
} else {
if (error) error(xhr);
}
}
};
xhr.open("GET", path, true);
xhr.send();
}
function loadBooks(list) {
// load books from bookList
var list_area = document.getElementById("list_body");
// console.log(list);
for (var i = 0; i < list.length; i++) {
var book = list[i];
var row = document.createElement("tr");
var checkbox = document.createElement("td");
var image = document.createElement("td");
var title = document.createElement("td");
var star = document.createElement("td");
star.setAttribute("class", "rate");
var author = document.createElement("td");
var year = document.createElement("td");
var price = document.createElement("td");
var year = document.createElement("td");
var publisher = document.createElement("td");
var category = document.createElement("td");
var checkboxInput = document.createElement("input");
checkboxInput.setAttribute("type", "checkbox");
checkboxInput.setAttribute("name", "book");
checkboxInput.setAttribute("value", book.title);
checkboxInput.setAttribute("class", "book_checkbox");
checkbox.appendChild(checkboxInput);
var img = document.createElement("img");
img.setAttribute("src", book.img);
img.setAttribute("alt", book.title);
img.setAttribute("width", "70");
image.appendChild(img);
title.innerHTML = book.title;
var rate = book.rating;
for (var j = 0; j < rate; j++) {
var starImg = document.createElement("img");
starImg.setAttribute("src", "images/star-16.ico");
star.appendChild(starImg);
}
for (var j = 0; j < 5 - rate; j++) {
var emptyStarImg = document.createElement("img");
emptyStarImg.setAttribute("src", "images/outline-star-16.ico");
star.appendChild(emptyStarImg);
}
author.innerHTML = book.authors;
year.innerHTML = book.year;
price.innerHTML = book.price;
publisher.innerHTML = book.publisher;
category.innerHTML = book.category;
row.appendChild(checkbox);
row.appendChild(image);
row.appendChild(title);
row.appendChild(star);
row.appendChild(author);
row.appendChild(year);
row.appendChild(price);
row.appendChild(publisher);
row.appendChild(category);
row.setAttribute("id", book.title);
list_area.appendChild(row);
}
let checkboxes = document.getElementsByClassName("book_checkbox");
for (let i = 0; i < checkboxes.length; i++) {
let book = checkboxes[i];
book.onclick = function () {
[...checkboxes].forEach(elem => {
if (elem != book) {
elem.checked = false;
}
})
}
}
}
function removeHighlight() {
var books = document.getElementById("list_body").getElementsByTagName("tr");
for (var i = 0; i < books.length; i++) {
books[i].style.backgroundColor = dark ? "#DEA254" : "#E3E3E3";
}
}