Update web/templates/search.html
This commit is contained in:
parent
a8ce34a78c
commit
2caab0f34f
@ -403,49 +403,81 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
(function(){
|
(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 form = document.querySelector('form.search-form') || document.querySelector('form[method="get"]');
|
||||||
const qInput = form && form.querySelector('input[name="q"]');
|
const qInput = form && form.querySelector('input[name="q"]');
|
||||||
|
|
||||||
// Helper: collect selected fields based on your existing checkbox naming: sel[fieldname]
|
// 1) Parse selected fields from the URL (most reliable, matches what server saw)
|
||||||
function getSelectedFields(scope=document){
|
function fieldsFromURL(urlSearch) {
|
||||||
const fields = [];
|
const params = new URLSearchParams(urlSearch || location.search);
|
||||||
scope.querySelectorAll('input[name^="sel["][type="checkbox"]').forEach(cb => {
|
const out = [];
|
||||||
if (cb.checked) {
|
// sel[field]=on → capture "field"
|
||||||
const name = cb.name.slice(4, -1); // sel[illustration] -> illustration
|
for (const [k, v] of params.entries()) {
|
||||||
fields.push(name);
|
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 out;
|
||||||
return fields.length ? fields : ["subject"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save current search on submit
|
// 2) Parse from checkboxes as a fallback (in case query string isn’t available)
|
||||||
if (form) {
|
function fieldsFromCheckboxes(scope=document) {
|
||||||
form.addEventListener('submit', () => {
|
const out = [];
|
||||||
const q = (qInput && qInput.value || '').trim();
|
scope.querySelectorAll('input[type="checkbox"][name^="sel["]').forEach(cb => {
|
||||||
const fields = getSelectedFields(form);
|
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 {
|
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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) => {
|
document.addEventListener('click', (e) => {
|
||||||
const a = e.target.closest('a');
|
const a = e.target.closest('a');
|
||||||
if (!a) return;
|
if (!a) return;
|
||||||
// Heuristic: only if this looks like a link to an entry page
|
// Heuristic: links to entries look like /entry/<id> (adjust if your pattern differs)
|
||||||
if (!/\/entry\/\d+/.test(a.getAttribute('href') || '')) return;
|
const href = a.getAttribute('href') || '';
|
||||||
const q = (qInput && qInput.value || '').trim();
|
if (!/\/entry\/\d+/.test(href)) return;
|
||||||
const fields = getSelectedFields(document);
|
|
||||||
try {
|
const q = currentQuery();
|
||||||
localStorage.setItem('lastSearchQ', q);
|
// Most reliable: read from current URL (exactly what server saw for these results)
|
||||||
localStorage.setItem('lastSearchFields', JSON.stringify(fields));
|
const fields = fieldsFromURL(location.search);
|
||||||
} catch(_) {}
|
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 %}
|
||||||
Loading…
Reference in New Issue
Block a user