-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter.js
85 lines (64 loc) · 2.43 KB
/
filter.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
/**
* Filter.js is a jQuery plugin that enables simple element filtering based on terms.
* The functionality is similar to isotope, but without layouts and very bare bones.
*
* @name filter.js
* @version 1.0.0
* @requires jQuery v1.9.2+
* @author Tom Maitland
* @license Apache v2 License
*
* For docs, visit:
* https://github.com/tommaitland/filter.js
*
* Copyright (c) 2013, Tom Maitland
*/
(function ($) {
"use strict";
$.fn.filter = function (options) {
var defaults = {
nav: '[data-filter]',
showAny: false
}
var $this = this,
settings = $.extend(defaults, options),
$target = $(settings.target),
selected = [];
return this.each( function() {
var $element = $(this);
$(settings.nav).each( function() {
$(this).click( function(event) {
// add selected class
$(this).toggleClass('selected');
// manipulate selected terms array
if ($.inArray($(this).data('filter'), selected) < 0) {
selected.push($(this).data('filter'));
} else {
var index = $.inArray($(this).data('filter'), selected);
selected.splice(index, 1);
}
// show/hide elements
$element.find('[data-filter-tags]').each( function() {
var terms = $(this).data('filter-tags').split(','),
show = null;
if (settings.showAny) {
for (var i=0;terms.length;i++) {
show ($.inArray(terms[i], selected)) >= 0 && show !== false);
}
} else {
for (var i=0;i<selected.length;i++) {
show = ($.inArray(selected[i], terms) >= 0 && show !== false);
}
}
if (show || selected.length == 0) {
$(this).fadeIn();
} else {
$(this).fadeOut();
}
});
event.preventDefault();
});
});
});
};
}(jQuery));