-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.html
197 lines (154 loc) · 7.5 KB
/
search.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>我が家の本棚</title>
<link rel="stylesheet" href="css/destyle.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/loader.css">
<script src="js/util.js"></script>
<script>
var books = [];
async function deleteFromDatabase(isbn) {
const flag = confirm("本当に削除しますか?");
if (flag) {
const uploadURL = localStorage.getItem("endpoint");
const upDataRaw = JSON.stringify([{ isbn: String(isbn) }]);
const upData = await base64Encode(upDataRaw);
const rawData = await fetch(uploadURL + "?action=delete&b=" + encodeURIComponent(upData));
const data = await rawData.json();
const registeredBooksArray = document.getElementById("searched").children;
for (let i = 0; i < registeredBooksArray.length; i++) {
let bookdiv = registeredBooksArray[i];
if (bookdiv.getElementsByClassName("isbnval")[0].innerText == isbn) {
bookdiv.remove();
break;
}
}
for (let i = 0; i < books.length; i++) {
if (books[i].isbn === isbn) {
books.splice(i, 1);
break;
}
}
}
}
window.addEventListener("DOMContentLoaded", async function () {
const loader = document.getElementById("searchloader");
loader.style.display = "block";
const uploadURL = localStorage.getItem("endpoint");
const searchResElm = document.getElementById("searched");
const rawData = await fetch(uploadURL + "?action=search");
const data = await rawData.json();
books = data;
for (let i = 0; i < data.length; i++) {
// 要素追加
let searched = document.getElementById("searched");
let bookdiv = searched.appendChild(document.createElement("div"));
bookdiv.className = "book";
let bookISBN = data[i].isbn;
let bookTitle = data[i].title;
let bookAuthors = data[i].author;
let bookPublisher = data[i].publisher;
let isbnkey = bookdiv.appendChild(document.createElement("span"));
isbnkey.innerText = "ISBN: ";
isbnkey.className = "bookkey";
let isbnval = bookdiv.appendChild(document.createElement("span"));
isbnval.innerText = bookISBN;
isbnval.className = "isbnval";
bookdiv.appendChild(document.createElement("br"));
let titlekey = bookdiv.appendChild(document.createElement("span"));
titlekey.innerText = "書名: ";
titlekey.className = "bookkey";
let titleval = bookdiv.appendChild(document.createElement("span"));
titleval.innerText = bookTitle;
titleval.className = "titleval";
bookdiv.appendChild(document.createElement("br"));
let authorkey = bookdiv.appendChild(document.createElement("span"));
authorkey.innerText = "著者: ";
authorkey.className = "bookkey";
let authorval = bookdiv.appendChild(document.createElement("span"));
authorval.innerText = bookAuthors;
authorval.className = "authorval";
bookdiv.appendChild(document.createElement("br"));
let deleteButton = bookdiv.appendChild(document.createElement("button"));
deleteButton.className = "remove";
deleteButton.innerText = "削除する";
deleteButton.addEventListener("click", function () {
deleteFromDatabase(bookISBN);
});
}
loader.style.display = "none";
});
function search() {
const tempBooks = books;
let res = [];
const query = document.getElementById("query").value;
for (let i = 0; i < tempBooks.length; i++) {
let tb = tempBooks[i];
let searchstr = tb.isbn + tb.title + tb.author + tb.publisher;
console.log(query.normalize("NFKC").toLowerCase(), searchstr.normalize("NFKC").toLowerCase())
if (searchstr.normalize("NFKC").toLowerCase().indexOf(query.normalize("NFKC").toLowerCase()) !== -1) {
res.push(tb);
}
}
let element = document.getElementById("searched");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
for (let i = 0; i < res.length; i++) {
// 要素追加
let searched = document.getElementById("searched");
let bookdiv = searched.appendChild(document.createElement("div"));
bookdiv.className = "book";
let bookISBN = res[i].isbn;
let bookTitle = res[i].title;
let bookAuthors = res[i].author;
let bookPublisher = res[i].publisher;
let isbnkey = bookdiv.appendChild(document.createElement("span"));
isbnkey.innerText = "ISBN: ";
isbnkey.className = "bookkey";
let isbnval = bookdiv.appendChild(document.createElement("span"));
isbnval.innerText = bookISBN;
isbnval.className = "isbnval";
bookdiv.appendChild(document.createElement("br"));
let titlekey = bookdiv.appendChild(document.createElement("span"));
titlekey.innerText = "書名: ";
titlekey.className = "bookkey";
let titleval = bookdiv.appendChild(document.createElement("span"));
titleval.innerText = bookTitle;
titleval.className = "titleval";
bookdiv.appendChild(document.createElement("br"));
let authorkey = bookdiv.appendChild(document.createElement("span"));
authorkey.innerText = "著者: ";
authorkey.className = "bookkey";
let authorval = bookdiv.appendChild(document.createElement("span"));
authorval.innerText = bookAuthors;
authorval.className = "authorval";
bookdiv.appendChild(document.createElement("br"));
let deleteButton = bookdiv.appendChild(document.createElement("button"));
deleteButton.className = "remove";
deleteButton.innerText = "削除する";
deleteButton.addEventListener("click", function () {
deleteFromDatabase(bookISBN);
});
}
}
</script>
</head>
<body>
<div id="bodyblock">
<h1>書籍登録・検索ページ</h1>
<div class="inputblock">
<label for="query">検索キーワードを入力</label><br>
<input type="text" id="query">
<button class="generalButton" id="search" onclick="search()">検索する</button>
</div>
<h3>検索結果</h3>
<div class="loader" id="searchloader"></div>
<div id="searched">
</div>
</div>
</body>
</html>