Skip to content

Commit b60555d

Browse files
author
Gregory
committed
use closure for jQuery variable
1 parent d1eaff2 commit b60555d

File tree

1 file changed

+58
-56
lines changed

1 file changed

+58
-56
lines changed

jquery.uitablefilter.js

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,75 @@
1818
* column to limit search too (the column title in the table header)
1919
* ifHidden - callback to execute if one or more elements was hidden
2020
*/
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;
2425

25-
var phrase_length = phrase.length;
26-
var words = phrase.toLowerCase().split(" ");
26+
var phrase_length = phrase.length;
27+
var words = phrase.toLowerCase().split(" ");
2728

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() }
3233

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")
4142

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+
}
4446
}
45-
}
4647

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) ) {
5152

52-
if( phrase[-1] === " " )
53-
{ this.last_phrase = phrase; return false; }
53+
if( phrase[-1] === " " )
54+
{ this.last_phrase = phrase; return false; }
5455

55-
var words = words[-1]; // just search for the newest word
56+
var words = words[-1]; // just search for the newest word
5657

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+
}
6566

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+
});
7172

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+
};
7677

77-
// caching for speedup
78-
jQuery.uiTableFilter.last_phrase = ""
78+
// caching for speedup
79+
$.uiTableFilter.last_phrase = ""
7980

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;
8891
}
89-
return true;
90-
}
92+
}) (jQuery);

0 commit comments

Comments
 (0)