1
|
1 |
|
(function() { |
2
|
|
|
/** |
3
|
|
|
* Create VanillaFilter instance |
4
|
|
|
*/ |
5
|
1 |
|
this.VanillaFilter = function() { |
6
|
|
|
/** |
7
|
|
|
* Default options for filtering |
8
|
|
|
* @type {Object} |
9
|
|
|
*/ |
10
|
7 |
|
this.options = { |
11
|
|
|
debug: false, |
12
|
|
|
vanillaTrigger: 'vanillatrigger', |
13
|
|
|
vanillaTarget: 'vanillatarget', |
14
|
|
|
vanillaDisplayType: 'block', |
15
|
|
|
vanillaFallbackSelector: '.vanilla-no-results', |
16
|
|
|
vanillaCallbackFunction: function() {} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Override defaults by settings from user |
21
|
|
|
* @param {Object} |
22
|
|
|
*/ |
23
|
7 |
|
if (arguments[0] && typeof arguments[0] === "object") { |
24
|
1 |
|
this.options = _extendDefaults(this.options, arguments[0]); |
25
|
|
|
} |
26
|
|
|
|
27
|
7 |
|
if(this.options.debug === true) { |
28
|
|
|
console.log('vanillafilter: Found options: [', this.options ,']'); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get all the filterTrigger elements to trigger filtering |
33
|
|
|
* @type {Elements Object} | {Element} |
34
|
|
|
*/ |
35
|
7 |
|
this.vanillaFallback = document.querySelectorAll(this.options.vanillaFallbackSelector); |
36
|
7 |
|
this.filterTrigger = document.querySelectorAll('[data-' + this.options.vanillaTrigger + ']'); |
37
|
7 |
|
this.filterValues = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check if we have any filterTrigger instances |
41
|
|
|
* @param {Elements Object} | {Element} |
42
|
|
|
*/ |
43
|
7 |
|
if (this.filterTrigger) { |
44
|
7 |
|
var _ = this; |
45
|
|
|
|
46
|
7 |
|
Object.keys(_.filterTrigger).map(function(index) { |
47
|
|
|
return _.bind(_.filterTrigger[index], _getTriggerHandler(_.filterTrigger[index])); |
48
|
|
|
}); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* If we have a fallback element, hide it on init |
53
|
|
|
* @param {Element} element |
54
|
|
|
*/ |
55
|
7 |
|
if(this.vanillaFallback) { |
56
|
7 |
|
if(this.options.debug === true) { |
57
|
|
|
console.log('vanillafilter: Found fallback element(s): [', this.vanillaFallback ,'], hiding it on initialisation'); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
7 |
|
this.fallback(false); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Bind the filter logic to a filterTrigger element |
66
|
|
|
* @param {Element} element |
67
|
|
|
* @param {String} handler |
68
|
|
|
*/ |
69
|
1 |
|
VanillaFilter.prototype.bind = function(element, handler) { |
|
|
|
|
70
|
2 |
|
if(this.options.debug === true) { |
71
|
|
|
console.log('vanillafilter: Bound filterTrigger to our vanillafilter: [', element ,'] with handler: [', handler ,']'); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return element.addEventListener(handler, this.triggerFilter.bind(this), true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Filter logic to show/hide elements based on their value |
79
|
|
|
*/ |
80
|
1 |
|
VanillaFilter.prototype.triggerFilter = function(event) { |
|
|
|
|
81
|
|
|
event.preventDefault(); |
82
|
|
|
|
83
|
|
|
var _ = this, |
84
|
|
|
activeFilter = event.target; |
85
|
|
|
|
86
|
2 |
|
if(_.options.debug === true) { |
87
|
|
|
console.log('vanillafilter: triggerFilter called for: [', activeFilter ,']. Added to activeFilters'); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the correct active filters |
92
|
|
|
*/ |
93
|
|
|
_.filterValues = _getFilterValues(activeFilter, _.options.vanillaTrigger, _.filterValues); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get all the targets to filter on |
97
|
|
|
*/ |
98
|
|
|
var allTargets = Object.keys(document.querySelectorAll('[data-' + _.options.vanillaTarget + ']')).map(function(index) { |
99
|
|
|
return document.querySelectorAll('[data-' + _.options.vanillaTarget + ']')[index]; |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Filter the correct results and show/hide them |
104
|
|
|
* @param {Element} item |
105
|
|
|
*/ |
106
|
|
|
var results = [], |
107
|
|
|
hide = []; |
108
|
|
|
|
109
|
|
|
allTargets.filter(function(targetElement, index) { |
110
|
|
|
var shouldShowTargetItem = false; |
111
|
|
|
|
112
|
2 |
|
if(_.filterValues.length === 0) { |
113
|
4 |
|
if(_.options.debug === true && index === 0) { |
114
|
|
|
console.log('vanillafilter: No filter values found, triggering display: [', _.options.vanillaDisplayType ,'] for all targets'); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
targetElement.style.display = _.options.vanillaDisplayType; |
118
|
|
|
} else { |
119
|
|
|
_.filterValues.filter(function(activeFilter) { |
120
|
|
|
var targetValues = _getTargetValues(targetElement, _.options.vanillaTarget); |
121
|
|
|
|
122
|
2 |
|
if(!shouldShowTargetItem) { |
123
|
|
|
shouldShowTargetItem = targetValues.includes(activeFilter); |
124
|
|
|
} |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check if we should show the item or not, if true, push it to our results array |
129
|
|
|
* @param {Boolean} should we show the target item? |
130
|
|
|
*/ |
131
|
2 |
|
if(shouldShowTargetItem) { |
132
|
2 |
|
if(_.options.debug === true) { |
133
|
|
|
console.log('vanillafilter: Set display: [', _.options.vanillaDisplayType ,'] for targetElement: [', targetElement ,']. Pushing to results: [', results ,']'); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
targetElement.style.display = _.options.vanillaDisplayType; |
137
|
|
|
results.push(targetElement); |
138
|
|
|
|
139
|
|
|
_vanillaCallback(_.options.vanillaCallbackFunction, targetElement, _.options.debug); |
140
|
|
|
} else { |
141
|
|
|
targetElement.style.display = 'none'; |
142
|
|
|
hide.push(targetElement); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
}); |
146
|
|
|
|
147
|
2 |
|
if(_.options.debug === true) { |
148
|
|
|
console.log('vanillafilter: Hiding target elements: [', hide ,']'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Trigger the fallback function on every filter change |
153
|
|
|
* and show a fallback element if there are 0 results |
154
|
|
|
*/ |
155
|
2 |
|
if(this.vanillaFallback.length) { |
156
|
2 |
|
this.fallback(_.filterValues.length && !results.length); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Show a fallback element when we have active filters, but no results are returned. |
162
|
|
|
*/ |
163
|
1 |
|
VanillaFilter.prototype.fallback = function(show) { |
|
|
|
|
164
|
7 |
|
var _ = this, |
165
|
|
|
display = (show ? 'block' : 'none'); |
166
|
|
|
|
167
|
7 |
|
Object.keys(_.vanillaFallback).map(function(index) { |
168
|
|
|
_.vanillaFallback[index].style.display = display; |
169
|
|
|
}); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Extend default options by user input options |
174
|
|
|
* @param {Object} options |
175
|
|
|
* @param {Object} overrides |
176
|
|
|
*/ |
177
|
1 |
|
_extendDefaults = function(options, overrides) { |
|
|
|
|
178
|
1 |
|
for (var option in overrides) { |
179
|
2 |
|
if (overrides.hasOwnProperty(option)) { |
180
|
1 |
|
options[option] = overrides[option]; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
return options; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the correct handler for the clicked filter trigger |
189
|
|
|
* @param {Element} element |
190
|
|
|
*/ |
191
|
1 |
|
_getTriggerHandler = function(element) { |
|
|
|
|
192
|
2 |
|
var handler = ['SELECT', 'INPUT'].includes(element.tagName) ? 'change' : 'click'; |
193
|
|
|
|
194
|
|
|
return handler; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the currenct active filter values from filter triggers to filter on |
199
|
|
|
* @param {Element} filterElem |
200
|
|
|
* @param {String} filterTriggerValue |
201
|
|
|
*/ |
202
|
1 |
|
_getFilterValues = function(filterElem, filterTriggerValue, currentFilters) { |
|
|
|
|
203
|
|
|
var value, |
204
|
|
|
newFilters = currentFilters; |
205
|
|
|
|
206
|
2 |
|
if(['SELECT', 'INPUT'].includes(filterElem.tagName)) { |
207
|
|
|
value = filterElem.value; |
208
|
|
|
} else { |
209
|
|
|
value = filterElem.dataset[filterTriggerValue]; |
210
|
|
|
} |
211
|
|
|
|
212
|
2 |
|
if(value === "") { |
213
|
|
|
newFilters = []; |
214
|
|
|
} else { |
215
|
2 |
|
if(['SELECT'].includes(filterElem.tagName)) { |
216
|
|
|
newFilters = []; |
217
|
|
|
newFilters.push(value); |
218
|
|
|
} else { |
219
|
2 |
|
if(currentFilters.includes(value)) { |
220
|
|
|
newFilters.splice(newFilters.indexOf(value), 1); |
221
|
|
|
} else { |
222
|
|
|
newFilters.push(value); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return newFilters; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get the filter values from a target element that should be filtered |
232
|
|
|
* @param {Element} targetElement |
233
|
|
|
* @param {String} targetFilterValue |
234
|
|
|
*/ |
235
|
1 |
|
_getTargetValues = function(targetElement, targetFilterValue) { |
|
|
|
|
236
|
|
|
var targetValues = targetElement.dataset[targetFilterValue], |
237
|
|
|
trimmedTargetValues = targetValues.replace(/\s/g, ''), |
238
|
|
|
separateTrimmedTargetValues = trimmedTargetValues.split(','); |
239
|
|
|
|
240
|
|
|
return separateTrimmedTargetValues.filter(Boolean); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* If a callback function is set, call it for this targetElement |
245
|
|
|
* @param {Function} callback function to trigger |
|
|
|
|
246
|
|
|
*/ |
247
|
1 |
|
_vanillaCallback = function(callbackFunction, targetElement, debug) { |
|
|
|
|
248
|
2 |
|
if(typeof callbackFunction === 'function') { |
|
|
|
|
249
|
2 |
|
if(debug === true) { |
250
|
|
|
console.log('vanillafilter: vanillaCallbackFunction called for element: [', targetElement ,']'); |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return callbackFunction(targetElement); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
})(); |
258
|
|
|
|