-
Notifications
You must be signed in to change notification settings - Fork 12
/
instafilta.js
277 lines (198 loc) · 9.38 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*!
* instaFilta
* Version: 1.4.4
* Description: jQuery plugin for performing in-page filtering
* Homepage and documentation: https://github.com/chromawoods/instaFilta
* Author: Andreas Larsson <[email protected]> (http://chromawoods.com)
* Contributions: Jaap-Jan Frans
*/
;(function($) {
$.fn.instaFilta = function(options) {
var _filterTerm = null,
_filterCategory = null;
/* Default settings. */
var settings = $.extend({
scope: null,
targets: '.instafilta-target',
sections: '.instafilta-section',
categoryDataAttr: 'instafilta-category',
matchCssClass: 'instafilta-match',
itemsHideEffect: 'hide',
itemsHideDuration: 0,
itemsShowEffect: 'show',
itemsShowDuration: 0,
sectionsHideEffect: 'hide',
sectionsHideDuration: 0,
sectionsShowEffect: 'show',
sectionsShowDuration: 0,
onFilterComplete: null,
markMatches: false,
hideEmptySections: true,
beginsWith: false,
caseSensitive: false,
typeDelay: 0,
useSynonyms: true,
synonyms: [
{ src: 'à,á,å,ä,â,ã', dst: 'a' },
{ src: 'À,Á,Å,Ä,Â,Ã', dst: 'A' },
{ src: 'è,é,ë,ê', dst: 'e' },
{ src: 'È,É,Ë,Ê', dst: 'E' },
{ src: 'ì,í,ï,î', dst: 'i' },
{ src: 'Ì,Í,Ï,Î', dst: 'I' },
{ src: 'ò,ó,ö,ô,õ', dst: 'o' },
{ src: 'Ò,Ó,Ö,Ô,Õ', dst: 'O' },
{ src: 'ù,ú,ü,û', dst: 'u' },
{ src: 'Ù,Ú,Ü,Û', dst: 'U' },
{ src: 'ç', dst: 'c' },
{ src: 'Ç', dst: 'C' },
{ src: 'æ', dst: 'ae' }
]
}, options);
/* Split synonym src into arrays. */
if (settings.useSynonyms) {
for (var i = 0, l = settings.synonyms.length; i < l; i++) {
settings.synonyms[i].src = settings.synonyms[i].src.split(',');
}
}
/* Setup each instaFilta instance. */
this.each(function() {
var typeTimer,
$targets,
$sections,
$scopeElement,
lastTerm = '';
/* Check if this instance should be bound to a scope. */
if (settings.scope) {
$scopeElement = $(this).closest(settings.scope);
$targets = $scopeElement.find(settings.targets);
$sections = $scopeElement.find(settings.sections);
}
else {
$targets = $(settings.targets);
$sections = $(settings.sections);
}
/* Prepare value(s) to match against. */
$targets.each(function() {
$target = $(this), original = $target.text();
$target.data('values', (function(s) {
var values = [original], normalized = original;
if (!settings.useSynonyms) { return values; }
for (var i = 0, l = s.length; i < l; i++) {
for (var j = 0; j < s[i].src.length; j++) {
normalized = normalized.replace(s[i].src[j], s[i].dst);
}
}
!!(normalized === original) || values.push(normalized);
return values;
}(settings.synonyms)));
});
/* Will hide any sections that don't have data-instafilta-hidden="false" */
var toggleSections = function() {
$sections.each(function() {
var $section = $(this);
if ($section.find('[data-instafilta-hide="false"]').length) {
$section[settings.sectionsShowEffect](settings.sectionsShowDuration);
}
else {
$section[settings.sectionsHideEffect](settings.sectionsHideDuration);
}
});
};
/* Apply the results of the filtering process GUI-wise. */
var toggleResults = function(forceShowAll) {
var $matchedItems = (function() {
return forceShowAll ? $targets.attr('data-instafilta-hide', 'false') : $targets;
}()).filter('[data-instafilta-hide="false"]')[settings.itemsShowEffect](settings.itemsShowDuration);
forceShowAll || $targets.filter('[data-instafilta-hide="true"]')[settings.itemsHideEffect](settings.itemsHideDuration);
settings.hideEmptySections && toggleSections();
/* Check if a callback function has been provided. */
if (typeof settings.onFilterComplete === 'function') {
settings.onFilterComplete.apply(this, [$matchedItems]);
}
return $matchedItems;
};
/* Reset the filter completely. */
var showAll = function() {
return toggleResults(true);
};
/* Main filtering function. */
_filterTerm = function(term) {
term = settings.caseSensitive ? term : term.toLowerCase();
if (lastTerm === term) { return false; }
else { lastTerm = term; }
term || showAll();
/* Iterate through associated targets and find matches. */
$targets.each(function() {
var $item = $(this);
if (!$item.data('originalText')) {
$item.data('originalHtml', $item.html());
$item.data('originalText', $item.data('values')[0]);
}
/* Returns the index at which a match was found. Returns -1 if not found. */
var matchedIndex = (function(values, t) {
var index = -1;
for (var i = 0; i < values.length; i++) {
index = (settings.caseSensitive ? values[i] : values[i].toLowerCase()).indexOf(t);
if (index >= 0) { break; } // We have a match, no need to iterate further.
}
return index;
}($item.data('values'), term));
var originalText = $item.data('originalText'),
targetText = settings.caseSensitive ? originalText : originalText.toLowerCase(),
matchedText = null,
newText = null;
/* If we should mark the match, wrap it in a span tag. */
if (matchedIndex >= 0 && settings.markMatches) {
matchedText = originalText.substring(matchedIndex, matchedIndex + term.length);
newText = originalText.replace(matchedText, '<span class="' + settings.matchCssClass + '">' + matchedText + '</span>');
$item.html($item.data('originalHtml').replace(originalText, newText));
}
$item.attr('data-instafilta-hide', (settings.beginsWith && matchedIndex !== 0) || matchedIndex < 0 ? 'true' : 'false');
});
return toggleResults();
};
/* Filter items depending on category data attribute. Categories can be a comma separated
* string or an array of strings. Shows all targets if categories is falsy. */
_filterCategory = function(categories, requireAll) {
if (!categories || !categories.length) {
return showAll();
}
if (typeof categories === 'string') {
categories = categories.split(',');
}
$targets.each(function() {
var hideStatus = true,
matched = 0,
$item = $(this),
targetCats = $item.data(settings.categoryDataAttr);
if (targetCats) {
targetCats = targetCats.split(',');
for (var i = 0; i < targetCats.length; i++) {
for (var j = 0; j < categories.length; j++) {
if (targetCats[i] === categories[j]) {
if (requireAll) { matched++; }
else { hideStatus = false; break; }
}
}
}
if (requireAll && matched === categories.length) { hideStatus = false; }
$item.html($item.data('originalText')).attr('data-instafilta-hide', hideStatus);
}
});
return toggleResults();
};
/* Setup event handlers. */
$(this).on('keyup', function() {
var $field = $(this);
clearTimeout(typeTimer);
typeTimer = setTimeout(function() {
_filterTerm($field.val());
}, settings.typeDelay);
});
});
return {
filterTerm: _filterTerm,
filterCategory: _filterCategory
};
};
}(jQuery));