-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjquery.livefilter.js
More file actions
77 lines (71 loc) · 2.28 KB
/
Copy pathjquery.livefilter.js
File metadata and controls
77 lines (71 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(function($) {
/** Plugin Main Function */
$.fn.livefilter = function(action, opts) {
if (typeof action === "undefined") {
action = "init";
}
if (typeof action === "object") {
opts = action;
action = "init";
}
if (action === "init") {
opts = $.extend({}, $.fn.livefilter.defaults, opts);
$(this).data("lf-data", opts);
$(this).unbind(".livefilter").bind("keyup.livefilter", $.fn.livefilter.onKey(opts));
} else if (action === "destroy") {
opts = $(this).data("lf-data");
$(opts.selector).filter("." + opts.hiddenClass)[opts.showFn]();
$(this).unbind(".livefilter");
} else {
log("action unknown", action);
}
return $(this);
};
/** keyup event action */
$.fn.livefilter.onKey = function(opts) {
var rv = function() {
var v = $(this).val();
log("filtering", v);
if (v) {
$(opts.selector).filter(":icontains(" + v + ")")[opts.showFn]().removeClass(opts.hiddenClass).end().filter(":not(:icontains(" + v + "))")[opts.hideFn]().addClass(opts.hiddenClass);
} else {
$(opts.selector).removeClass(opts.hiddenClass)[opts.showFn]();
}
};
if (opts.debounce) {
log("debounce active", opts.debounce);
rv = $.fn.livefilter.debounce(rv, opts.debounce);
}
return rv;
};
/** Delay execution of too frequent tasks*/
$.fn.livefilter.debounce = function(func, threshold) {
var timeout;
return function debounced() {
var obj = this, args = arguments;
function delayed() {
func.apply(obj, args);
timeout = null;
}
if (timeout) clearTimeout(timeout);
timeout = setTimeout(delayed, threshold || 100);
};
};
/** Case insensitive :contains jQuery pseudo selector */
$.expr[":"].icontains = function(obj, index, meta, stack) {
return (obj.textContent || obj.innerText || jQuery(obj).text() || "").toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
};
var log=function(){
if (typeof console!=='undefined' && console.log){
console.log(Array.prototype.slice.call(arguments).join(', '));
}
};
/** Plugin global defaults */
$.fn.livefilter.defaults = {
selector: "tbody tr",
debounce: 500,
hiddenClass: "lf-hidden",
showFn: "show",
hideFn: "hide"
};
})(jQuery);