1
- import $ from "jquery " ;
2
- import Base from "../../core/base " ;
3
- import utils from "../../core/utils " ;
1
+ import { BasePattern } from "@patternslib/patternslib/src/core/basepattern " ;
2
+ import events from "../../core/events " ;
3
+ import dom from "../../core/dom " ;
4
4
import logging from "../../core/logging" ;
5
- import Parser from "../../core/parser" ;
5
+ import Parser from "@patternslib/patternslib/src/core/parser" ;
6
+ import registry from "@patternslib/patternslib/src/core/registry" ;
7
+ import utils from "../../core/utils" ;
6
8
7
9
const log = logging . getLogger ( "depends" ) ;
8
10
@@ -13,127 +15,101 @@ parser.addArgument("transition", "none", ["none", "css", "fade", "slide"]);
13
15
parser . addArgument ( "effect-duration" , "fast" ) ;
14
16
parser . addArgument ( "effect-easing" , "swing" ) ;
15
17
16
- export default Base . extend ( {
17
- name : "depends" ,
18
- trigger : ".pat-depends" ,
19
- jquery_plugin : true ,
18
+ class Pattern extends BasePattern {
19
+ static name = "depends" ;
20
+ static trigger = ".pat-depends" ;
21
+ static parser = parser ;
20
22
21
- async init ( $el , opts ) {
23
+ async init ( ) {
24
+ this . $el = $ ( this . el ) ;
22
25
const DependsHandler = ( await import ( "../../lib/dependshandler" ) ) . default ; // prettier-ignore
23
26
24
- const dependent = this . $el [ 0 ] ;
25
- const options = parser . parse ( this . $el , opts ) ;
26
- this . $modal = this . $el . parents ( ".pat-modal" ) ;
27
-
28
- let handler ;
29
27
try {
30
- handler = new DependsHandler ( this . $ el, options . condition ) ;
28
+ this . handler = new DependsHandler ( this . el , this . options . condition ) ;
31
29
} catch ( e ) {
32
- log . error ( "Invalid condition: " + e . message , dependent ) ;
30
+ log . error ( "Invalid condition: " + e . message , this . el ) ;
33
31
return ;
34
32
}
35
33
36
- let state = handler . evaluate ( ) ;
37
- switch ( options . action ) {
38
- case "show" :
39
- utils . hideOrShow ( $el , state , options , this . name ) ;
40
- this . updateModal ( ) ;
41
- break ;
42
- case "enable" :
43
- if ( state ) this . enable ( ) ;
44
- else this . disable ( ) ;
45
- break ;
46
- case "both" :
47
- if ( state ) {
48
- utils . hideOrShow ( $el , state , options , this . name ) ;
49
- this . updateModal ( ) ;
50
- this . enable ( ) ;
51
- } else {
52
- utils . hideOrShow ( $el , state , options , this . name ) ;
53
- this . updateModal ( ) ;
54
- this . disable ( ) ;
55
- }
56
- break ;
57
- }
34
+ // Initialize
35
+ this . set_state ( ) ;
58
36
59
- const data = {
60
- handler : handler ,
61
- options : options ,
62
- dependent : dependent ,
63
- } ;
37
+ for ( const input of this . handler . getAllInputs ( ) ) {
38
+ events . add_event_listener (
39
+ input ,
40
+ "change" ,
41
+ `pat-depends--change--${ this . uuid } ` , // We need to support multiple events per dependant ...
42
+ this . set_state . bind ( this )
43
+ ) ;
44
+ events . add_event_listener (
45
+ input ,
46
+ "keyup" ,
47
+ `pat-depends--keyup--${ this . uuid } ` , // ... therefore we need to add a uuid to the event id ...
48
+ this . set_state . bind ( this )
49
+ ) ;
64
50
65
- for ( let input of handler . getAllInputs ( ) ) {
66
51
if ( input . form ) {
67
- let $form = $ ( input . form ) ;
68
- let dependents = $form . data ( "patDepends.dependents" ) ;
69
- if ( ! dependents ) {
70
- dependents = [ data ] ;
71
- $form . on ( "reset.pat-depends" , ( ) => this . onReset ) ;
72
- } else if ( dependents . indexOf ( data ) === - 1 ) dependents . push ( data ) ;
73
- $form . data ( "patDepends.dependents" , dependents ) ;
52
+ events . add_event_listener (
53
+ input . form ,
54
+ "reset" ,
55
+ `pat-depends--reset--${ this . uuid } ` , // ... to not override previously set event handlers.
56
+ async ( ) => {
57
+ // TODO: note sure, what this timeout is for.
58
+ await utils . timeout ( 50 ) ;
59
+ this . set_state . bind ( this ) ;
60
+ }
61
+ ) ;
74
62
}
75
- $ ( input ) . on ( "change.pat-depends" , null , data , this . onChange . bind ( this ) ) ;
76
- $ ( input ) . on ( "keyup.pat-depends" , null , data , this . onChange . bind ( this ) ) ;
77
63
}
78
- } ,
64
+ }
79
65
80
- async onReset ( event ) {
81
- const dependents = $ ( event . target ) . data ( "patDepends.dependents" ) ;
82
- await utils . timeout ( 50 ) ;
83
- for ( let dependent of dependents ) {
84
- event . data = dependent ;
85
- this . onChange ( event ) ;
86
- }
87
- } ,
66
+ update_modal ( ) {
67
+ const modal = this . el . closest ( ".pat-modal" ) ;
88
68
89
- updateModal ( ) {
90
69
// If we're in a modal, make sure that it gets resized.
91
- if ( this . $ modal. length ) {
70
+ if ( this . modal ) {
92
71
$ ( document ) . trigger ( "pat-update" , { pattern : "depends" } ) ;
93
72
}
94
- } ,
73
+ }
95
74
96
75
enable ( ) {
97
- if ( this . $el . is ( ":input" ) ) {
98
- this . $el [ 0 ] . disabled = null ;
99
- } else if ( this . $ el. is ( "a" ) ) {
100
- this . $el . off ( " click.patternDepends ") ;
76
+ if ( dom . is_input ( this . el ) ) {
77
+ this . el . disabled = false ;
78
+ } else if ( this . el . tagName === "A" ) {
79
+ events . remove_event_listener ( this . el , "pat-depends-- click") ;
101
80
}
102
- this . $ el. removeClass ( "disabled" ) ;
81
+ this . el . classList . remove ( "disabled" ) ;
103
82
this . $el . trigger ( "pat-update" , {
104
83
pattern : "depends" ,
105
84
action : "attribute-changed" ,
106
- dom : this . $el [ 0 ] ,
85
+ dom : this . el ,
107
86
enabled : true ,
108
87
} ) ;
109
- } ,
88
+ }
110
89
111
90
disable ( ) {
112
- if ( this . $el . is ( ":input" ) ) {
113
- this . $el [ 0 ] . disabled = "disabled" ;
114
- } else if ( this . $el . is ( "a" ) ) {
115
- this . $el . on ( "click.patternDepends" , ( e ) => e . preventDefault ( ) ) ;
91
+ if ( dom . is_input ( this . el ) ) {
92
+ this . el . disabled = true ;
93
+ } else if ( this . el . tagName === "A" ) {
94
+ events . add_event_listener ( this . el , "click" , "pat-depends--click" , ( e ) =>
95
+ e . preventDefault ( )
96
+ ) ;
116
97
}
117
- this . $ el. addClass ( "disabled" ) ;
98
+ this . el . classList . add ( "disabled" ) ;
118
99
this . $el . trigger ( "pat-update" , {
119
100
pattern : "depends" ,
120
101
action : "attribute-changed" ,
121
- dom : this . $el [ 0 ] ,
102
+ dom : this . el ,
122
103
enabled : false ,
123
104
} ) ;
124
- } ,
125
-
126
- onChange ( event ) {
127
- const handler = event . data . handler ;
128
- const options = event . data . options ;
129
- const dependent = event . data . dependent ;
130
- const $depdendent = $ ( dependent ) ;
131
- const state = handler . evaluate ( ) ;
105
+ }
132
106
133
- switch ( options . action ) {
107
+ set_state ( ) {
108
+ const state = this . handler . evaluate ( ) ;
109
+ switch ( this . options . action ) {
134
110
case "show" :
135
- utils . hideOrShow ( $depdendent , state , options , this . name ) ;
136
- this . updateModal ( ) ;
111
+ utils . hideOrShow ( this . el , state , this . options , this . name ) ;
112
+ this . update_modal ( ) ;
137
113
break ;
138
114
case "enable" :
139
115
if ( state ) {
@@ -143,14 +119,20 @@ export default Base.extend({
143
119
}
144
120
break ;
145
121
case "both" :
146
- utils . hideOrShow ( $depdendent , state , options , this . name ) ;
147
- this . updateModal ( ) ;
122
+ utils . hideOrShow ( this . el , state , this . options , this . name ) ;
123
+ this . update_modal ( ) ;
148
124
if ( state ) {
149
125
this . enable ( ) ;
150
126
} else {
151
127
this . disable ( ) ;
152
128
}
153
129
break ;
154
130
}
155
- } ,
156
- } ) ;
131
+ }
132
+ }
133
+
134
+ // Register Pattern class in the global pattern registry
135
+ registry . register ( Pattern ) ;
136
+
137
+ // Make it available
138
+ export default Pattern ;
0 commit comments