2021-10-01 16:33:31 +02:00
|
|
|
/* SPDX-License-Identifier: AGPL-3.0-or-later */
|
2021-08-24 11:31:29 +02:00
|
|
|
/* global AutoComplete */
|
2021-10-01 16:33:31 +02:00
|
|
|
(function(w, d, searxng) {
|
2017-02-12 15:06:01 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var firstFocus = true, qinput_id = "q", qinput;
|
|
|
|
|
|
|
|
function placeCursorAtEnd(element) {
|
|
|
|
if (element.setSelectionRange) {
|
|
|
|
var len = element.value.length;
|
|
|
|
element.setSelectionRange(len, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitIfQuery() {
|
|
|
|
if (qinput.value.length > 0) {
|
|
|
|
var search = document.getElementById('search');
|
|
|
|
setTimeout(search.submit.bind(search), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:13:50 +02:00
|
|
|
function createClearButton(qinput) {
|
|
|
|
var cs = document.getElementById('clear_search');
|
|
|
|
var updateClearButton = function() {
|
|
|
|
if (qinput.value.length === 0) {
|
|
|
|
cs.classList.add("empty");
|
|
|
|
} else {
|
|
|
|
cs.classList.remove("empty");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// update status, event listener
|
|
|
|
updateClearButton();
|
|
|
|
cs.addEventListener('click', function() {
|
|
|
|
qinput.value='';
|
|
|
|
qinput.focus();
|
|
|
|
updateClearButton();
|
|
|
|
});
|
|
|
|
qinput.addEventListener('keyup', updateClearButton, false);
|
|
|
|
}
|
|
|
|
|
2021-10-01 16:33:31 +02:00
|
|
|
searxng.ready(function() {
|
2017-02-12 15:06:01 +01:00
|
|
|
qinput = d.getElementById(qinput_id);
|
|
|
|
|
2021-08-24 11:31:29 +02:00
|
|
|
function placeCursorAtEndOnce() {
|
2017-02-12 15:06:01 +01:00
|
|
|
if (firstFocus) {
|
|
|
|
placeCursorAtEnd(qinput);
|
|
|
|
firstFocus = false;
|
|
|
|
} else {
|
|
|
|
// e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qinput !== null) {
|
2018-08-09 16:13:50 +02:00
|
|
|
// clear button
|
|
|
|
createClearButton(qinput);
|
2021-12-05 11:32:04 +01:00
|
|
|
|
2017-02-12 15:06:01 +01:00
|
|
|
// autocompleter
|
2021-10-01 16:33:31 +02:00
|
|
|
if (searxng.autocompleter) {
|
|
|
|
searxng.autocomplete = AutoComplete.call(w, {
|
2017-02-12 15:06:01 +01:00
|
|
|
Url: "./autocompleter",
|
2021-10-01 16:33:31 +02:00
|
|
|
EmptyMessage: searxng.translations.no_item_found,
|
|
|
|
HttpMethod: searxng.method,
|
2020-09-19 10:13:05 +02:00
|
|
|
HttpHeaders: {
|
|
|
|
"Content-type": "application/x-www-form-urlencoded",
|
|
|
|
"X-Requested-With": "XMLHttpRequest"
|
|
|
|
},
|
2017-02-12 15:06:01 +01:00
|
|
|
MinChars: 4,
|
|
|
|
Delay: 300,
|
2021-12-05 11:32:04 +01:00
|
|
|
_Position:function() {
|
|
|
|
this.DOMResults.setAttribute("class", "autocomplete");
|
|
|
|
this.DOMResults.style.top = (this.Input.offsetTop + this.Input.offsetHeight) + "px";
|
|
|
|
this.DOMResults.style.left = this.Input.offsetLeft + "px";
|
|
|
|
this.DOMResults.style.width = this.Input.clientWidth + "px";
|
|
|
|
},
|
2017-02-12 15:06:01 +01:00
|
|
|
}, "#" + qinput_id);
|
|
|
|
|
|
|
|
// hack, see : https://github.com/autocompletejs/autocomplete.js/issues/37
|
|
|
|
w.addEventListener('resize', function() {
|
|
|
|
var event = new CustomEvent("position");
|
|
|
|
qinput.dispatchEvent(event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
qinput.addEventListener('focus', placeCursorAtEndOnce, false);
|
|
|
|
qinput.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// vanilla js version of search_on_category_select.js
|
2021-10-01 16:33:31 +02:00
|
|
|
if (qinput !== null && d.querySelector('.help') != null && searxng.search_on_category_select) {
|
2017-02-12 15:06:01 +01:00
|
|
|
d.querySelector('.help').className='invisible';
|
|
|
|
|
2021-10-01 16:33:31 +02:00
|
|
|
searxng.on('#categories input', 'change', function() {
|
2017-02-12 15:06:01 +01:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2021-11-21 21:38:00 +01:00
|
|
|
searxng.on(d.getElementById('safesearch'), 'change', submitIfQuery);
|
2021-10-01 16:33:31 +02:00
|
|
|
searxng.on(d.getElementById('time_range'), 'change', submitIfQuery);
|
|
|
|
searxng.on(d.getElementById('language'), 'change', submitIfQuery);
|
2017-02-12 15:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2021-10-01 16:33:31 +02:00
|
|
|
})(window, document, window.searxng);
|