blow/static/js/search.js

120 lines
4.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()
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
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();
}
2021-11-07 12:00:09 +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-07 17:57:31 +00:00
let search_results_container = document.getElementById('search-results');
2021-11-07 12:00:09 +00:00
search_input.addEventListener('keyup', function(event) {
2021-11-07 11:41:12 +00:00
// 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 18:16:19 +00:00
// console.log('search')
2021-11-07 11:41:12 +00:00
search_term = search_input.value.trim();
2021-11-07 18:28:26 +00:00
// console.log(search_term)
2021-11-07 11:56:53 +00:00
search_results = search_index.search(search_term, elasticlunr_options);
2021-11-07 18:16:19 +00:00
// console.log(search_results)
2021-11-07 17:57:31 +00:00
if (Array.isArray(search_results) && search_results.length > 0) {
2021-11-07 18:15:20 +00:00
let result_list = document.getElementById('results-list');
2021-11-07 18:28:26 +00:00
result_list.replaceChildren();
2021-11-07 18:15:20 +00:00
let item = "";
2021-11-07 17:57:31 +00:00
for (i = 0; i < search_results.length; i++) {
2021-11-07 18:03:11 +00:00
let item = formatResultItem(search_results[i]);
2021-11-07 18:15:20 +00:00
result_list.appendChild(item);
2021-11-07 17:57:31 +00:00
}
}
2021-11-07 11:41:12 +00:00
// if (results.length === 0) {
// $searchResults.style.display = "none";
// return;
// }
2021-11-06 18:41:11 +00:00
}
2021-11-07 12:00:09 +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 18:03:11 +00:00
document.getElementById('search-input').value = ""
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-07 18:03:11 +00:00
function formatResultItem(search_result) {
console.log(search_result)
2021-11-07 19:27:37 +00:00
let formatted_result = `<li class="flex flex-col gap-y-2 hover:bg-gray-200 dark:hover:bg-gray-600 text-black dark:text-gray-200 p-2">
2021-11-07 18:15:20 +00:00
<span class="text-xl text-bold">${search_result.doc.title}</span>
<span class="text-lg">${search_result.doc.description}</span>
</li>`
2021-11-07 18:20:55 +00:00
return htmlToElement(formatted_result)
}
// Credits : https://stackoverflow.com/a/35385518/7098666
function htmlToElement(html) {
let template = document.createElement('template');
html = html.trim();
template.innerHTML = html;
return template.content.firstChild;
2021-11-07 17:57:31 +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);
// }