autocompleter.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @license
  3. * (C) Copyright Contributors to the SearXNG project.
  4. * (C) Copyright Contributors to the searx project (2014 - 2021).
  5. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. $(document).ready(function(){
  9. var original_search_value = '';
  10. if(searxng.autocompleter) {
  11. var searchResults = new Bloodhound({
  12. datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  13. queryTokenizer: Bloodhound.tokenizers.whitespace,
  14. remote: {
  15. url: './autocompleter?q=%QUERY',
  16. wildcard: '%QUERY'
  17. }
  18. });
  19. searchResults.initialize();
  20. $("#q").on('keydown', function(e) {
  21. if(e.which == 13) {
  22. original_search_value = $('#q').val();
  23. }
  24. });
  25. $('#q').typeahead({
  26. name: 'search-results',
  27. highlight: false,
  28. hint: true,
  29. displayKey: function(result) {
  30. return result;
  31. },
  32. classNames: {
  33. input: 'tt-input',
  34. hint: 'tt-hint',
  35. menu: 'tt-dropdown-menu',
  36. dataset: 'tt-dataset-search-results',
  37. },
  38. }, {
  39. name: 'autocomplete',
  40. source: searchResults,
  41. });
  42. $('#q').bind('typeahead:select', function(ev, suggestion) {
  43. if(original_search_value) {
  44. $('#q').val(original_search_value);
  45. }
  46. $("#search_form").submit();
  47. });
  48. }
  49. });