1
- import $ from "jquery" ;
2
1
import parser from "./depends_parse" ;
3
2
4
- function DependsHandler ( el , expression ) {
5
- const $el = $ ( el ) ;
6
- var $context = $el . closest ( "form" ) ;
7
- if ( ! $context . length ) $context = $ ( document ) ;
8
- this . $el = $el ;
9
- this . $context = $context ;
10
- this . ast = parser . parse ( expression ) ; // TODO: handle parse exceptions here
11
- }
3
+ class DependsHandler {
4
+ constructor ( el , expression ) {
5
+ this . el = el ;
6
+ this . context = el . closest ( "form" ) || document ;
7
+ this . ast = parser . parse ( expression ) ; // TODO: handle parse exceptions here
8
+ }
9
+
10
+ _findInput ( name ) {
11
+ const input = this . context . querySelector ( `
12
+ input[name=${ name } ],
13
+ select[name=${ name } ],
14
+ textarea[name=${ name } ],
15
+ button[name=${ name } ]
16
+ ` ) ;
17
+ return input || document . querySelector ( `#${ name } ` ) || null ;
18
+ }
12
19
13
- DependsHandler . prototype = {
14
- _findInputs : function ( name ) {
15
- var $input = this . $context . find ( ":input[name='" + name + "']" ) ;
16
- if ( ! $input . length ) $input = $ ( "#" + name ) ;
17
- return $input ;
18
- } ,
20
+ _getValue ( name ) {
21
+ const input = this . _findInput ( name ) ;
22
+ if ( ! input ) {
23
+ return null ;
24
+ }
19
25
20
- _getValue : function ( name ) {
21
- var $input = this . _findInputs ( name ) ;
22
- if ( ! $input . length ) return null ;
26
+ if (
27
+ ( input . type === "radio" || input . type === "checkbox" ) &&
28
+ input . checked === false
29
+ ) {
30
+ return null ;
31
+ }
23
32
24
- if ( $input . attr ( "type" ) === "radio" || $input . attr ( "type" ) === "checkbox" )
25
- return $input . filter ( ":checked" ) . val ( ) || null ;
26
- else return $input . val ( ) ;
27
- } ,
33
+ return input . value ;
34
+ }
28
35
29
- getAllInputs : function ( ) {
30
- var todo = [ this . ast ] ,
31
- $inputs = $ ( ) ,
32
- node ;
36
+ getAllInputs ( ) {
37
+ const todo = [ this . ast ] ;
38
+ const inputs = new Set ( ) ;
33
39
34
40
while ( todo . length ) {
35
- node = todo . shift ( ) ;
36
- if ( node . input ) $inputs = $inputs . add ( this . _findInputs ( node . input ) ) ;
37
- if ( node . children && node . children . length )
41
+ const node = todo . shift ( ) ;
42
+ if ( node . input ) {
43
+ const input = this . _findInput ( node . input ) ;
44
+ if ( input ) {
45
+ inputs . add ( input ) ;
46
+ }
47
+ }
48
+ if ( node . children && node . children . length ) {
38
49
todo . push . apply ( todo , node . children ) ;
50
+ }
39
51
}
40
- return $ inputs;
41
- } ,
52
+ return [ ... inputs ] ;
53
+ }
42
54
43
- _evaluate : function ( node ) {
44
- var value = node . input ? this . _getValue ( node . input ) : null ,
45
- i ;
55
+ _evaluate ( node ) {
56
+ const value = node . input ? this . _getValue ( node . input ) : null ;
46
57
47
58
switch ( node . type ) {
48
59
case "NOT" :
49
60
return ! this . _evaluate ( node . children [ 0 ] ) ;
50
- case "AND" :
51
- for ( i = 0 ; i < node . children . length ; i ++ )
52
- if ( ! this . _evaluate ( node . children [ i ] ) ) return false ;
53
- return true ;
54
- case "OR" :
55
- for ( i = 0 ; i < node . children . length ; i ++ )
56
- if ( this . _evaluate ( node . children [ i ] ) ) return true ;
57
- return false ;
61
+ case "AND" : {
62
+ // As soon as one child evaluates to false, the AND expression is false.
63
+ const is_false = node . children . some ( ( child ) => ! this . _evaluate ( child ) ) ;
64
+ return ! is_false ;
65
+ }
66
+ case "OR" : {
67
+ // As soon as one child evaluates to true, the OR expression is true.
68
+ const is_true = node . children . some ( ( child ) => this . _evaluate ( child ) ) ;
69
+ return is_true ;
70
+ }
58
71
case "comparison" :
59
72
switch ( node . operator ) {
60
73
case "=" :
@@ -70,21 +83,25 @@ DependsHandler.prototype = {
70
83
case ">=" :
71
84
return value >= node . value ;
72
85
case "~=" :
73
- if ( value === null ) return false ;
86
+ if ( value === null ) {
87
+ return false ;
88
+ }
74
89
return value . indexOf ( node . value ) != - 1 ;
75
90
case "=~" :
76
- if ( value === null || ! node . value ) return false ;
91
+ if ( value === null || ! node . value ) {
92
+ return false ;
93
+ }
77
94
return node . value . indexOf ( value ) != - 1 ;
78
95
}
79
96
break ;
80
97
case "truthy" :
81
98
return ! ! value ;
82
99
}
83
- } ,
100
+ }
84
101
85
- evaluate : function ( ) {
102
+ evaluate ( ) {
86
103
return this . _evaluate ( this . ast ) ;
87
- } ,
88
- } ;
104
+ }
105
+ }
89
106
90
107
export default DependsHandler ;
0 commit comments