Total Complexity | 21 |
Complexity/F | 2.63 |
Lines of Code | 71 |
Function Count | 8 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | GLSR.forms = function( selector ) { |
||
|
|||
2 | this.el = document.querySelector( selector ); |
||
3 | if( !this.el )return; |
||
1 ignored issue
–
show
|
|||
4 | this.depends = this.el.querySelectorAll( '[data-depends]' ); |
||
5 | if( !this.depends.length )return; |
||
1 ignored issue
–
show
|
|||
6 | this.init(); |
||
7 | }; |
||
8 | |||
9 | GLSR.forms.prototype = { |
||
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
|
|||
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
|
|||
52 | var dependency; |
||
53 | try { |
||
54 | dependency = JSON.parse( data ); |
||
55 | } |
||
56 | catch( error ) { |
||
57 | console.log( data ); |
||
1 ignored issue
–
show
|
|||
58 | return console.error( error ); |
||
59 | } |
||
60 | if( dependency.name !== ev.target.name.replace( '[]', '' ))return; |
||
1 ignored issue
–
show
|
|||
61 | this.toggleHiddenField( el, this.isSelected( ev.target, dependency )); |
||
1 ignored issue
–
show
|
|||
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
|
|||
69 | row.classList[bool ? 'remove' : 'add']( 'hidden' ); |
||
70 | }, |
||
71 | }; |
||
72 |
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.