autocompleter.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  16. */
  17. if(searx.autocompleter) {
  18. searx.searchResults = new Bloodhound({
  19. datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  20. queryTokenizer: Bloodhound.tokenizers.whitespace,
  21. remote: './autocompleter?q=%QUERY'
  22. });
  23. searx.searchResults.initialize();
  24. }
  25. $(document).ready(function(){
  26. var original_search_value = '';
  27. if(searx.autocompleter) {
  28. $("#q").on('keydown', function(e) {
  29. if(e.which == 13) {
  30. original_search_value = $('#q').val();
  31. }
  32. });
  33. $('#q').typeahead(null, {
  34. name: 'search-results',
  35. displayKey: function(result) {
  36. return result;
  37. },
  38. source: searx.searchResults.ttAdapter()
  39. });
  40. $('#q').bind('typeahead:selected', function(ev, suggestion) {
  41. if(original_search_value) {
  42. $('#q').val(original_search_value);
  43. }
  44. $("#search_form").submit();
  45. });
  46. }
  47. });