csv-library-website/library/static/js/search.js
2023-07-14 21:16:30 +02:00

43 lines
1.1 KiB
JavaScript

let searchInput = document.getElementById('booksearch');
var allpublications = document.getElementsByClassName("filter");
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)
});
})
}
function showBookId(bookid) {
let book = document.getElementById(bookid)
addClass(book, "show");
}
function clearSearchBooks() {
for (i = 0; i < allpublications.length; i++) {
addClass(allpublications[i], "show");
}
}