summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBnyro <bnyro@tutanota.com>2024-11-26 15:01:21 +0100
committerMarkus Heiser <markus.heiser@darmarIT.de>2024-11-29 15:05:00 +0100
commit6a9b97cad23c2dccb6e5bbe36b58fd3280da8af9 (patch)
tree951cf758987a450b4474ef76f096d0a19bc9a4a6
parent6ca89e13198ed3b1e67e8f8209022affc4377603 (diff)
downloadsearxng-6a9b97cad23c2dccb6e5bbe36b58fd3280da8af9.tar.gz
searxng-6a9b97cad23c2dccb6e5bbe36b58fd3280da8af9.zip
[feat] search: shift/ctrl click a category to select multiple categories
-rw-r--r--searx/static/themes/simple/src/js/main/search.js53
-rw-r--r--searx/static/themes/simple/src/less/search.less17
-rw-r--r--searx/templates/simple/categories.html1
3 files changed, 52 insertions, 19 deletions
diff --git a/searx/static/themes/simple/src/js/main/search.js b/searx/static/themes/simple/src/js/main/search.js
index 46756507e..bc0f8b428 100644
--- a/searx/static/themes/simple/src/js/main/search.js
+++ b/searx/static/themes/simple/src/js/main/search.js
@@ -166,21 +166,46 @@
searxng.on(d.getElementById('language'), 'change', submitIfQuery);
}
- // most common browsers at the time of writing this support :has, except for Firefox
- // can be removed when Firefox / Firefox ESL starts supporting it as well
- try {
- // this fails when the browser does not support :has
- 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");
- })
- }
+ // remove CSS class that's only needed when the browser supports no JavaScript
+ const categoriesContainer = d.querySelector("#categories_container");
+ if (categoriesContainer) {
+ categoriesContainer.classList.remove("nojs");
+ }
+
+ 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);
diff --git a/searx/static/themes/simple/src/less/search.less b/searx/static/themes/simple/src/less/search.less
index fb5cd12f4..458bba155 100644
--- a/searx/static/themes/simple/src/less/search.less
+++ b/searx/static/themes/simple/src/less/search.less
@@ -77,16 +77,23 @@ button.category_button {
}
&.selected,
- &:active,
- &:focus-within {
+ &:active {
color: var(--color-categories-item-selected-font);
border-bottom: 2px solid var(--color-categories-item-border-selected);
}
}
-#categories_container:has(button.category_button:focus-within) button.category_button.selected {
- color: var(--color-base-font);
- border-bottom: none;
+// only used when JavaScript is disabled
+.no-js #categories_container:has(button.category_button:focus-within) button.category_button {
+ &.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 {
diff --git a/searx/templates/simple/categories.html b/searx/templates/simple/categories.html
index 2bef2f8f6..10eaf97db 100644
--- a/searx/templates/simple/categories.html
+++ b/searx/templates/simple/categories.html
@@ -31,6 +31,7 @@
<div class="category_name">{{- _(category) -}}</div>{{- '' -}}
</button>{{- '' -}}
{%- endfor -%}
+ <input name="categories" id="selected-categories" type="hidden" />
{{- '\n' -}}
{%- endif -%}
</div>{{- '' -}}