Skip to content

Commit 002d2ad

Browse files
committed
use coffeescript
1 parent 7157cfe commit 002d2ad

File tree

2 files changed

+177
-88
lines changed

2 files changed

+177
-88
lines changed

jquery.uitablefilter.coffee

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
###
2+
* Copyright (c) 2008 Greg Weber greg at gregweber.info
3+
* Dual licensed under the MIT and GPLv2 licenses just as jQuery is:
4+
* http://jquery.org/license
5+
*
6+
* documentation at http://gregweber.info/projects/uitablefilter
7+
*
8+
* allows table rows to be filtered (made invisible)
9+
* <code>
10+
* t = $('table')
11+
* $.uiTableFilter( t, phrase )
12+
* </code>
13+
* arguments:
14+
* jQuery object containing table rows
15+
* phrase to search for
16+
* optional arguments:
17+
* column to limit search too (the column title in the table header)
18+
* ifHidden - callback to execute if one or more elements was hidden
19+
###
20+
(($) ->
21+
$.uiTableFilter = (jq, phrase, column, ifHidden) ->
22+
new_hidden = false;
23+
return false if this.last_phrase == phrase
24+
25+
phrase_length = phrase.length
26+
words = phrase.toLowerCase().split(" ")
27+
28+
noMatch = (elem) -> elem.hide(); new_hidden = true
29+
# these function pointers may change
30+
getText = (elem) -> elem.text()
31+
matches = (elem) -> elem.show()
32+
33+
if column
34+
index = null
35+
jq.find("thead > tr:last > th").each( (i) ->
36+
if $.trim($(this).text()) == column
37+
index = i
38+
return false
39+
)
40+
if index == null
41+
throw("given column: " + column + " not found")
42+
43+
getText = (elem) -> $(elem.find( ("td:eq(" + index + ")") )).text()
44+
45+
# if added one letter to last time,
46+
# just check newest word and only need to hide
47+
if (words.size > 1) && (phrase.substr(0, phrase_length - 1) == this.last_phrase)
48+
if phrase[-1] == " "
49+
this.last_phrase = phrase
50+
return false
51+
52+
words = words[-1] # just search for the newest word
53+
54+
# only hide visible rows
55+
matches = (elem) ->
56+
elems = jq.find("tbody:first > tr:visible")
57+
else
58+
new_hidden = true
59+
elems = jq.find("tbody:first > tr")
60+
61+
elems.each(->
62+
elem = $(this)
63+
if $.uiTableFilter.has_words( getText(elem), words, false )
64+
matches(elem)
65+
else
66+
noMatch(elem)
67+
)
68+
69+
last_phrase = phrase
70+
ifHidden() if ifHidden && new_hidden
71+
return jq
72+
73+
# caching for speedup
74+
$.uiTableFilter.last_phrase = ""
75+
76+
# not jQuery dependent
77+
# "" [""] -> Boolean
78+
# "" [""] Boolean -> Boolean
79+
$.uiTableFilter.has_words = ( str, words, caseSensitive ) ->
80+
text = caseSensitive ? str : str.toLowerCase()
81+
for word in words
82+
return false if text.indexOf(word) == -1
83+
return true;
84+
)(jQuery)

jquery.uitablefilter.js

Lines changed: 93 additions & 88 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)