forked from chromawoods/instaFilta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstafilta.js
97 lines (67 loc) · 3.07 KB
/
instafilta.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*!
* instaFilta
* Description: "jQuery plugin for performing in-page filtering"
* Version: "1.1.0"
* Homepage: https://github.com/chromawoods/instaFilta
* Author: Andreas Larsson <[email protected]> (http://chromawoods.com)
*/
;(function($) {
$.fn.instaFilta = function(options) {
var settings = $.extend({
targets: '.instafilta-target',
sections: '.instafilta-section',
matchCssClass: 'instafilta-match',
markMatches: false,
hideEmptySections: true,
beginsWith: false,
caseSensitive: false,
typeDelay: 0
}, options);
var typeTimer,
$targets = $(settings.targets),
$sections = $(settings.sections),
lastTerm = '';
var hideEmptySections = function() {
$sections.each(function() {
var $section = $(this);
$section.toggle(!!($section.find('[data-instafilta-hide="false"]').length));
});
};
var doFiltering = function(term) {
term = settings.caseSensitive ? term : term.toLowerCase();
if (lastTerm === term) { return false; }
else { lastTerm = term; }
if (!term) {
$targets.attr('data-instafilta-hide', 'false').show();
$sections.show();
}
$targets.each(function() {
var $item = $(this),
originalText = $item.text(),
targetText = settings.caseSensitive ? originalText : originalText.toLowerCase(),
matchedIndex = targetText.indexOf(term),
matchedText = null;
if (!$item.data('originalText')) {
$item.data('originalText', originalText);
}
else { $item.html($item.data('originalText')); }
if (matchedIndex >= 0 && settings.markMatches) {
matchedText = originalText.substring(matchedIndex, matchedIndex + term.length);
$item.html(originalText.replace(matchedText, '<span class="' + settings.matchCssClass + '">' + matchedText + '</span>'));
}
$item.attr('data-instafilta-hide', (settings.beginsWith && matchedIndex !== 0) || matchedIndex < 0 ? 'true' : 'false');
});
$targets.filter('[data-instafilta-hide="true"]').hide();
$targets.filter('[data-instafilta-hide="false"]').show();
settings.hideEmptySections && hideEmptySections();
};
var onKeyUp = function() {
var $field = $(this);
clearTimeout(typeTimer);
typeTimer = setTimeout(function() {
doFiltering($field.val());
}, settings.typeDelay);
};
return $(this).on('keyup', onKeyUp);
};
}(jQuery));