Cancelled
Push — master ( 28a5cf...9e6262 )
by Paul
06:07
created

+/scripts/admin/forms.js   A

Complexity

Total Complexity 21
Complexity/F 2.63

Size

Lines of Code 71
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 2
dl 0
loc 71
rs 10
c 1
b 0
f 0
wmc 21
mnd 2
bc 15
fnc 8
bpm 1.875
cpm 2.625
noi 10

6 Functions

Rating   Name   Duplication   Size   Complexity  
A GLSR.forms.onChange 0 16 1
A GLSR.forms.toggleHiddenField 0 5 3
A GLSR.forms.normalizeValues 0 3 1
A GLSR.forms.init 0 7 3
A GLSR.forms.normalizeValue 0 9 3
A GLSR.forms.isSelected 0 9 3
1
GLSR.forms = function( selector ) {
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ 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...
2
	this.el = document.querySelector( selector );
3
	if( !this.el )return;
1 ignored issue
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
4
	this.depends = this.el.querySelectorAll( '[data-depends]' );
5
	if( !this.depends.length )return;
1 ignored issue
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
6
	this.init();
7
};
8
9
GLSR.forms.prototype = {
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ 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...
10
11
	/** @return void */
12
	init: function() {
13
		var formControls = this.el.elements;
14
		for( var i = 0; i < formControls.length; i++ ) {
15
			if( ['INPUT', 'SELECT'].indexOf( formControls[i].nodeName ) === -1 )continue;
1 ignored issue
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
16
			formControls[i].addEventListener( 'change', this.onChange.bind( this ));
17
		}
18
	},
19
20
	/** @return bool */
21
	isSelected: function( el, dependency ) {
22
		if( 'checkbox' === el.type ) {
23
			return !!el.checked;
24
		}
25
		else if( Array.isArray( dependency.value )) {
26
			return this.normalizeValues( dependency.value ).indexOf( this.normalizeValue( el.value )) !== -1;
27
		}
28
		return this.normalizeValue( dependency.value ) === this.normalizeValue( el.value );
29
	},
30
31
	/** @return bool|string */
32
	normalizeValue: function( value ) {
33
		if(['true','on','yes','1'].indexOf( value ) !== -1 ) {
34
			return true;
35
		}
36
		if(['false','off','no','0'].indexOf( value ) !== -1 ) {
37
			return false;
38
		}
39
		return value;
40
	},
41
42
	/** @return array */
43
	normalizeValues: function( values ) {
44
		return values.map( this.normalizeValue );
45
	},
46
47
	/** @return void */
48
	onChange: function( ev ) {
49
		this.depends.forEach( function( el ) {
50
			var data = el.getAttribute( 'data-depends' );
51
			if( !data )return;
1 ignored issue
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
52
			var dependency;
53
			try {
54
				dependency = JSON.parse( data );
55
			}
56
			catch( error ) {
57
				console.log( data );
1 ignored issue
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
58
				return console.error( error );
59
			}
60
			if( dependency.name !== ev.target.name.replace( '[]', '' ))return;
1 ignored issue
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
61
			this.toggleHiddenField( el, this.isSelected( ev.target, dependency ));
1 ignored issue
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
62
		}.bind( this ));
63
	},
64
65
	/** @return void */
66
	toggleHiddenField: function( el, bool ) {
67
		var row = el.closest( '.glsr-field' );
68
		if( !row )return;
1 ignored issue
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
69
		row.classList[bool ? 'remove' : 'add']( 'hidden' );
70
	},
71
};
72