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 8(function($){ 9 $.fn.autoComplete = function(options){ 10 var o = $.extend({}, $.fn.autoComplete.defaults, options); 11 12 // public methods 13 if (typeof options == 'string') { 14 this.each(function(){ 15 var that = $(this); 16 if (options == 'destroy') { 17 $(window).off('resize.autocomplete', that.updateSC); 18 that.off('blur.autocomplete focus.autocomplete keydown.autocomplete keyup.autocomplete'); 19 if (that.data('autocomplete')) 20 that.attr('autocomplete', that.data('autocomplete')); 21 else 22 that.removeAttr('autocomplete'); 23 $(that.data('sc')).remove(); 24 that.removeData('sc').removeData('autocomplete'); 25 } 26 }); 27 return this; 28 } 29 30 return this.each(function(){ 31 var that = $(this); 32 // sc = 'suggestions container' 33 that.sc = $('<div class="autocomplete-suggestions autocomplete-wrapper-custom'+o.menuClass+'"></div>'); 34 that.data('sc', that.sc).data('autocomplete', that.attr('autocomplete')); 35 that.attr('autocomplete', 'off'); 36 that.cache = {}; 37 that.last_val = ''; 38 39 that.updateSC = function(resize, next){ 40 that.sc.css({ 41 top: that.offset().top + that.outerHeight(), 42 left: that.offset().left, 43 width: that.outerWidth() 44 }); 45 if (!resize) { 46 that.sc.show(); 47 if (!that.sc.maxHeight) that.sc.maxHeight = parseInt(that.sc.css('max-height')); 48 if (!that.sc.suggestionHeight) that.sc.suggestionHeight = $('.autocomplete-suggestion', that.sc).first().outerHeight(); 49 if (that.sc.suggestionHeight) 50 if (!next) that.sc.scrollTop(0); 51 else { 52 var scrTop = that.sc.scrollTop(), selTop = next.offset().top - that.sc.offset().top; 53 if (selTop + that.sc.suggestionHeight - that.sc.maxHeight > 0) 54 that.sc.scrollTop(selTop + that.sc.suggestionHeight + scrTop - that.sc.maxHeight); 55 else if (selTop < 0) 56 that.sc.scrollTop(selTop + scrTop); 57 } 58 } 59 } 60 $(window).on('resize.autocomplete', that.updateSC); 61 62 that.sc.appendTo('body'); 63 64 that.sc.on('mouseleave', '.autocomplete-suggestion', function (){ 65 $('.autocomplete-suggestion.selected').removeClass('selected'); 66 }); 67 68 that.sc.on('mouseenter', '.autocomplete-suggestion', function (){ 69 $('.autocomplete-suggestion.selected').removeClass('selected'); 70 $(this).addClass('selected'); 71 }); 72 73 that.sc.on('mousedown click', '.autocomplete-suggestion', function (e){ 74 var item = $(this), v = item.data('val'); 75 if (v || item.hasClass('autocomplete-suggestion')) { // else outside click 76 that.val(v); 77 o.onSelect(e, v, item); 78 that.sc.hide(); 79 } 80 return false; 81 }); 82 83 that.on('blur.autocomplete', function(){ 84 try { over_sb = $('.autocomplete-suggestions:hover').length; } catch(e){ over_sb = 0; } // IE7 fix :hover 85 if (!over_sb) { 86 that.last_val = that.val(); 87 that.sc.hide(); 88 setTimeout(function(){ that.sc.hide(); }, 350); // hide suggestions on fast input 89 } else if (!that.is(':focus')) setTimeout(function(){ that.focus(); }, 20); 90 }); 91 92 if (!o.minChars) that.on('focus.autocomplete', function(){ that.last_val = '\n'; 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 for (var i=0;i<data.length;i++) s += o.renderItem(data[i], val); 100 that.sc.html(s); 101 that.updateSC(0); 102 } 103 else 104 that.sc.hide(); 105 } 106 107 that.on('keydown.autocomplete', function(e){ 108 // down (40), up (38) 109 if ((e.which == 40 || e.which == 38) && that.sc.html()) { 110 var next, sel = $('.autocomplete-suggestion.selected', that.sc); 111 if (!sel.length) { 112 next = (e.which == 40) ? $('.autocomplete-suggestion', that.sc).first() : $('.autocomplete-suggestion', that.sc).last(
112); 113 that.val(next.addClass('selected').data('val')); 114 } else { 115 next = (e.which == 40) ? sel.next('.autocomplete-suggestion') : sel.prev('.autocomplete-suggestion'); 116 if (next.length) { sel.removeClass('selected'); that.val(next.addClass('selected').data('val')); } 117 else { sel.removeClass('selected'); that.val(that.last_val); next = 0; } 118 } 119 that.updateSC(0, next); 120 return false; 121 } 122 // esc 123 else if (e.which == 27) that.val(that.last_val).sc.hide(); 124 // enter or tab 125 else if (e.which == 13 || e.which == 9) { 126 var sel = $('.autocomplete-suggestion.selected', that.sc); 127 if (sel.length && that.sc.is(':visible')) { o.onSelect(e, sel.data('val'), sel); setTimeout(function(){ that.sc.hide(); }, 20); } 128 } 129 }); 130 131 that.on('keyup.autocomplete', function(e){ 132 if (!~$.inArray(e.which, [13, 27, 35, 36, 37, 38, 39, 40])) { 133 var val = that.val(); 134 if (val.length >= o.minChars) { 135 if (val != that.last_val) { 136 that.last_val = val; 137 clearTimeout(that.timer); 138 if (o.cache) { 139 if (val in that.cache) { suggest(that.cache[val]); return; } 140 // no requests if previous suggestions were empty 141 for (var i=1; i<val.length-o.minChars; i++) { 142 var part = val.slice(0, val.length-i); 143 if (part in that.cache && !that.cache[part].length) { suggest([]); return; } 144 } 145 } 146 that.timer = setTimeout(function(){ o.source(val, suggest) }, o.delay); 147 } 148 } else { 149 that.last_val = val; 150 that.sc.hide(); 151 } 152 } 153 }); 154 }); 155 } 156 157 $.fn.autoComplete.defaults = { 158 source: 0, 159 minChars: 3, 160 delay: 1000, 161 cache: 1, 162 menuClass: '', 163 renderItem: function (item, search){ 164 // escape special characters 165 search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); 166 var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi"); 167 return '<div class="autocomplete-suggestion" data-val="' + item + '">' + item.replace(re, "<b>$1</b>") + '</div>'; 168 }, 169 onSelect: function(e, term, item){} 170 }; 171}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.