[feat] search: shift/ctrl click a category to select multiple categories

This commit is contained in:
Bnyro 2024-11-26 15:01:21 +01:00 committed by Markus Heiser
parent 6ca89e1319
commit 6a9b97cad2
3 changed files with 52 additions and 19 deletions

View file

@ -166,21 +166,46 @@
searxng.on(d.getElementById('language'), 'change', submitIfQuery); searxng.on(d.getElementById('language'), 'change', submitIfQuery);
} }
// most common browsers at the time of writing this support :has, except for Firefox // remove CSS class that's only needed when the browser supports no JavaScript
// can be removed when Firefox / Firefox ESL starts supporting it as well const categoriesContainer = d.querySelector("#categories_container");
try { if (categoriesContainer) {
// this fails when the browser does not support :has categoriesContainer.classList.remove("nojs");
d.querySelector("html:has(body)");
} catch (_) {
// manually deselect the old selection when a new category is selected
for (let button of d.querySelectorAll("button.category_button")) {
searxng.on(button, 'click', () => {
const selected = d.querySelector("button.category_button.selected");
console.log(selected);
selected.classList.remove("selected");
})
}
} }
const categoryButtons = d.querySelectorAll("button.category_button");
for (let button of categoryButtons) {
searxng.on(button, 'click', (event) => {
if (event.shiftKey) {
event.preventDefault();
button.classList.toggle("selected");
return;
}
// manually deselect the old selection when a new category is selected
const selectedCategories = d.querySelectorAll("button.category_button.selected");
for (let categoryButton of selectedCategories) {
categoryButton.classList.remove("selected");
}
button.classList.add("selected");
})
}
// override form submit action to update the actually selected categories
const form = d.querySelector("#search");
searxng.on(form, 'submit', (event) => {
event.preventDefault();
const categoryValuesInput = d.querySelector("#selected-categories");
if (categoryValuesInput) {
let categoryValues = [];
for (let categoryButton of categoryButtons) {
if (categoryButton.classList.contains("selected")) {
categoryValues.push(categoryButton.name.replace("category_", ""));
}
}
categoryValuesInput.value = categoryValues.join(",");
}
form.submit();
});
}); });
})(window, document, window.searxng); })(window, document, window.searxng);

View file

@ -77,16 +77,23 @@ button.category_button {
} }
&.selected, &.selected,
&:active, &:active {
&:focus-within {
color: var(--color-categories-item-selected-font); color: var(--color-categories-item-selected-font);
border-bottom: 2px solid var(--color-categories-item-border-selected); border-bottom: 2px solid var(--color-categories-item-border-selected);
} }
} }
#categories_container:has(button.category_button:focus-within) button.category_button.selected { // only used when JavaScript is disabled
color: var(--color-base-font); .no-js #categories_container:has(button.category_button:focus-within) button.category_button {
border-bottom: none; &.selected {
color: var(--color-base-font);
border-bottom: none;
}
&:focus-within {
color: var(--color-categories-item-selected-font);
border-bottom: 2px solid var(--color-categories-item-border-selected);
}
} }
#search_logo { #search_logo {

View file

@ -31,6 +31,7 @@
<div class="category_name">{{- _(category) -}}</div>{{- '' -}} <div class="category_name">{{- _(category) -}}</div>{{- '' -}}
</button>{{- '' -}} </button>{{- '' -}}
{%- endfor -%} {%- endfor -%}
<input name="categories" id="selected-categories" type="hidden" />
{{- '\n' -}} {{- '\n' -}}
{%- endif -%} {%- endif -%}
</div>{{- '' -}} </div>{{- '' -}}