| 1 |
(function ($) { |
| 2 |
|
| 3 |
var timer = 0; |
| 4 |
|
| 5 |
var sel = function (ptr, context) { |
| 6 |
if ($.isFunction(ptr)) |
| 7 |
return ptr.apply(context); |
| 8 |
return typeof ptr == "string" ? context.find(ptr) : ptr; |
| 9 |
}; |
| 10 |
|
| 11 |
var filter = function (obj, search, opts) { |
| 12 |
var re = new RegExp(search.replace(/[.\\\\+*?\[\]\{\}\(\)-]/g, "\\$&"), |
| 13 |
opts.caseSensitive ? "" : "i"); |
| 14 |
var ctx = sel(opts.element, obj); |
| 15 |
var n = 0; |
| 16 |
var size = opts.stepSize || 100; |
| 17 |
|
| 18 |
var step = function () { |
| 19 |
for (var i = 0; i < size; i++) { |
| 20 |
|
| 21 |
if (n >= ctx.length) { |
| 22 |
if (opts.after) |
| 23 |
opts.after.apply(ctx); |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
var e = $(ctx[n++]); |
| 28 |
|
| 29 |
var box = opts.subject ? sel(opts.subject, e) : this; |
| 30 |
var val = box.text(); |
| 31 |
|
| 32 |
if (!e.hasClass('hidden')) { |
| 33 |
if (!search.length) { |
| 34 |
e.show(); |
| 35 |
if (opts.highlight) { |
| 36 |
box.html(val); |
| 37 |
} |
| 38 |
} else if (val.match(re)) { |
| 39 |
e.show(); |
| 40 |
if (opts.highlight) { |
| 41 |
box.html(val.replace(re, opts.highlightRe)); |
| 42 |
} |
| 43 |
} else { |
| 44 |
e.hide(); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
timer = setTimeout(step, 1); |
| 49 |
}; |
| 50 |
|
| 51 |
clearTimeout(timer); |
| 52 |
step(); |
| 53 |
|
| 54 |
}; |
| 55 |
|
| 56 |
$.fn.searchFilter = function (opts) { |
| 57 |
if (opts.highlight) { |
| 58 |
opts.highlightRe = $("<div>@</div>").wrapInner(opts.highlight).html().replace(/@/g, "$$&"); |
| 59 |
} |
| 60 |
$(this).on("keyup", function () { |
| 61 |
filter(this, this.value, opts) |
| 62 |
}); |
| 63 |
$(this).on("change", function () { |
| 64 |
filter(this, this.value, opts) |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
})(window.jQuery); |