Guest User

Untitled

a guest
Apr 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. $.fn.ac = function (options) {
  2. options = $.extend({}, $.fn.ac.defaults, options);
  3. var element = $(this);
  4. var index = -1;
  5.  
  6. //Generate the lists that will be used for ac
  7. var ul = $('<ul />').css({ display: 'none'}).insertAfter(this);
  8. ul.attr('id', 'ac_' + $.data(this));
  9.  
  10. //when the focus is lost of the input hide the ac list. 200ms so if we click the list it
  11. //will get caught.
  12. element.blur(function (e) {
  13. setTimeout(function () {
  14. ul.hide();
  15. }, 200)
  16. });
  17.  
  18. //observe the field with keyup
  19. $(this).keyup(function (e) {
  20. var value = $(this).val().match('[^ ]*$')[0];
  21.  
  22. switch (e.which) {
  23. case 8: //Backspace
  24. case 13: //Enter
  25. case 27: //Escape
  26. case 37: //Left
  27. case 38: //Up
  28. case 39: //Right
  29. case 40: //Down
  30. case 46: //Delete
  31. break;
  32. default:
  33. if (value.length >= 3) {
  34. //Ajax call. atm substituted with a static content.
  35. $.getJSON(options.url + '?' + options.param + '=' + value, function (data) {
  36. //clear the list
  37. ul.find('li').remove();
  38.  
  39. //Render list and bind the up down array keys.
  40. $.each(data, function (key, item) {
  41. $('<li />').html(item).appendTo(ul);
  42. });
  43.  
  44. //Show if we got one or more hits
  45. if (ul.find('li').length > 0) {
  46. ul.show();
  47. }
  48.  
  49. //on a li click change the last word in the input box with the li one
  50. ul.find('li').click(function (e) {
  51. $.fn.ac.select(ul, element, $(this));
  52. index = -1;
  53. });
  54. });
  55. }
  56. break;
  57. }
  58.  
  59. e.preventDefault();
  60. });
  61.  
  62. $(this).keydown(function (e) {
  63. var testIndex = index;
  64. switch (e.which) {
  65. case 13: //Enter
  66. if (index == -1) {
  67. console.log('hej');
  68. $(this).parents('form:first').submit();
  69. return true;
  70. }
  71.  
  72. $.fn.ac.select(ul, element, ul.find('li:eq(' + testIndex + ')'))
  73.  
  74. index = -1;
  75. return false;
  76. break;
  77. case 46: //Delete
  78. case 8: //Backspace
  79. case 27: //Escape
  80. ul.hide();
  81. index = -1;
  82. break;
  83. case 38: //Up
  84. testIndex = index - 1;
  85. e.preventDefault();
  86. break;
  87. case 40: //Down
  88. testIndex = index + 1;
  89. e.preventDefault();
  90. break;
  91. }
  92.  
  93. //Default is prevented so lets move the mother fucker..
  94. if (e.isDefaultPrevented()) {
  95. var elm = ul.find('li:eq(' + testIndex + ')');
  96. if (elm.length > 0) {
  97. ul.find('li:eq(' + index + ')').removeClass('ac-selected');
  98. index = testIndex;
  99. elm.addClass('ac-selected');
  100. }
  101. }
  102. });
  103. };
  104.  
  105. $.fn.ac.select = function (ul, element, li) {
  106. element.val(element.val().replace(/[^ ]*$/, li.html()) + ' ');
  107. element.focus();
  108. ul.hide();
  109. }
  110.  
  111. $.fn.ac.defaults = {
  112. url : 'ac.json',
  113. param : 'p_prefix'
  114. }
Add Comment
Please, Sign In to add comment