Update web/templates/search.html

This commit is contained in:
Joshua Laymon 2025-08-23 16:22:28 +00:00
parent b421abe69d
commit cc1ac48b6f

View File

@ -400,4 +400,52 @@
margin:0; margin:0;
} }
</style> </style>
<script>
(function(){
// Adjust these selectors if your form/checkbox names differ
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);
}
});
// Fallback: if none checked, your backend defaults to ["subject"]
return fields.length ? fields : ["subject"];
}
// Save current search on submit
if (form) {
form.addEventListener('submit', () => {
const q = (qInput && qInput.value || '').trim();
const fields = getSelectedFields(form);
try {
localStorage.setItem('lastSearchQ', q);
localStorage.setItem('lastSearchFields', JSON.stringify(fields));
} catch(_) {}
});
}
// Also save if user clicks a result link without re-submitting (common flow)
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(_) {}
});
})();
</script>
{% endblock %} {% endblock %}