3 * JQuery QuickSearch - Hook up a form field to hide non-matching elements.
4 * $Id: quicksearch.js 53 2009-01-07 02:52:03Z deveiant $
6 * Author: Michael Granger <mgranger@laika.com>
9 jQuery
.fn
.quicksearch = function( target
, searchElems
, options
) {
10 // console.debug( "Quicksearch fn" );
15 highlightMatches
: false,
17 noSearchResultsIndicator
: null
19 if ( options
) $.extend( settings
, options
);
21 return jQuery(this).each( function() {
22 // console.debug( "Creating a new quicksearch on %o for %o", this, searchElems );
23 new jQuery
.quicksearch( this, searchElems
, settings
);
28 jQuery
.quicksearch = function( searchBox
, searchElems
, settings
) {
30 var boxdiv
= $(searchBox
).parents('div').eq(0);
33 setupKeyEventHandlers();
37 function setupKeyEventHandlers() {
38 // console.debug( "Hooking up the 'keypress' event to %o", searchBox );
41 keyup( function(e
) { return onSearchKey( e
.keyCode
); });
44 keypress( function(e
) {
46 // Execute the search on Enter, Tab, or Newline
50 clearTimeout( timeout
);
60 // Only allow valid search characters
62 return validQSChar( e
.charCode
);
67 function focusOnLoad() {
68 if ( !settings
.focusOnLoad
) return false;
72 function onSearchKey ( code
) {
73 clearTimeout( timeout
);
74 // console.debug( "...scheduling search." );
75 timeout
= setTimeout( doQuickSearch
, settings
.delay
);
78 function validQSChar( code
) {
79 var c
= String
.fromCharCode( code
);
82 (c
>= 'a' && c
<= 'z') ||
83 (c
>= 'A' && c
<= 'Z')
87 function doQuickSearch() {
88 var searchText
= searchBox
.value
;
89 var pat
= new RegExp( searchText
, "im" );
92 if ( settings
.noSearchResultsIndicator
) {
93 $('#' + settings
.noSearchResultsIndicator
).hide();
96 // All elements start out hidden
97 $(searchElems
).each( function(index
) {
98 var str
= $(this).text();
100 if ( pat
.test(str
) ) {
108 if ( shownCount
== 0 && settings
.noSearchResultsIndicator
) {
109 $('#' + settings
.noSearchResultsIndicator
).slideDown();