-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastsearch.js
52 lines (42 loc) · 1.34 KB
/
fastsearch.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
let callbackInit = $callback => {
if ($callback && typeof $callback === "function") {
$callback();
}
};
let eventInit = ($event, $elem, $function) => {
$elem.on($event, function() {
callbackInit($function);
});
};
const _fastsearchMethods = {
getCurrentVal: $input => $input.val().toLowerCase(),
filtration: ($items, $val, $valLength) => {
$items.filter(function() {
let _thisItem = $(this),
name = _thisItem.text().toLowerCase(),
nameSub = name.substr(0, $valLength);
if (nameSub.includes($val)) {
_thisItem.removeClass("--deactive");
}
});
},
filtrationInit: ($input, $items) => () => {
let currentVal = _fastsearchMethods.getCurrentVal($input),
currentValLength = currentVal.length;
$items.addClass("--deactive");
_fastsearchMethods.filtration($items, currentVal, currentValLength);
}
};
let fastsearch = $container => {
$container.each(function() {
let _container = $(this),
input = _container.find("[data-input]"),
items = _container.find("[data-item]");
eventInit(
"input",
input,
_fastsearchMethods.filtrationInit(input, items)
);
});
};
fastsearch($("[data-fastsearch]"));