Guest User

Untitled

a guest
Nov 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. <div class="form-group">
  2. <label class="control-label">Enter an Airport</label>
  3. <input id="autocomplete" type="text" />
  4. </div>
  5.  
  6. var options = {
  7. shouldSort: true,
  8. threshold: 0.4,
  9. maxPatternLength: 32,
  10. keys: [{
  11. name: 'iata',
  12. weight: 0.5
  13. }, {
  14. name: 'name',
  15. weight: 0.3
  16. }, {
  17. name: 'city',
  18. weight: 0.2
  19. }]
  20. };
  21.  
  22. var fuse = new Fuse(airports, options)
  23.  
  24.  
  25. var ac = $('#autocomplete')
  26. .on('click', function(e) {
  27. e.stopPropagation();
  28. })
  29. .on('focus keyup', search)
  30. .on('keydown', onKeyDown);
  31.  
  32. var wrap = $('<div>')
  33. .addClass('autocomplete-wrapper')
  34. .insertBefore(ac)
  35. .append(ac);
  36.  
  37. var list = $('<div>')
  38. .addClass('autocomplete-results')
  39. .on('click', '.autocomplete-result', function(e) {
  40. e.preventDefault();
  41. e.stopPropagation();
  42. selectIndex($(this).data('index'));
  43. })
  44. .appendTo(wrap);
  45.  
  46. $(document)
  47. .on('mouseover', '.autocomplete-result', function(e) {
  48. var index = parseInt($(this).data('index'), 10);
  49. if (!isNaN(index)) {
  50. list.attr('data-highlight', index);
  51. }
  52. })
  53. .on('click', clearResults);
  54.  
  55. function clearResults() {
  56. results = [];
  57. numResults = 0;
  58. list.empty();
  59. }
  60.  
  61. function selectIndex(index) {
  62. if (results.length >= index + 1) {
  63. ac.val(results[index].iata);
  64. clearResults();
  65. }
  66. }
  67.  
  68. var results = [];
  69. var numResults = 0;
  70. var selectedIndex = -1;
  71.  
  72. function search(e) {
  73. if (e.which === 38 || e.which === 13 || e.which === 40) {
  74. return;
  75. }
  76.  
  77. if (ac.val().length > 0) {
  78. results = _.take(fuse.search(ac.val()), 7);
  79. numResults = results.length;
  80.  
  81. var divs = results.map(function(r, i) {
  82. return '<div class="autocomplete-result" data-index="'+ i +'">'
  83. + '<div><b>'+ r.iata +'</b> - '+ r.name +'</div>'
  84. + '<div class="autocomplete-location">'+ r.city +', '+ r.country +'</div>'
  85. + '</div>';
  86. });
  87.  
  88. selectedIndex = -1;
  89. list.html(divs.join(''))
  90. .attr('data-highlight', selectedIndex);
  91.  
  92. } else {
  93. numResults = 0;
  94. list.empty();
  95. }
  96. }
  97.  
  98. function onKeyDown(e) {
  99. switch(e.which) {
  100. case 38: // up
  101. selectedIndex--;
  102. if (selectedIndex <= -1) {
  103. selectedIndex = -1;
  104. }
  105. list.attr('data-highlight', selectedIndex);
  106. break;
  107. case 13: // enter
  108. selectIndex(selectedIndex);
  109. break;
  110. case 9: // enter
  111. selectIndex(selectedIndex);
  112. e.stopPropagation();
  113. return;
  114. case 40: // down
  115. selectedIndex++;
  116. if (selectedIndex >= numResults) {
  117. selectedIndex = numResults-1;
  118. }
  119. list.attr('data-highlight', selectedIndex);
  120. break;
  121.  
  122. default: return; // exit this handler for other keys
  123. }
  124. e.stopPropagation();
  125. e.preventDefault(); // prevent the default action (scroll / move caret)
  126. }
Add Comment
Please, Sign In to add comment