blow/static/js/search.js

90 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-11-03 19:41:31 +00:00
document.addEventListener("DOMContentLoaded", function() {
2021-11-06 18:41:11 +00:00
// let search_input = document.getElementById('search').addEventListener('click', openSearch)
// var openmodal = document.querySelectorAll('.modal-open')
// for (var i = 0; i < openmodal.length; i++) {
// openmodal[i].addEventListener('click', function(event){
// event.preventDefault()
2021-11-07 11:41:12 +00:00
// toggleSearchModal()
2021-11-06 18:41:11 +00:00
// })
// }
2021-11-07 11:41:12 +00:00
let nav_search_input = document.getElementById('search');
nav_search_input.addEventListener('click', function(event){
2021-11-06 18:41:11 +00:00
event.preventDefault()
2021-11-07 11:41:12 +00:00
toggleSearchModal()
2021-11-06 18:41:11 +00:00
})
const overlay = document.querySelector('.modal-overlay')
2021-11-07 11:41:12 +00:00
overlay.addEventListener('click', toggleSearchModal)
2021-11-06 18:41:11 +00:00
2021-11-07 11:54:28 +00:00
let closemodal = document.querySelectorAll('.modal-close')
2021-11-07 11:52:22 +00:00
// closemodal.addEventListener('click', toggleSearchModal)
for (var i = 0; i < closemodal.length; i++) {
closemodal[i].addEventListener('click', toggleSearchModal)
}
2021-11-06 18:41:11 +00:00
2021-11-07 11:41:12 +00:00
let search_index = elasticlunr.Index.load(window.searchIndex);
let elasticlunr_options = {
bool: "AND",
fields: {
title: {boost: 2},
body: {boost: 1},
}
};
let search_term = "";
let search_results = "";
let search_input = document.getElementById('search-input');
2021-11-06 18:41:11 +00:00
document.onkeydown = function(evt) {
evt = evt || window.event
2021-11-07 10:06:01 +00:00
let isEscape = false
let isCmdK = false
2021-11-06 18:41:11 +00:00
if ("key" in evt) {
isEscape = (evt.key === "Escape" || evt.key === "Esc")
2021-11-07 10:15:11 +00:00
isCmdK = (evt.key === "k" && evt.metaKey === true)
2021-11-06 18:41:11 +00:00
} else {
2021-11-07 10:15:11 +00:00
isCmdK = (evt.keyCode === 75 && evt.metaKey)
2021-11-06 18:41:11 +00:00
isEscape = (evt.keyCode === 27)
}
2021-11-07 11:21:43 +00:00
if (isCmdK) { evt.preventDefault() }
2021-11-07 11:41:12 +00:00
if ((isEscape && document.body.classList.contains('search-active')) || isCmdK) {
toggleSearchModal();
}
// Trigger search
2021-11-07 11:51:01 +00:00
if ([...document.body.classList].includes('search-active') && search_input.value.trim().length > 3) {
2021-11-07 11:41:12 +00:00
console.log('search')
search_term = search_input.value.trim();
console.log(search_term)
search_results = index.search(term, options);
console.log(search_results)
// if (results.length === 0) {
// $searchResults.style.display = "none";
// return;
// }
2021-11-06 18:41:11 +00:00
}
};
2021-11-03 19:41:31 +00:00
});
2021-11-07 11:41:12 +00:00
function toggleSearchModal () {
2021-11-06 18:41:11 +00:00
const modal = document.getElementById('search-modal')
modal.classList.toggle('opacity-0')
modal.classList.toggle('pointer-events-none')
2021-11-07 11:49:05 +00:00
document.body.classList.toggle('search-active')
if ([...document.body.classList].includes('search-active')) {
2021-11-07 11:24:17 +00:00
// window.setTimeout(function() {
2021-11-07 11:23:49 +00:00
document.getElementById('search-input').value = ""
2021-11-07 11:14:01 +00:00
document.getElementById('search-input').focus()
2021-11-07 11:24:17 +00:00
// }, 500);
2021-11-07 10:47:54 +00:00
}
2021-11-06 18:20:43 +00:00
}
2021-11-04 22:55:27 +00:00
2021-11-06 18:41:11 +00:00
// function openSearch() {
// console.log("open modal");
// let search_modal = document.getElementById('search-modal');
// search_modal.classList.remove('hidden');
// console.log(search_modal);
// }
2021-11-07 11:49:05 +00:00
// function search() {
// let index = elasticlunr.Index.load(window.searchIndex);
// }