Update web/templates/search.html

This commit is contained in:
Joshua Laymon 2025-08-23 16:27:45 +00:00
parent a8ce34a78c
commit 2caab0f34f

View File

@ -403,49 +403,81 @@
<script>
(function(){
// Adjust these selectors if your form/checkbox names differ
// Try to find your search form; fallback to any GET form on the page
const form = document.querySelector('form.search-form') || document.querySelector('form[method="get"]');
const qInput = form && form.querySelector('input[name="q"]');
// Helper: collect selected fields based on your existing checkbox naming: sel[fieldname]
function getSelectedFields(scope=document){
const fields = [];
scope.querySelectorAll('input[name^="sel["][type="checkbox"]').forEach(cb => {
if (cb.checked) {
const name = cb.name.slice(4, -1); // sel[illustration] -> illustration
fields.push(name);
// 1) Parse selected fields from the URL (most reliable, matches what server saw)
function fieldsFromURL(urlSearch) {
const params = new URLSearchParams(urlSearch || location.search);
const out = [];
// sel[field]=on → capture "field"
for (const [k, v] of params.entries()) {
if (k.startsWith('sel[') && k.endsWith(']') && (v === 'on' || v === 'true' || v === '1')) {
out.push(k.slice(4, -1));
}
});
// Fallback: if none checked, your backend defaults to ["subject"]
return fields.length ? fields : ["subject"];
}
return out;
}
// Save current search on submit
if (form) {
form.addEventListener('submit', () => {
const q = (qInput && qInput.value || '').trim();
const fields = getSelectedFields(form);
// 2) Parse from checkboxes as a fallback (in case query string isnt available)
function fieldsFromCheckboxes(scope=document) {
const out = [];
scope.querySelectorAll('input[type="checkbox"][name^="sel["]').forEach(cb => {
if (cb.checked) out.push(cb.name.slice(4, -1));
});
return out;
}
function currentQuery() {
return (qInput && qInput.value || '').trim();
}
function save(q, fields) {
// Default to ["subject"] if nothing explicitly selected (mirrors backend)
if (!fields || !fields.length) fields = ["subject"];
try {
localStorage.setItem('lastSearchQ', q);
localStorage.setItem('lastSearchQ', q || '');
localStorage.setItem('lastSearchFields', JSON.stringify(fields));
} catch(_) {}
}
// Save on form submit
if (form) {
form.addEventListener('submit', () => {
const q = currentQuery();
// Prefer whats headed to the server (checkboxes), but also merge URL if present
const f1 = fieldsFromCheckboxes(form);
const f2 = fieldsFromURL(); // in case the page was reloaded with selections
const fields = Array.from(new Set([...f1, ...f2]));
save(q, fields);
});
}
// Also save if user clicks a result link without re-submitting (common flow)
// Save when clicking a result link (user may not submit again before clicking)
document.addEventListener('click', (e) => {
const a = e.target.closest('a');
if (!a) return;
// Heuristic: only if this looks like a link to an entry page
if (!/\/entry\/\d+/.test(a.getAttribute('href') || '')) return;
const q = (qInput && qInput.value || '').trim();
const fields = getSelectedFields(document);
try {
localStorage.setItem('lastSearchQ', q);
localStorage.setItem('lastSearchFields', JSON.stringify(fields));
} catch(_) {}
// Heuristic: links to entries look like /entry/<id> (adjust if your pattern differs)
const href = a.getAttribute('href') || '';
if (!/\/entry\/\d+/.test(href)) return;
const q = currentQuery();
// Most reliable: read from current URL (exactly what server saw for these results)
const fields = fieldsFromURL(location.search);
const fallback = fieldsFromCheckboxes(document);
const merged = Array.from(new Set([...(fields||[]), ...(fallback||[])]));
save(q, merged);
});
// Also save once on page load (covers pressing Enter without clicking results first)
document.addEventListener('DOMContentLoaded', () => {
const q = currentQuery();
const fields = fieldsFromURL(location.search);
if (q && fields.length) save(q, fields);
});
})();
</script>
{% endblock %}