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 '+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', '.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 }); 81 82 that.on('blur.autocomplete', function(){ 83 try { over_sb = $('.autocomplete-suggestions:hover').length; } catch(e){ over_sb = 0; } // IE7 fix :hover 84 if (!over_sb) { 85 that.last_val = that.val(); 86 that.sc.hide(); 87 setTimeout(function(){ that.sc.hide(); }, 350); // hide suggestions on fast input 88 } else if (!that.is(':focus')) setTimeout(function(){ that.focus(); }, 20); 89 }); 90 91 if (!o.minChars) that.on('focus.autocomplete', function(){ that.last_val = '\n'; that.trigger('keyup.autocomplete'); }); 92 93 function suggest(data){ 94 var val = that.val(); 95 that.cache[val] = data; 96 if (data.length && val.length >= o.minChars) { 97 var s = ''; 98 for (var i=0;i<data.length;i++) s += o.renderItem(data[i], val); 99 that.sc.html(s); 100 that.updateSC(0); 101 } 102 else 103 that.sc.hide(); 104 } 105 106 that.on('keydown.autocomplete', function(e){ 107 // down (40), up (38) 108 if ((e.which == 40 || e.which == 38) && that.sc.html()) { 109 var next, sel = $('.autocomplete-suggestion.selected', that.sc); 110 if (!sel.length) { 111 next = (e.which == 40) ? $('.autocomplete-suggestion', that.sc).first() : $('.autocomplete-suggestion', that.sc).last(
111); 112 that.val(next.addClass('selected').data('val')); 113 } else { 114 next = (e.which == 40) ? sel.next('.autocomplete-suggestion') : sel.prev('.autocomplete-suggestion'); 115 if (next.length) { sel.removeClass('selected'); that.val(next.addClass('selected').data('val')); } 116 else { sel.removeClass('selected'); that.val(that.last_val); next = 0; } 117 } 118 that.updateSC(0, next); 119 return false; 120 } 121 // esc 122 else if (e.which == 27) that.val(that.last_val).sc.hide(); 123 // enter or tab 124 else if (e.which == 13 || e.which == 9) { 125 // KST 2016-09-13 // 126 setTimeout(function(){ that.sc.hide(); }, 200); 127 // ENDE KST // 128 var sel = $('.autocomplete-suggestion.selected', that.sc); 129 if (sel.length && that.sc.is(':visible')) { o.onSelect(e, sel.data('val'), sel); setTimeout(function(){ that.sc.hide(); }, 20); } 130 } 131 }); 132 133 that.on('keyup.autocomplete', function(e){ 134 if (!~$.inArray(e.which, [13, 27, 35, 36, 37, 38, 39, 40])) { 135 var val = that.val(); 136 if (val.length >= o.minChars) { 137 if (val != that.last_val) { 138 that.last_val = val; 139 clearTimeout(that.timer); 140 if (o.cache) { 141 if (val in that.cache) { suggest(that.cache[val]); return; } 142 // no requests if previous suggestions were empty 143 for (var i=1; i<val.length-o.minChars; i++) { 144 var part = val.slice(0, val.length-i); 145 if (part in that.cache && !that.cache[part].length) { suggest([]); return; } 146 } 147 } 148 that.timer = setTimeout(function(){ o.source(val, suggest) }, o.delay); 149 } 150 } else { 151 that.last_val = val; 152 that.sc.hide(); 153 } 154 } 155 }); 156 }); 157 } 158 159 $.fn.autoComplete.defaults = { 160 source: 0, 161 minChars: 3, 162 delay: 150, 163 cache: 1, 164 menuClass: '', 165 renderItem: function (item, search){ 166 // escape special characters 167 search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); 168 var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi"); 169 return '<div class="autocomplete-suggestion" data-val="' + item + '">' + item.replace(re, "<b>$1</b>") + '</div>'; 170 }, 171 onSelect: function(e, term, item){} 172 }; 173}(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.