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 ( b19dae...483b03 )
by Dennis
01:49
created

VanillaFilter.triggerFilter   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.3943

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
c 2
b 0
f 1
nc 2
nop 1
dl 0
loc 40
ccs 4
cts 15
cp 0.2667
crap 1.3943
rs 8.8571
1
;
2 1
(function() {
3
	/**
4
	 * Create VanillaFilter instance
5
	 */
6 1
	this.VanillaFilter = function() {
7
		/**
8
		 * Default options for filtering
9
		 * @type {Object}
10
		 */
11 1
		this.options = {
12
			vanillaTrigger: 'vanillatrigger',
13
			vanillaTarget: 'vanillatarget',
14
			vanillaDisplayType: 'block',
15
		}
16
17
		/**
18
		 * Override defaults by settings from user
19
		 * @param  {Object}
20
		 */
21 4
		if (arguments[0] && typeof arguments[0] === "object") {
22
			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 1
		this.filterTrigger = document.querySelectorAll('[data-' + this.options.vanillaTrigger + ']');
30 1
		this.filterValues = [];
31
32
		/**
33
		 * Check if we have any filterTrigger instances
34
		 * @param  {Elements Object} | {Element}
35
		 */
36 2
		if (this.filterTrigger) {
37 1
			var _ = this;
38
39 1
			Object.keys(_.filterTrigger).map(function(index) {
40
				return _.bind(_.filterTrigger[index], getTriggerHandler(_.filterTrigger[index]));
41
			});
42
		}
43
	}
44
45
	/**
46
	 * Bind the filter logic to a filterTrigger element
47
	 * @param  {Element} element
48
	 * @param  {String} handler
49
	 */
50 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...
51
		return element.addEventListener(handler, this.triggerFilter.bind(this), true);
52
	}
53
54
	/**
55
	 * Filter logic to show/hide elements based on their value
56
	 */
57 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...
58
		event.preventDefault();
59
60
		var _ = this,
61
			activeFilter = event.target;
62
63
		/**
64
		 * Set the correct active filters
65
		 */
66
		_.filterValues = getFilterValues(activeFilter, _.options.vanillaTrigger, _.filterValues);
67
68
		/**
69
		 * Get all the targets to filter on
70
		 */
71
		var allTargets = Object.keys(document.querySelectorAll('[data-' + _.options.vanillaTarget + ']')).map(function(index) {
72
			return document.querySelectorAll('[data-' + _.options.vanillaTarget + ']')[index];
73
		});
74
75
		/**
76
		 * Filter the correct results and show/hide them
77
		 * @param  {Element} item
78
		 */
79
		allTargets.filter(function(item) {
80 2
			if(_.filterValues.length === 0) {
81
				return item.style.display = _.options.vanillaDisplayType;
82
			} else {
83
				var intersect = false;
84
85
				_.filterValues.filter(function(activeFilter) {
86
					var targetValues = getTargetValues(item, _.options.vanillaTarget);
87
88 2
					if(!intersect) {
89
						intersect = targetValues.includes(activeFilter);
90
					}
91
				});
92
93 2
				return item.style.display = intersect ? _.options.vanillaDisplayType : 'none';
94
			}
95
		});
96
	}
97
98
	/**
99
	 * Extend default options by user input options
100
	 * @param  {Object} source
101
	 * @param  {Object} properties
102
	 */
103 1
	function extendDefaults(source, properties) {
104
		for (var property in properties) {
105 2
			if (properties.hasOwnProperty(property)) {
106
				source[property] = properties[property];
107
			}
108
		}
109
110
		return source;
111
	}
112
113
	/**
114
	 * Get the correct handler for the clicked filter trigger
115
	 * @param  {Element} element
116
	 */
117 1
	function getTriggerHandler(element) {
118 2
		return ['SELECT', 'INPUT'].includes(element.tagName) ? 'change' : 'click';
119
	}
120
121
	/**
122
	 * Get the currenct active filter values from filter triggers to filter on
123
	 * @param  {Element} filterElem
124
	 * @param  {String} filterTriggerValue
125
	 */
126 1
	function getFilterValues(filterElem, filterTriggerValue, currentFilters) {
127
		var value,
128
			newFilters = currentFilters;
129
130 2
		if(['SELECT', 'INPUT'].includes(filterElem.tagName)) {
131
			value = filterElem.value;
132
		} else {
133
			value = filterElem.dataset[filterTriggerValue];
134
		}
135
136 2
		if(value === "") {
137
			newFilters = [];
138
		} else {
139 2
			if(currentFilters.includes(value)) {
140
				newFilters.splice(newFilters.indexOf(value), 1);
141
			} else {
142
				newFilters.push(value);
143
			}
144
		}
145
146
		return newFilters;
147
	}
148
149
	/**
150
	 * Get the filter values from a target element that should be filtered
151
	 * @param  {Element} targetElem
152
	 * @param  {String} targetFilterValue
153
	 */
154 1
	function getTargetValues(targetElem, targetFilterValue) {
155
		var targetValues = targetElem.dataset[targetFilterValue],
156
			trimmedTargetValues = targetValues.replace(/\s/g, ''),
157
			separateTrimmedTargetValues = trimmedTargetValues.split(',');
158
159
		return separateTrimmedTargetValues.filter(Boolean);
160
	}
161
162
})();
163