searx_search.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.autofocus", 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. function createClearButton(qinput) {
  33. var cs = document.getElementById('clear_search');
  34. var updateClearButton = function() {
  35. if (qinput.value.length === 0) {
  36. cs.classList.add("empty");
  37. } else {
  38. cs.classList.remove("empty");
  39. }
  40. };
  41. // update status, event listener
  42. updateClearButton();
  43. cs.addEventListener('click', function() {
  44. qinput.value='';
  45. qinput.focus();
  46. updateClearButton();
  47. });
  48. qinput.addEventListener('keyup', updateClearButton, false);
  49. }
  50. searx.ready(function() {
  51. qinput = d.getElementById(qinput_id);
  52. function placeCursorAtEndOnce(e) {
  53. if (firstFocus) {
  54. placeCursorAtEnd(qinput);
  55. firstFocus = false;
  56. } else {
  57. // e.preventDefault();
  58. }
  59. }
  60. if (qinput !== null) {
  61. // clear button
  62. createClearButton(qinput);
  63. // autocompleter
  64. if (searx.autocompleter) {
  65. searx.autocomplete = AutoComplete.call(w, {
  66. Url: "./autocompleter",
  67. EmptyMessage: searx.translations.no_item_found,
  68. HttpMethod: searx.method,
  69. HttpHeaders: {
  70. "Content-type": "application/x-www-form-urlencoded",
  71. "X-Requested-With": "XMLHttpRequest"
  72. },
  73. MinChars: 4,
  74. Delay: 300,
  75. }, "#" + qinput_id);
  76. // hack, see : https://github.com/autocompletejs/autocomplete.js/issues/37
  77. w.addEventListener('resize', function() {
  78. var event = new CustomEvent("position");
  79. qinput.dispatchEvent(event);
  80. });
  81. }
  82. qinput.addEventListener('focus', placeCursorAtEndOnce, false);
  83. qinput.focus();
  84. }
  85. // vanilla js version of search_on_category_select.js
  86. if (qinput !== null && searx.search_on_category_select) {
  87. d.querySelector('.help').className='invisible';
  88. searx.on('#categories input', 'change', function(e) {
  89. var i, categories = d.querySelectorAll('#categories input[type="checkbox"]');
  90. for(i=0; i<categories.length; i++) {
  91. if (categories[i] !== this && categories[i].checked) {
  92. categories[i].click();
  93. }
  94. }
  95. if (! this.checked) {
  96. this.click();
  97. }
  98. submitIfQuery();
  99. return false;
  100. });
  101. searx.on(d.getElementById('time_range'), 'change', submitIfQuery);
  102. searx.on(d.getElementById('language'), 'change', submitIfQuery);
  103. }
  104. });
  105. })(window, document, window.searx);