-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.dom.filter.text.babel.js
68 lines (52 loc) · 2.09 KB
/
jquery.dom.filter.text.babel.js
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
(function($) {
$.fn.domFilterText = function (options) {
let self = this;
let opts = $.extend(true, {}, $.fn.domFilterText.defaults, options);
let cacheCss = (targets) => {
targets.each((i, el) => {
$(el).data('display', $(el).css('display'));
});
if (targets.children().length) {
cacheCss(targets.children());
}
};
cacheCss(opts.filterTarget);
return self.each((i, el) => {
if (el.nodeName.toLowerCase() !== 'input' || el.type.toLowerCase() !== 'text') {
console.warn(`(el.id ? el.id : 'Node') type is invalid, use <input type='text'> instead.`);
return false;
}
$(self).on('keyup', (e) => {
let filterVal = $(e.target).val();
let filter = (targets) => {
targets.each((i, el) => {
$(el).css('display', $(el).data('display'));
});
let targetRemove = targets.filter((i, el) => {
let comparator = '';
if (opts.filterProperty === 'html') {
comparator = $(el).html();
} else {
comparator = $(el).filterProperty;
}
if (opts.caseSensitive) {
return comparator.indexOf(filterVal) < 0;
} else {
return comparator.toLowerCase().indexOf(filterVal.toLowerCase()) < 0;
}
});
targetRemove.not(e.target).css('display', 'none');
if (targets.children().length) {
filter(targets.children());
}
};
filter(opts.filterTarget);
});
});
};
$.fn.domFilterText.defaults = {
filterTarget: $(document.body),
filterProperty: 'html',
caseSensitive: false
};
}(jQuery));