searx_search.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * searx is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU Affero General Public License as published by
  4. * the Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * searx is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Affero General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Affero General Public License
  13. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  14. *
  15. * (C) 2017 by Alexandre Flament, <alex@al-f.net>
  16. */
  17. (function(w, d, searx) {
  18. 'use strict';
  19. var firstFocus = true, qinput_id = "q", qinput;
  20. function placeCursorAtEnd(element) {
  21. if (element.setSelectionRange) {
  22. var len = element.value.length;
  23. element.setSelectionRange(len, len);
  24. }
  25. }
  26. function submitIfQuery() {
  27. if (qinput.value.length > 0) {
  28. var search = document.getElementById('search');
  29. setTimeout(search.submit.bind(search), 0);
  30. }
  31. }
  32. searx.ready(function() {
  33. qinput = d.getElementById(qinput_id);
  34. function placeCursorAtEndOnce(e) {
  35. if (firstFocus) {
  36. placeCursorAtEnd(qinput);
  37. firstFocus = false;
  38. } else {
  39. // e.preventDefault();
  40. }
  41. }
  42. if (qinput !== null) {
  43. // autocompleter
  44. if (searx.autocompleter) {
  45. searx.autocomplete = AutoComplete.call(w, {
  46. Url: "./autocompleter",
  47. EmptyMessage: searx.noItemFound,
  48. HttpMethod: searx.method,
  49. MinChars: 4,
  50. Delay: 300,
  51. }, "#" + qinput_id);
  52. // hack, see : https://github.com/autocompletejs/autocomplete.js/issues/37
  53. w.addEventListener('resize', function() {
  54. var event = new CustomEvent("position");
  55. qinput.dispatchEvent(event);
  56. });
  57. }
  58. qinput.addEventListener('focus', placeCursorAtEndOnce, false);
  59. qinput.focus();
  60. }
  61. // vanilla js version of search_on_category_select.js
  62. if (qinput !== null && searx.search_on_category_select) {
  63. d.querySelector('.help').className='invisible';
  64. searx.on('#categories input', 'change', function(e) {
  65. var i, categories = d.querySelectorAll('#categories input[type="checkbox"]');
  66. for(i=0; i<categories.length; i++) {
  67. if (categories[i] !== this && categories[i].checked) {
  68. categories[i].click();
  69. }
  70. }
  71. if (! this.checked) {
  72. this.click();
  73. }
  74. submitIfQuery();
  75. return false;
  76. });
  77. searx.on(d.getElementById('time_range'), 'change', submitIfQuery);
  78. searx.on(d.getElementById('language'), 'change', submitIfQuery);
  79. }
  80. });
  81. })(window, document, window.searx);