Update web/templates/search.html
This commit is contained in:
+12
-62
@@ -401,83 +401,33 @@
|
|||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
<!-- Save q + selected fields at the moment the search form submits -->
|
||||||
<script>
|
<script>
|
||||||
(function(){
|
(function(){
|
||||||
// Try to find your search form; fallback to any GET form on the page
|
const form = document.querySelector('form.search-form');
|
||||||
const form = document.querySelector('form.search-form') || document.querySelector('form[method="get"]');
|
if (!form) return;
|
||||||
const qInput = form && form.querySelector('input[name="q"]');
|
|
||||||
|
|
||||||
// 1) Parse selected fields from the URL (most reliable, matches what server saw)
|
const qInput = form.querySelector('input[name="q"]');
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2) Parse from checkboxes as a fallback (in case query string isn’t available)
|
function getSelectedFields(scope=form){
|
||||||
function fieldsFromCheckboxes(scope=document) {
|
|
||||||
const out = [];
|
const out = [];
|
||||||
scope.querySelectorAll('input[type="checkbox"][name^="sel["]').forEach(cb => {
|
scope.querySelectorAll('input[type="checkbox"][name^="sel["]').forEach(cb => {
|
||||||
if (cb.checked) out.push(cb.name.slice(4, -1));
|
if (cb.checked) out.push(cb.name.slice(4, -1)); // sel[illustration] -> illustration
|
||||||
});
|
});
|
||||||
return out;
|
return out.length ? out : ["subject"]; // backend default
|
||||||
}
|
}
|
||||||
|
|
||||||
function currentQuery() {
|
form.addEventListener('submit', () => {
|
||||||
return (qInput && qInput.value || '').trim();
|
const q = (qInput && qInput.value || '').trim();
|
||||||
}
|
const fields = getSelectedFields();
|
||||||
|
|
||||||
function save(q, fields) {
|
|
||||||
// Default to ["subject"] if nothing explicitly selected (mirrors backend)
|
|
||||||
if (!fields || !fields.length) fields = ["subject"];
|
|
||||||
try {
|
try {
|
||||||
localStorage.setItem('lastSearchQ', q || '');
|
localStorage.setItem('lastSearchQ', q);
|
||||||
localStorage.setItem('lastSearchFields', JSON.stringify(fields));
|
localStorage.setItem('lastSearchFields', JSON.stringify(fields));
|
||||||
} catch(_) {}
|
} catch(_) {}
|
||||||
}
|
|
||||||
|
|
||||||
// Save on form submit
|
|
||||||
if (form) {
|
|
||||||
form.addEventListener('submit', () => {
|
|
||||||
const q = currentQuery();
|
|
||||||
// Prefer what’s 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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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: 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>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user