2022-10-03 21:40:57 +00:00
|
|
|
function _codigoparallevar_enable_search_box(selector, options) {
|
|
|
|
const element = document.querySelector(selector);
|
|
|
|
if ('placeholder' in options) {
|
|
|
|
element.setAttribute('placeholder', options.placeholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare backdrop
|
|
|
|
const resultsBoxBackdrop = document.createElement('div');
|
|
|
|
resultsBoxBackdrop.setAttribute('class', 'results-box-container hidden');
|
|
|
|
const resultsBox = document.createElement('div');
|
|
|
|
resultsBox.setAttribute('class', 'results-box');
|
|
|
|
|
2022-10-04 21:55:57 +00:00
|
|
|
// Results box contents
|
|
|
|
const innerSearchBox = document.createElement('input');
|
|
|
|
innerSearchBox.setAttribute('type', 'text');
|
|
|
|
innerSearchBox.setAttribute('placeholder', element.getAttribute('placeholder'));
|
|
|
|
resultsBox.appendChild(innerSearchBox);
|
|
|
|
|
|
|
|
const resultsList = document.createElement('ul');
|
|
|
|
resultsBox.appendChild(resultsList);
|
|
|
|
|
2022-10-26 20:42:24 +00:00
|
|
|
const noResultsBox = document.createElement('div');
|
|
|
|
noResultsBox.setAttribute('class', 'no-results-box hidden');
|
|
|
|
noResultsBox.innerText = 'No results 🤷';
|
|
|
|
resultsBox.appendChild(noResultsBox);
|
|
|
|
|
2022-10-03 21:40:57 +00:00
|
|
|
resultsBoxBackdrop.appendChild(resultsBox);
|
|
|
|
document.body.appendChild(resultsBoxBackdrop);
|
|
|
|
|
2022-10-04 21:55:57 +00:00
|
|
|
// Popup cancellation
|
|
|
|
resultsBoxBackdrop.onclick = () => {
|
|
|
|
resultsBoxBackdrop.classList.add('hidden');
|
|
|
|
};
|
|
|
|
resultsBox.onclick = (ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Element triggers popup
|
|
|
|
element.onfocus = () => {
|
|
|
|
resultsBoxBackdrop.classList.remove('hidden');
|
|
|
|
innerSearchBox.focus();
|
2022-10-16 22:10:06 +00:00
|
|
|
|
|
|
|
const wasKeyDown = document.onkeydown;
|
|
|
|
document.onkeydown = (ev) => {
|
|
|
|
if (ev.key === 'Escape') {
|
|
|
|
resultsBoxBackdrop.classList.add('hidden');
|
|
|
|
document.onkeydown = wasKeyDown;
|
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
|
|
|
};
|
2022-10-04 21:55:57 +00:00
|
|
|
};
|
2022-10-03 21:40:57 +00:00
|
|
|
|
2022-10-26 20:42:24 +00:00
|
|
|
const DEBOUNCE_TIME = 250; // Milliseconds
|
2022-10-03 21:40:57 +00:00
|
|
|
const MIN_LENGTH = 3;
|
2022-10-21 18:17:41 +00:00
|
|
|
const SEARCH_ENDPOINT = (window.location.host.startsWith('localhost')
|
|
|
|
? 'http://localhost:3001/api/search'
|
|
|
|
: 'https://api.codigoparallevar.com/api/search'
|
|
|
|
);
|
2022-10-03 21:40:57 +00:00
|
|
|
|
|
|
|
let debounceWaiter = null;
|
|
|
|
let currentQuery = null;
|
|
|
|
|
|
|
|
let lastVal = null;
|
|
|
|
const doQuery = () => {
|
2022-10-04 21:55:57 +00:00
|
|
|
const val = innerSearchBox.value.trim();
|
2022-10-03 21:40:57 +00:00
|
|
|
if ((val.length < MIN_LENGTH) || (val === lastVal)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastVal = val;
|
2023-05-01 20:37:00 +00:00
|
|
|
resultsBox.classList.add('loading');
|
2022-10-03 21:40:57 +00:00
|
|
|
|
|
|
|
const uri = SEARCH_ENDPOINT + '?q=' + encodeURIComponent(val);
|
|
|
|
let query = fetch(uri);
|
|
|
|
currentQuery = query;
|
|
|
|
query
|
|
|
|
.then(res => res.json())
|
|
|
|
.then((body) => {
|
|
|
|
if (query !== currentQuery) {
|
|
|
|
console.log("Query out-raced 🤷");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-01 20:37:00 +00:00
|
|
|
resultsBox.classList.remove('loading');
|
2022-10-16 22:10:06 +00:00
|
|
|
resultsList.innerHTML = '';
|
2022-10-30 22:58:44 +00:00
|
|
|
for (const list of [
|
|
|
|
body.results.notes.filter(n => n.is_todo !== "1"),
|
|
|
|
body.results.notes.filter(n => n.is_todo === "1"),
|
|
|
|
]){
|
|
|
|
for (const note of list) {
|
|
|
|
const resultCard = document.createElement('li');
|
|
|
|
|
|
|
|
const resultContents = document.createElement('a');
|
|
|
|
resultContents.setAttribute('href', './' + note.id + '.node.html');
|
|
|
|
|
|
|
|
const resultTitle = document.createElement('h2');
|
|
|
|
resultTitle.innerText = `${note.title} (${note.top_level_title})`;
|
|
|
|
if (note.is_todo === "1") {
|
|
|
|
resultTitle.setAttribute('class', 'is-todo');
|
|
|
|
}
|
|
|
|
else if (note.is_done === "1") {
|
|
|
|
resultTitle.setAttribute('class', 'is-done');
|
|
|
|
}
|
|
|
|
|
|
|
|
resultContents.appendChild(resultTitle);
|
|
|
|
resultCard.appendChild(resultContents);
|
|
|
|
resultsList.appendChild(resultCard);
|
2022-10-18 21:13:21 +00:00
|
|
|
}
|
2022-10-16 22:10:06 +00:00
|
|
|
}
|
2022-10-26 20:42:24 +00:00
|
|
|
if (body.results.notes.length == 0) {
|
|
|
|
noResultsBox.classList.remove('hidden');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
noResultsBox.classList.add('hidden');
|
|
|
|
}
|
2022-10-03 21:40:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
element.removeAttribute('disabled');
|
|
|
|
|
2022-10-04 21:55:57 +00:00
|
|
|
innerSearchBox.onkeyup = (ev) => {
|
2022-10-03 21:40:57 +00:00
|
|
|
if (debounceWaiter !== null) {
|
|
|
|
clearTimeout(debounceWaiter);
|
|
|
|
}
|
|
|
|
debounceWaiter = setTimeout(doQuery, DEBOUNCE_TIME);
|
|
|
|
};
|
|
|
|
}
|
2022-10-04 21:55:57 +00:00
|
|
|
|
2022-10-16 22:10:06 +00:00
|
|
|
// // TODO: Remove this when dev is done
|
|
|
|
// _codigoparallevar_enable_search_box('#searchbox', {placeholder: 'Search...'})
|
|
|
|
// document.querySelector('#searchbox').focus()
|