From 1af96e2a7e449ae628059e7dca7574ba36bfd491 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 18 Jan 2022 18:35:50 +0100 Subject: [PATCH] [fix] plugins in simple theme The previous commit introduced a new plugin Resource API. This commit implements the necessary changes in the simple theme. (There's no change to infinite_scroll.py because it hasn't actually yet been implemented in the Simple theme.) --- searx/plugins/search_on_category_select.py | 1 + searx/plugins/vim_hotkeys.py | 1 + .../themes/simple/src/js/head/00_init.js | 5 +-- .../themes/simple/src/js/main/search.js | 31 ------------------- .../js/plugins/search_on_category_select.js | 31 +++++++++++++++++++ .../keyboard.js => plugins/vim_hotkeys.js} | 26 +++++++--------- searx/templates/simple/base.html | 3 -- 7 files changed, 46 insertions(+), 52 deletions(-) create mode 100644 searx/static/themes/simple/src/js/plugins/search_on_category_select.js rename searx/static/themes/simple/src/js/{main/keyboard.js => plugins/vim_hotkeys.js} (94%) diff --git a/searx/plugins/search_on_category_select.py b/searx/plugins/search_on_category_select.py index 6698b747f..ea02131df 100644 --- a/searx/plugins/search_on_category_select.py +++ b/searx/plugins/search_on_category_select.py @@ -26,4 +26,5 @@ preference_section = 'ui' js_dependencies = ( Resource(path='plugins/js/search_on_category_select.js', themes=('oscar',)), + Resource(path='themes/simple/src/js/plugins/search_on_category_select.js', themes=('simple',)), ) diff --git a/searx/plugins/vim_hotkeys.py b/searx/plugins/vim_hotkeys.py index 3d1982b3e..9eb7de509 100644 --- a/searx/plugins/vim_hotkeys.py +++ b/searx/plugins/vim_hotkeys.py @@ -12,5 +12,6 @@ preference_section = 'ui' js_dependencies = ( Resource(path='plugins/js/vim_hotkeys.js', themes=('oscar',)), + Resource(path='themes/simple/src/js/plugins/vim_hotkeys.js', themes=('simple',)), ) css_dependencies = (Resource(path='plugins/css/vim_hotkeys.css', themes=('oscar',)),) diff --git a/searx/static/themes/simple/src/js/head/00_init.js b/searx/static/themes/simple/src/js/head/00_init.js index 073371ce6..c65168a0b 100644 --- a/searx/static/themes/simple/src/js/head/00_init.js +++ b/searx/static/themes/simple/src/js/head/00_init.js @@ -19,9 +19,6 @@ touch: (("ontouchstart" in w) || w.DocumentTouch && document instanceof DocumentTouch) || false, method: script.getAttribute('data-method'), autocompleter: script.getAttribute('data-autocompleter') === 'true', - search_on_category_select: script.getAttribute('data-search-on-category-select') === 'true', - infinite_scroll: script.getAttribute('data-infinite-scroll') === 'true', - hotkeys: script.getAttribute('data-hotkeys') === 'true', static_path: script.getAttribute('data-static-path'), translations: JSON.parse(script.getAttribute('data-translations')), theme: { @@ -37,4 +34,4 @@ if (w.searxng.touch) { hmtlElement.classList.add('touch'); } -})(window, document); \ No newline at end of file +})(window, document); diff --git a/searx/static/themes/simple/src/js/main/search.js b/searx/static/themes/simple/src/js/main/search.js index 6ef95f5bf..269137e06 100644 --- a/searx/static/themes/simple/src/js/main/search.js +++ b/searx/static/themes/simple/src/js/main/search.js @@ -12,13 +12,6 @@ } } - function submitIfQuery () { - if (qinput.value.length > 0) { - var search = document.getElementById('search'); - setTimeout(search.submit.bind(search), 0); - } - } - function createClearButton (qinput) { var cs = document.getElementById('clear_search'); var updateClearButton = function () { @@ -95,30 +88,6 @@ qinput.addEventListener('focus', placeCursorAtEndOnce, false); qinput.focus(); } - - // vanilla js version of search_on_category_select.js - if (qinput !== null && d.querySelector('.help') != null && searxng.search_on_category_select) { - d.querySelector('.help').className = 'invisible'; - - searxng.on('#categories input', 'change', function () { - var i, categories = d.querySelectorAll('#categories input[type="checkbox"]'); - for (i = 0; i < categories.length; i++) { - if (categories[i] !== this && categories[i].checked) { - categories[i].click(); - } - } - if (! this.checked) { - this.click(); - } - submitIfQuery(); - return false; - }); - - searxng.on(d.getElementById('safesearch'), 'change', submitIfQuery); - searxng.on(d.getElementById('time_range'), 'change', submitIfQuery); - searxng.on(d.getElementById('language'), 'change', submitIfQuery); - } - }); })(window, document, window.searxng); diff --git a/searx/static/themes/simple/src/js/plugins/search_on_category_select.js b/searx/static/themes/simple/src/js/plugins/search_on_category_select.js new file mode 100644 index 000000000..2eaec9044 --- /dev/null +++ b/searx/static/themes/simple/src/js/plugins/search_on_category_select.js @@ -0,0 +1,31 @@ +const qinput = document.getElementById('q'); + +function submitIfQuery() { + if (qinput.value.length > 0) { + var search = document.getElementById('search'); + setTimeout(search.submit.bind(search), 0); + } +} + +// vanilla js version of search_on_category_select.js +if (qinput !== null && document.querySelector('.help') != null) { + document.querySelector('.help').className = 'invisible'; + + searxng.on('#categories input', 'change', function () { + var i, categories = document.querySelectorAll('#categories input[type="checkbox"]'); + for (i = 0; i < categories.length; i++) { + if (categories[i] !== this && categories[i].checked) { + categories[i].click(); + } + } + if (!this.checked) { + this.click(); + } + submitIfQuery(); + return false; + }); + + searxng.on(document.getElementById('safesearch'), 'change', submitIfQuery); + searxng.on(document.getElementById('time_range'), 'change', submitIfQuery); + searxng.on(document.getElementById('language'), 'change', submitIfQuery); +} diff --git a/searx/static/themes/simple/src/js/main/keyboard.js b/searx/static/themes/simple/src/js/plugins/vim_hotkeys.js similarity index 94% rename from searx/static/themes/simple/src/js/main/keyboard.js rename to searx/static/themes/simple/src/js/plugins/vim_hotkeys.js index a8ab7222f..67f42526c 100644 --- a/searx/static/themes/simple/src/js/main/keyboard.js +++ b/searx/static/themes/simple/src/js/plugins/vim_hotkeys.js @@ -154,22 +154,20 @@ searxng.ready(function () { } }; - if (searxng.hotkeys) { - searxng.on(document, "keydown", function (e) { - // check for modifiers so we don't break browser's hotkeys - if (Object.prototype.hasOwnProperty.call(vimKeys, e.keyCode) && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) { - var tagName = e.target.tagName.toLowerCase(); - if (e.keyCode === 27) { - vimKeys[e.keyCode].fun(e); - } else { - if (e.target === document.body || tagName === 'a' || tagName === 'button') { - e.preventDefault(); - vimKeys[e.keyCode].fun(); - } + searxng.on(document, "keydown", function (e) { + // check for modifiers so we don't break browser's hotkeys + if (Object.prototype.hasOwnProperty.call(vimKeys, e.keyCode) && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) { + var tagName = e.target.tagName.toLowerCase(); + if (e.keyCode === 27) { + vimKeys[e.keyCode].fun(e); + } else { + if (e.target === document.body || tagName === 'a' || tagName === 'button') { + e.preventDefault(); + vimKeys[e.keyCode].fun(); } } - }); - } + } + }); function highlightResult (which) { return function (noScroll) { diff --git a/searx/templates/simple/base.html b/searx/templates/simple/base.html index 949ee80c1..48b62c527 100644 --- a/searx/templates/simple/base.html +++ b/searx/templates/simple/base.html @@ -24,9 +24,6 @@ {% block head %}