|
18 | 18 | * column to limit search too (the column title in the table header)
|
19 | 19 | * ifHidden - callback to execute if one or more elements was hidden
|
20 | 20 | */
|
21 |
| -jQuery.uiTableFilter = function(jq, phrase, column, ifHidden){ |
22 |
| - var new_hidden = false; |
23 |
| - if( this.last_phrase === phrase ) return false; |
| 21 | +(function($) { |
| 22 | + $.uiTableFilter = function(jq, phrase, column, ifHidden){ |
| 23 | + var new_hidden = false; |
| 24 | + if( this.last_phrase === phrase ) return false; |
24 | 25 |
|
25 |
| - var phrase_length = phrase.length; |
26 |
| - var words = phrase.toLowerCase().split(" "); |
| 26 | + var phrase_length = phrase.length; |
| 27 | + var words = phrase.toLowerCase().split(" "); |
27 | 28 |
|
28 |
| - // these function pointers may change |
29 |
| - var matches = function(elem) { elem.show() } |
30 |
| - var noMatch = function(elem) { elem.hide(); new_hidden = true } |
31 |
| - var getText = function(elem) { return elem.text() } |
| 29 | + // these function pointers may change |
| 30 | + var matches = function(elem) { elem.show() } |
| 31 | + var noMatch = function(elem) { elem.hide(); new_hidden = true } |
| 32 | + var getText = function(elem) { return elem.text() } |
32 | 33 |
|
33 |
| - if( column ) { |
34 |
| - var index = null; |
35 |
| - jq.find("thead > tr:last > th").each( function(i){ |
36 |
| - if( $.trim($(this).text()) == column ){ |
37 |
| - index = i; return false; |
38 |
| - } |
39 |
| - }); |
40 |
| - if( index == null ) throw("given column: " + column + " not found") |
| 34 | + if( column ) { |
| 35 | + var index = null; |
| 36 | + jq.find("thead > tr:last > th").each( function(i){ |
| 37 | + if( $.trim($(this).text()) == column ){ |
| 38 | + index = i; return false; |
| 39 | + } |
| 40 | + }); |
| 41 | + if( index == null ) throw("given column: " + column + " not found") |
41 | 42 |
|
42 |
| - getText = function(elem){ return jQuery(elem.find( |
43 |
| - ("td:eq(" + index + ")") )).text() |
| 43 | + getText = function(elem){ return $(elem.find( |
| 44 | + ("td:eq(" + index + ")") )).text() |
| 45 | + } |
44 | 46 | }
|
45 |
| - } |
46 | 47 |
|
47 |
| - // if added one letter to last time, |
48 |
| - // just check newest word and only need to hide |
49 |
| - if( (words.size > 1) && (phrase.substr(0, phrase_length - 1) === |
50 |
| - this.last_phrase) ) { |
| 48 | + // if added one letter to last time, |
| 49 | + // just check newest word and only need to hide |
| 50 | + if( (words.size > 1) && (phrase.substr(0, phrase_length - 1) === |
| 51 | + this.last_phrase) ) { |
51 | 52 |
|
52 |
| - if( phrase[-1] === " " ) |
53 |
| - { this.last_phrase = phrase; return false; } |
| 53 | + if( phrase[-1] === " " ) |
| 54 | + { this.last_phrase = phrase; return false; } |
54 | 55 |
|
55 |
| - var words = words[-1]; // just search for the newest word |
| 56 | + var words = words[-1]; // just search for the newest word |
56 | 57 |
|
57 |
| - // only hide visible rows |
58 |
| - matches = function(elem) {;} |
59 |
| - var elems = jq.find("tbody > tr:visible") |
60 |
| - } |
61 |
| - else { |
62 |
| - new_hidden = true; |
63 |
| - var elems = jq.find("tbody > tr") |
64 |
| - } |
| 58 | + // only hide visible rows |
| 59 | + matches = function(elem) {;} |
| 60 | + var elems = jq.find("tbody > tr:visible") |
| 61 | + } |
| 62 | + else { |
| 63 | + new_hidden = true; |
| 64 | + var elems = jq.find("tbody > tr") |
| 65 | + } |
65 | 66 |
|
66 |
| - elems.each(function(){ |
67 |
| - var elem = jQuery(this); |
68 |
| - jQuery.uiTableFilter.has_words( getText(elem), words, false ) ? |
69 |
| - matches(elem) : noMatch(elem); |
70 |
| - }); |
| 67 | + elems.each(function(){ |
| 68 | + var elem = $(this); |
| 69 | + $.uiTableFilter.has_words( getText(elem), words, false ) ? |
| 70 | + matches(elem) : noMatch(elem); |
| 71 | + }); |
71 | 72 |
|
72 |
| - last_phrase = phrase; |
73 |
| - if( ifHidden && new_hidden ) ifHidden(); |
74 |
| - return jq; |
75 |
| -}; |
| 73 | + last_phrase = phrase; |
| 74 | + if( ifHidden && new_hidden ) ifHidden(); |
| 75 | + return jq; |
| 76 | + }; |
76 | 77 |
|
77 |
| -// caching for speedup |
78 |
| -jQuery.uiTableFilter.last_phrase = "" |
| 78 | + // caching for speedup |
| 79 | + $.uiTableFilter.last_phrase = "" |
79 | 80 |
|
80 |
| -// not jQuery dependent |
81 |
| -// "" [""] -> Boolean |
82 |
| -// "" [""] Boolean -> Boolean |
83 |
| -jQuery.uiTableFilter.has_words = function( str, words, caseSensitive ) |
84 |
| -{ |
85 |
| - var text = caseSensitive ? str : str.toLowerCase(); |
86 |
| - for (var i=0; i < words.length; i++) { |
87 |
| - if (text.indexOf(words[i]) === -1) return false; |
| 81 | + // not jQuery dependent |
| 82 | + // "" [""] -> Boolean |
| 83 | + // "" [""] Boolean -> Boolean |
| 84 | + $.uiTableFilter.has_words = function( str, words, caseSensitive ) |
| 85 | + { |
| 86 | + var text = caseSensitive ? str : str.toLowerCase(); |
| 87 | + for (var i=0; i < words.length; i++) { |
| 88 | + if (text.indexOf(words[i]) === -1) return false; |
| 89 | + } |
| 90 | + return true; |
88 | 91 | }
|
89 |
| - return true; |
90 |
| -} |
| 92 | +}) (jQuery); |
0 commit comments