blow/static/js/search.js

68 lines
2.1 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()
// toggleModal()
// })
// }
let search_input = document.getElementById('search');
search_input.addEventListener('click', function(event){
event.preventDefault()
toggleModal()
})
const overlay = document.querySelector('.modal-overlay')
overlay.addEventListener('click', toggleModal)
2021-11-07 10:06:01 +00:00
let closemodal = document.getElementById('close-modal')
2021-11-07 10:08:30 +00:00
closemodal.addEventListener('click', toggleModal)
// for (var i = 0; i < closemodal.length; i++) {
// closemodal[i].addEventListener('click', toggleModal)
// }
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 10:06:01 +00:00
if ((isEscape && document.body.classList.contains('modal-active')) || isCmdK) {
2021-11-07 10:20:09 +00:00
toggleModal();
2021-11-06 18:41:11 +00:00
}
};
2021-11-03 19:41:31 +00:00
});
2021-11-06 18:41:11 +00:00
function toggleModal () {
const body = document.querySelector('body')
const modal = document.getElementById('search-modal')
modal.classList.toggle('opacity-0')
modal.classList.toggle('pointer-events-none')
body.classList.toggle('modal-active')
2021-11-07 10:52:57 +00:00
if ([...body.classList].includes('modal-active')) {
2021-11-07 11:14:01 +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:15:12 +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-06 18:20:43 +00:00
function search() {
let index = elasticlunr.Index.load(window.searchIndex);
2021-11-03 19:41:31 +00:00
}