| 1 |
(function($) { |
| 2 |
|
| 3 |
var sel = function(ptr, context) { |
| 4 |
if($.isFunction(ptr)) |
| 5 |
return ptr.apply(context); |
| 6 |
return typeof ptr == "string" ? context.find(ptr) : ptr; |
| 7 |
}; |
| 8 |
|
| 9 |
var filter = function(obj, search, opts) { |
| 10 |
var re = new RegExp(search.replace(/[.\\\\+*?\[\]\{\}\(\)-]/g, "\\$&"), |
| 11 |
opts.caseSensitive ? "" : "i"); |
| 12 |
var ctx = sel(opts.element, obj); |
| 13 |
|
| 14 |
ctx.each(function() { |
| 15 |
var e = $(this); |
| 16 |
|
| 17 |
var box = opts.subject ? sel(opts.subject, e) : this; |
| 18 |
var val = box.text(); |
| 19 |
|
| 20 |
if(!search.length) { |
| 21 |
e.show(); |
| 22 |
if(opts.highlight) { |
| 23 |
box.html(val); |
| 24 |
} |
| 25 |
} else if(val.match(re)) { |
| 26 |
e.show(); |
| 27 |
if(opts.highlight) { |
| 28 |
box.html(val.replace(re, opts.highlightRe)); |
| 29 |
} |
| 30 |
} else { |
| 31 |
e.hide(); |
| 32 |
} |
| 33 |
|
| 34 |
}); |
| 35 |
|
| 36 |
if(opts.after) |
| 37 |
opts.after.apply(ctx); |
| 38 |
|
| 39 |
}; |
| 40 |
|
| 41 |
$.fn.searchFilter = function(opts) { |
| 42 |
if(opts.highlight) { |
| 43 |
opts.highlightRe = $("<div>@</div>").wrapInner(opts.highlight).html().replace(/@/g, "$$&"); |
| 44 |
} |
| 45 |
$(this).keyup(function() { filter(this, this.value, opts) }); |
| 46 |
$(this).change(function() { filter(this, this.value, opts) }); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
})(window.jQuery); |
| 51 |
|