jquery.autocomplete.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. jQuery autoComplete v1.0.7
  3. Copyright (c) 2014 Simon Steinberger / Pixabay
  4. GitHub: https://github.com/Pixabay/jQuery-autoComplete
  5. License: http://www.opensource.org/licenses/mit-license.php
  6. */
  7. (function ($) {
  8. $.fn.autoComplete = function (options) {
  9. var o = $.extend({}, $.fn.autoComplete.defaults, options);
  10. // public methods
  11. if (typeof options == 'string') {
  12. this.each(function () {
  13. var that = $(this);
  14. if (options == 'destroy') {
  15. $(window).off('resize.autocomplete', that.updateSC);
  16. that.off('blur.autocomplete focus.autocomplete keydown.autocomplete keyup.autocomplete');
  17. if (that.data('autocomplete'))
  18. that.attr('autocomplete', that.data('autocomplete'));
  19. else
  20. that.removeAttr('autocomplete');
  21. $(that.data('sc')).remove();
  22. that.removeData('sc').removeData('autocomplete');
  23. }
  24. });
  25. return this;
  26. }
  27. return this.each(function () {
  28. var that = $(this);
  29. // sc = 'suggestions container'
  30. that.sc = $('<div class="autocomplete-suggestions ' + o.menuClass + '"></div>');
  31. that.data('sc', that.sc).data('autocomplete', that.attr('autocomplete'));
  32. that.attr('autocomplete', 'off');
  33. that.cache = {};
  34. that.last_val = '';
  35. that.updateSC = function (resize, next) {
  36. that.sc.css({
  37. top: that.offset().top + that.outerHeight(),
  38. left: that.offset().left,
  39. width: that.outerWidth()
  40. });
  41. if (!resize) {
  42. that.sc.show();
  43. if (!that.sc.maxHeight) that.sc.maxHeight = parseInt(that.sc.css('max-height'));
  44. if (!that.sc.suggestionHeight) that.sc.suggestionHeight = $('.autocomplete-suggestion', that.sc).first().outerHeight();
  45. if (that.sc.suggestionHeight)
  46. if (!next) that.sc.scrollTop(0);
  47. else {
  48. var scrTop = that.sc.scrollTop(), selTop = next.offset().top - that.sc.offset().top;
  49. if (selTop + that.sc.suggestionHeight - that.sc.maxHeight > 0)
  50. that.sc.scrollTop(selTop + that.sc.suggestionHeight + scrTop - that.sc.maxHeight);
  51. else if (selTop < 0)
  52. that.sc.scrollTop(selTop + scrTop);
  53. }
  54. }
  55. }
  56. $(window).on('resize.autocomplete', that.updateSC);
  57. that.sc.appendTo('body');
  58. that.sc.on('mouseleave', '.autocomplete-suggestion', function () {
  59. $('.autocomplete-suggestion.selected').removeClass('selected');
  60. });
  61. that.sc.on('mouseenter', '.autocomplete-suggestion', function () {
  62. $('.autocomplete-suggestion.selected').removeClass('selected');
  63. $(this).addClass('selected');
  64. });
  65. that.sc.on('mousedown click', '.autocomplete-suggestion', function (e) {
  66. var item = $(this), v = item.data('val');
  67. if (v || item.hasClass('autocomplete-suggestion')) { // else outside click
  68. that.val(v);
  69. o.onSelect(e, v, item);
  70. that.sc.hide();
  71. }
  72. return false;
  73. });
  74. that.on('blur.autocomplete', function () {
  75. try {
  76. over_sb = $('.autocomplete-suggestions:hover').length;
  77. } catch (e) {
  78. over_sb = 0;
  79. } // IE7 fix :hover
  80. if (!over_sb) {
  81. that.last_val = that.val();
  82. that.sc.hide();
  83. setTimeout(function () {
  84. that.sc.hide();
  85. }, 350); // hide suggestions on fast input
  86. } else if (!that.is(':focus')) setTimeout(function () {
  87. that.focus();
  88. }, 20);
  89. });
  90. if (!o.minChars) that.on('focus.autocomplete', function () {
  91. that.last_val = '\n';
  92. that.trigger('keyup.autocomplete');
  93. });
  94. function suggest(data) {
  95. var val = that.val();
  96. that.cache[val] = data;
  97. if (data.length && val.length >= o.minChars) {
  98. var s = '';
  99. if (data.length > 0) {
  100. s += typeof o.header === 'function' ? o.header.call(data, o, that) : o.header;
  101. for (var i = 0; i < data.length; i++) s += o.renderItem(data[i], val);
  102. s += typeof o.footer === 'function' ? o.footer.call(data, o, that) : o.footer;
  103. }
  104. that.sc.html(s);
  105. that.updateSC(0);
  106. }
  107. else
  108. that.sc.hide();
  109. }
  110. that.on('keydown.autocomplete', function (e) {
  111. // down (40), up (38)
  112. if ((e.which == 40 || e.which == 38) && that.sc.html()) {
  113. var next, sel = $('.autocomplete-suggestion.selected', that.sc);
  114. if (!sel.length) {
  115. next = (e.which == 40) ? $('.autocomplete-suggestion', that.sc).first() : $('.autocomplete-suggestion', that.sc).last();
  116. that.val(next.addClass('selected').data('val'));
  117. } else {
  118. next = (e.which == 40) ? sel.next('.autocomplete-suggestion') : sel.prev('.autocomplete-suggestion');
  119. if (next.length) {
  120. sel.removeClass('selected');
  121. that.val(next.addClass('selected').data('val'));
  122. }
  123. else {
  124. sel.removeClass('selected');
  125. that.val(that.last_val);
  126. next = 0;
  127. }
  128. }
  129. that.updateSC(0, next);
  130. return false;
  131. }
  132. // esc
  133. else if (e.which == 27) that.val(that.last_val).sc.hide();
  134. // enter or tab
  135. else if (e.which == 13 || e.which == 9) {
  136. var sel = $('.autocomplete-suggestion.selected', that.sc);
  137. if (sel.length && that.sc.is(':visible')) {
  138. o.onSelect(e, sel.data('val'), sel);
  139. setTimeout(function () {
  140. that.sc.hide();
  141. }, 20);
  142. }
  143. }
  144. });
  145. that.on('keyup.autocomplete', function (e) {
  146. if (!~$.inArray(e.which, [13, 27, 35, 36, 37, 38, 39, 40])) {
  147. var val = that.val();
  148. if (val.length >= o.minChars) {
  149. if (val != that.last_val) {
  150. that.last_val = val;
  151. clearTimeout(that.timer);
  152. if (o.cache) {
  153. if (val in that.cache) {
  154. suggest(that.cache[val]);
  155. return;
  156. }
  157. // no requests if previous suggestions were empty
  158. for (var i = 1; i < val.length - o.minChars; i++) {
  159. var part = val.slice(0, val.length - i);
  160. if (part in that.cache && !that.cache[part].length) {
  161. suggest([]);
  162. return;
  163. }
  164. }
  165. }
  166. that.timer = setTimeout(function () {
  167. o.source(val, suggest)
  168. }, o.delay);
  169. }
  170. } else {
  171. that.last_val = val;
  172. that.sc.hide();
  173. }
  174. }
  175. });
  176. });
  177. }
  178. $.fn.autoComplete.defaults = {
  179. source: 0,
  180. minChars: 3,
  181. delay: 150,
  182. cache: 1,
  183. menuClass: '',
  184. header: '',
  185. footer: '',
  186. renderItem: function (item, search) {
  187. // escape special characters
  188. search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  189. var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
  190. return '<div class="autocomplete-suggestion" data-val="' + item + '">' + item.replace(re, "<b>$1</b>") + '</div>';
  191. },
  192. onSelect: function (e, term, item) {
  193. }
  194. };
  195. }(jQuery));