csv-library-website/library/static/js/search.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-07-12 21:09:22 +02:00
let searchInput = document.getElementById('booksearch');
2023-07-14 21:16:30 +02:00
var allpublications = document.getElementsByClassName("filter");
2023-07-12 21:09:22 +02:00
2023-07-14 21:16:30 +02:00
const ENTER_KEY_CODE = 13;
searchInput.addEventListener('keyup', function(e) {
if (e.keyCode === ENTER_KEY_CODE) {
if (searchInput.value.length > 2) {
searchBooks(searchInput.value);
} else {
clearSearchBooks();
}
}
})
function searchBooks(searchQuery) {
let searchUrl = `search/${searchQuery}`
fetch(searchUrl)
.then(response => response.json())
.then(searchdata => {
console.log(`book ids: ${searchdata} found for ${searchQuery}`);
if (searchdata === undefined || searchdata.length == 0) return;
for (i = 0; i < allpublications.length; i++) {
removeClass(allpublications[i], "show");
}
searchdata.forEach(bookid => {
showBookId(bookid)
});
})
2023-07-12 21:09:22 +02:00
}
2023-07-14 21:16:30 +02:00
function showBookId(bookid) {
let book = document.getElementById(bookid)
addClass(book, "show");
}
function clearSearchBooks() {
for (i = 0; i < allpublications.length; i++) {
addClass(allpublications[i], "show");
}
2023-07-12 21:09:22 +02:00
}