1$.fn.ajaxSearch=function(options) { 2 var settings = { 3 url : './assets/sx.php', 4 preview : '#searchajax', 5 criteria : '#searchcriteria', 6 searchform : '#searchform', 7 minlength : 1, 8 maxhits: 6 9 }; 10 11 var lastquery=''; 12 var searchid=10000; 13 var previewvisible=false; 14 var activeLink=0; 15 16 return this.each(function() { 17 if (options) { 18 $.extend(settings,options); 19 } 20 21 $(this).attr('autocomplete','off').parent().attr('autocomplete','off'); 22 23 function hideAjaxPreview() { 24 if (previewvisible) { 25 $(settings.preview).slideUp(100); 26 previewvisible = false; 27 } 28 } 29 30 function showAjaxPreview() { 31 if (!previewvisible) { 32 $(settings.preview).slideDown(100); 33 previewvisible = true; 34 } 35 } 36 37 function setActiveLink(index) { 38 activeLink=index; 39 $(settings.preview).find('a').each(function(i){ 40 if (index==i) { 41 $(this).addClass('activelink'); 42 $(settings.criteria).val($(this).eq(0).html()); 43 } else { 44 $(this).removeClass('activelink'); 45 } 46 }); 47 } 48 49 $(this).keypress(function(e) { 50 if (e.keyCode==27) { 51 hideAjaxPreview(); 52 return false; 53 } 54 }); 55 56 $(this).blur(function() { 57 window.setTimeout(hideAjaxPreview, 150); 58 }); 59 60 $(this).keydown(function(e) { 61 if (e.keyCode==40) { 62 if (previewvisible) { 63 if (activeLink<$(settings.preview).find('a').length-1) { 64 setActiveLink(activeLink+1); 65 } 66 return false; 67 } 68 } 69 70 if (e.keyCode==38) { 71 if (previewvisible) { 72 if (activeLink>0) { 73 setActiveLink(activeLink-1);
74 } 75 return false; 76 } 77 } 78 }); 79 80 $(this).keyup(function(e) { 81 82 if (e.keyCode==40) return false; 83 if (e.keyCode==38) return false; 84 if (e.keyCode==13) return true; 85 if (e.keyCode==27) return true; 86 87 88 var query=this.value; 89 if (query!=lastquery) { 90 searchid++; 91 lastquery=query; 92 if (query.length >= settings.minlength) { 93 $.ajax({ 94 url: settings.url, 95 data: { 96 'q': query, 97 'id': searchid, 98 'max': settings.maxhits 99 }, 100 dataType: 'html', 101 success: function(data, t, x){ 102 if (data.substr(0, 5) == searchid) { 103 if (data.length > 5) { 104 $(settings.preview).html(data.substr(5)); 105 showAjaxPreview(); 106 setActiveLink(-1); 107 $(settings.preview).find('a').click(function(){ 108 $(settings.criteria).val(this.innerHTML); 109 $(settings.searchform).get(0).submit(); 110 return false; 111 }) 112 } else { 113 hideAjaxPreview(); 114 } 115 } 116 } 117 }) 118 } else { 119 if (previewvisible) { 120 hideAjaxPreview(); 121 } 122 } 123 } 124 }); 125 }); 126};
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.