1jQuery.fn.liveSearch = function (conf) { 2 var config = jQuery.extend({ 3 //url: '/search.php?q=', 4 id: 'jquery-live-search', 5 duration: 400, 6 typeDelay: 200, 7 loadingClass: 'search_loading', 8 onSlideUp: function () {}, 9 uptadePosition: false 10 }, conf); 11 12 var liveSearch = jQuery('#' + config.id); 13 14 // Create live-search if it doesn't exist 15 if (!liveSearch.length) { 16 liveSearch = jQuery('<div id="' + config.id + '"></div>') 17 .appendTo(document.body) 18 .hide() 19 .slideUp(0); 20 21 // Close live-search when clicking outside it 22 jQuery(document.body).click(function(event) { 23 var clicked = jQuery(event.target); 24 25 if (!(clicked.is('#' + config.id) || clicked.parents('#' + config.id).length || clicked.is('input'))) { 26 liveSearch.slideUp(config.duration, function () { 27 config.onSlideUp(); 28 }); 29 } 30 }); 31 } 32 33 return this.each(function () { 34 var input = jQuery(this).attr('autocomplete', 'off'); 35 var liveSearchPaddingBorderHoriz = parseInt(liveSearch.css('paddingLeft'), 10) + parseInt(liveSearch.css('paddingRight'), 10) + parseInt(liveSearch.css('borderLeftWidth'), 10) + parseInt(liveSearch.css('borderRightWidth'), 10); 36 37 // Re calculates live search's position 38 var repositionLiveSearch = function () { 39 var tmpOffset = input.offset(); 40 var inputDim = { 41 left: tmpOffset.left, 42 top: tmpOffset.top, 43 width: input.outerWidth(), 44 height: input.outerHeight() 45 }; 46 47 inputDim.topPos = inputDim.top + inputDim.height; 48 inputDim.totalWidth = inputDim.width - liveSearchPaddingBorderHoriz; 49 50 liveSearch.css({ 51 position: 'absolute', 52 left: inputDim.left + 'px', 53 top: inputDim.topPos + 'px', 54 width: inputDim.totalWidth + 'px' 55 }); 56 }; 57 58 // Shows live-search for this input 59 var showLiveSearch = function () { 60 // Always reposition the live-search every time it is shown 61 // in case user has resized browser-window or zoomed in or whatever 62 repositionLiveSearch(); 63 64 // We need to bind a resize-event every time live search is shown 65 // so it resizes based on the correct input element 66 jQuery(window).unbind('resize', repositionLiveSearch); 67 jQuery(window).bind('resize', repositionLiveSearch); 68 69 liveSearch.slideDown(config.duration); 70 }; 71 72 // Hides live-search for this input 73 var hideLiveSearch = function () { 74 liveSearch.slideUp(config.duration, function () { 75 config.onSlideUp(); 76 }); 77 }; 78 79 input 80 // On focus, if the live-search is empty, perform an new search 81 // If not, just slide it down. Only do this if there's something in the input 82 .focus(function () { 83 if (this.value !== '') { 84 // Perform a new search if there are no search results 85 if (liveSearch.html() == '') { 86 this.lastValue = '';
87 input.keyup(); 88 } 89 // If there are search results show live search 90 else { 91 // HACK: In case search field changes width onfocus 92 input.keyup(); 93 setTimeout(showLiveSearch, 1); 94 } 95 } 96 }) 97 // Auto update live-search onkeyup 98 .keyup(function () { 99 // Don't update live-search if it's got the same value as last time 100 if (this.value != this.lastValue) { 101 input.addClass(config.loadingClass); 102 103 var q = this.value.trim(); 104 var post_type = jQuery('#searchform #search_post_type').val(); 105 if( post_type == 'forum' ) post_type = ''; 106 107 // Stop previous ajax-request 108 if (this.timer) { 109 clearTimeout(this.timer); 110 } 111 112 // Start a new ajax-request in X ms 113 this.timer = setTimeout(function () { 114 jQuery.get(config.url + q + '&post_type=' + post_type, function (data) { 115 input.removeClass(config.loadingClass); 116 117 // Show live-search if results and search-term aren't empty 118 if (data.length && q.length) { 119 liveSearch.html(data); 120 showLiveSearch(); 121 } 122 else { 123 hideLiveSearch(); 124 } 125 }); 126 }, config.typeDelay); 127 128 this.lastValue = this.value; 129 } 130 }); 131 }); 132};
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.