GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f74f0c...b40549 )
by Dennis
02:26
created

VanillaFilter.triggerFilter   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8.1239

Importance

Changes 4
Bugs 2 Features 2
Metric Value
cc 4
c 4
b 2
f 2
nc 5
nop 1
dl 0
loc 25
ccs 4
cts 11
cp 0.3636
crap 8.1239
rs 8.5806
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 5
		this.options = {
11
			vanillaTrigger: 'vanillatrigger',
12
			vanillaTarget: 'vanillatarget',
13
			vanillaDisplayType: 'block',
14
			vanillaFallbackSelector: '.vanilla-no-results'
15
		}
16
17
		/**
18
		 * Override defaults by settings from user
19
		 * @param  {Object}
20
		 */
21 5
		if (arguments[0] && typeof arguments[0] === "object") {
22 1
			this.options = extendDefaults(this.options, arguments[0]);
23
		}
24
25
		/**
26
		 * Get all the filterTrigger elements to trigger filtering
27
		 * @type {Elements Object} | {Element}
28
		 */
29 5
		this.vanillaFallback = document.querySelectorAll(this.options.vanillaFallbackSelector);
30 5
		this.filterTrigger = document.querySelectorAll('[data-' + this.options.vanillaTrigger + ']');
31 5
		this.filterValues = [];
32
33
		/**
34
		 * Check if we have any filterTrigger instances
35
		 * @param  {Elements Object} | {Element}
36
		 */
37 5
		if (this.filterTrigger) {
38 5
			var _ = this;
39
40 5
			Object.keys(_.filterTrigger).map(function(index) {
41
				return _.bind(_.filterTrigger[index], getTriggerHandler(_.filterTrigger[index]));
42
			});
43
		}
44
45
		/**
46
		 * If we have a fallback element, hide it on init
47
		 * @param  {Element} element
48
		 */
49 5
		if(this.vanillaFallback) {
50 5
			this.fallback(false);
51
		}
52
	}
53
54
	/**
55
	 * Bind the filter logic to a filterTrigger element
56
	 * @param  {Element} element
57
	 * @param  {String} handler
58
	 */
59 1
	VanillaFilter.prototype.bind = function(element, handler) {
0 ignored issues
show
Bug introduced by
The variable VanillaFilter seems to be never declared. If this is a global, consider adding a /** global: VanillaFilter */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
60
		return element.addEventListener(handler, this.triggerFilter.bind(this), true);
61
	}
62
63
	/**
64
	 * Filter logic to show/hide elements based on their value
65
	 */
66 1
	VanillaFilter.prototype.triggerFilter = function(event) {
0 ignored issues
show
Bug introduced by
The variable VanillaFilter seems to be never declared. If this is a global, consider adding a /** global: VanillaFilter */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
67
		event.preventDefault();
68
69
		var _ = this,
70
			activeFilter = event.target;
71
72
		/**
73
		 * Set the correct active filters
74
		 */
75
		_.filterValues = getFilterValues(activeFilter, _.options.vanillaTrigger, _.filterValues);
76
77
		/**
78
		 * Get all the targets to filter on
79
		 */
80
		var allTargets = Object.keys(document.querySelectorAll('[data-' + _.options.vanillaTarget + ']')).map(function(index) {
81
			return document.querySelectorAll('[data-' + _.options.vanillaTarget + ']')[index];
82
		});
83
84
		/**
85
		 * Filter the correct results and show/hide them
86
		 * @param  {Element} item
87
		 */
88
		var results = [];
89
90
		allTargets.filter(function(item) {
91
			var intersect = false;
92
93 2
			if(_.filterValues.length === 0) {
94
				item.style.display = _.options.vanillaDisplayType;
95
			} else {
96
				// console.log('filtervalues', _.filterValues);
97
98
				_.filterValues.filter(function(activeFilter) {
99
					var targetValues = getTargetValues(item, _.options.vanillaTarget);
100
101 2
					if(!intersect) {
102
						intersect = targetValues.includes(activeFilter);
103
					}
104
				});
105
106
				// console.log(item.dataset['vanillatarget'], intersect);
107
108 2
				item.style.display = intersect ? _.options.vanillaDisplayType : 'none';
109
110 2
				if(intersect) {
111
					results.push(item);
112
				}
113
			}
114
		});
115
116
		/**
117
		 * Trigger the fallback function on every filter change
118
		 */
119 2
		if(this.vanillaFallback.length) {
120 2
			this.fallback(_.filterValues.length && !results.length);
121
		}
122
	}
123
124
	/**
125
	 * Show a fallback element when we have active filters, but no results are returned.
126
	 */
127 1
	VanillaFilter.prototype.fallback = function(show) {
0 ignored issues
show
Bug introduced by
The variable VanillaFilter seems to be never declared. If this is a global, consider adding a /** global: VanillaFilter */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
128 5
		var _ = this,
129
			display = (show ? 'block' : 'none');
130
131 5
		Object.keys(_.vanillaFallback).map(function(index) {
132
			_.vanillaFallback[index].style.display = display;
133
		});
134
	}
135
136
	/**
137
	 * Extend default options by user input options
138
	 * @param  {Object} options
139
	 * @param  {Object} overrides
140
	 */
141 1
	function extendDefaults(options, overrides) {
142 1
		for (var option in overrides) {
143 2
			if (overrides.hasOwnProperty(option)) {
144 1
				options[option] = overrides[option];
145
			}
146
		}
147
148 1
		return options;
149
	}
150
151
	/**
152
	 * Get the correct handler for the clicked filter trigger
153
	 * @param  {Element} element
154
	 */
155 1
	function getTriggerHandler(element) {
156 2
		var handler = ['SELECT', 'INPUT'].includes(element.tagName) ? 'change' : 'click';
157
158
		return handler;
159
	}
160
161
	/**
162
	 * Get the currenct active filter values from filter triggers to filter on
163
	 * @param  {Element} filterElem
164
	 * @param  {String} filterTriggerValue
165
	 */
166 1
	function getFilterValues(filterElem, filterTriggerValue, currentFilters) {
167
		var value,
168
			newFilters = currentFilters;
169
170 2
		if(['SELECT', 'INPUT'].includes(filterElem.tagName)) {
171
			value = filterElem.value;
172
		} else {
173
			value = filterElem.dataset[filterTriggerValue];
174
		}
175
176 2
		if(value === "") {
177
			newFilters = [];
178
		} else {
179 2
			if(['SELECT'].includes(filterElem.tagName)) {
180
				newFilters = [];
181
				newFilters.push(value);
182
			} else {
183 2
				if(currentFilters.includes(value)) {
184
					newFilters.splice(newFilters.indexOf(value), 1);
185
				} else {
186
					newFilters.push(value);
187
				}
188
			}
189
		}
190
191
		return newFilters;
192
	}
193
194
	/**
195
	 * Get the filter values from a target element that should be filtered
196
	 * @param  {Element} targetElem
197
	 * @param  {String} targetFilterValue
198
	 */
199 1
	function getTargetValues(targetElem, targetFilterValue) {
200
		var targetValues = targetElem.dataset[targetFilterValue],
201
			trimmedTargetValues = targetValues.replace(/\s/g, ''),
202
			separateTrimmedTargetValues = trimmedTargetValues.split(',');
203
204
		return separateTrimmedTargetValues.filter(Boolean);
205
	}
206
207
})();
208