@@ -16,6 +16,20 @@ parser.addArgument("transition", "none", ["none", "css", "fade", "slide"]);
16
16
parser . addArgument ( "effect-duration" , "fast" ) ;
17
17
parser . addArgument ( "effect-easing" , "swing" ) ;
18
18
19
+
20
+ // A custom input event which differs from the one in `core/events` in that it
21
+ // accepts a `detail` object to pass arbitrary information around.
22
+ // TODO: The events in `core/events` should be refactored to accept a `detail`
23
+ // object.
24
+ const input_event = ( detail = { } ) => {
25
+ return new CustomEvent ( "input" , {
26
+ bubbles : true ,
27
+ cancelable : false ,
28
+ detail : detail ,
29
+ } ) ;
30
+ } ;
31
+
32
+
19
33
class Pattern extends BasePattern {
20
34
static name = "depends" ;
21
35
static trigger = ".pat-depends" ;
@@ -44,7 +58,14 @@ class Pattern extends BasePattern {
44
58
input ,
45
59
"input" ,
46
60
`pat-depends--input--${ this . uuid } ` ,
47
- this . set_state . bind ( this )
61
+ ( e ) => {
62
+ if ( e ?. detail ?. pattern_uuid === this . uuid ) {
63
+ // Ignore input events invoked from this pattern
64
+ // instance to avoid infinite loops.
65
+ return ;
66
+ }
67
+ this . set_state ( ) ;
68
+ }
48
69
) ;
49
70
50
71
if ( input . form ) {
@@ -74,15 +95,40 @@ class Pattern extends BasePattern {
74
95
enable ( ) {
75
96
const inputs = dom . find_inputs ( this . el ) ;
76
97
for ( const input of inputs ) {
98
+ if ( input . disabled === false ) {
99
+ // Do not re-enable an already enabled input.
100
+ continue ;
101
+ }
102
+
103
+ // Now, enable the input element.
77
104
input . disabled = false ;
78
- // Trigger the input after disabling so that any other bound
105
+
106
+ if ( input === this . el ) {
107
+ // Do not re-trigger this pattern on it's own element to avoid
108
+ // infinite loops.
109
+ continue ;
110
+ }
111
+
112
+ if ( dom . is_button ( input ) ) {
113
+ // Do not trigger the input event on buttons as they do not
114
+ // support it.
115
+ continue ;
116
+ }
117
+
118
+ // Trigger the input after enabling so that any other bound
79
119
// actions can react on that.
80
- input . dispatchEvent ( events . input_event ( ) ) ;
120
+ input . dispatchEvent ( input_event ( { pattern_uuid : this . uuid } ) ) ;
81
121
}
122
+
123
+ // Restore the original click behavior for anchor elements.
82
124
if ( this . el . tagName === "A" ) {
83
125
events . remove_event_listener ( this . el , "pat-depends--click" ) ;
84
126
}
127
+
128
+ // Remove the disabled class from the element.
85
129
this . el . classList . remove ( "disabled" ) ;
130
+
131
+ // Trigger the pat-update event to notify other patterns about enabling.
86
132
this . $el . trigger ( "pat-update" , {
87
133
pattern : "depends" ,
88
134
action : "attribute-changed" ,
@@ -94,17 +140,42 @@ class Pattern extends BasePattern {
94
140
disable ( ) {
95
141
const inputs = dom . find_inputs ( this . el ) ;
96
142
for ( const input of inputs ) {
143
+ if ( input . disabled === true ) {
144
+ // Do not re-disable an already disabled input.
145
+ continue ;
146
+ }
147
+
148
+ // Now, disable the input element.
97
149
input . disabled = true ;
150
+
151
+ if ( input === this . el ) {
152
+ // Do not re-trigger this pattern on it's own element to avoid
153
+ // infinite loops.
154
+ continue ;
155
+ }
156
+
157
+ if ( dom . is_button ( input ) ) {
158
+ // Do not trigger the input event on buttons as they do not
159
+ // support it.
160
+ continue ;
161
+ }
162
+
98
163
// Trigger the input after disabling so that any other bound
99
164
// actions can react on that.
100
- input . dispatchEvent ( events . input_event ( ) ) ;
165
+ input . dispatchEvent ( input_event ( { pattern_uuid : this . uuid } ) ) ;
101
166
}
167
+
168
+ // Prevent the default click behavior for anchor elements.
102
169
if ( this . el . tagName === "A" ) {
103
170
events . add_event_listener ( this . el , "click" , "pat-depends--click" , ( e ) =>
104
171
e . preventDefault ( )
105
172
) ;
106
173
}
174
+
175
+ // Add the disabled class to the element.
107
176
this . el . classList . add ( "disabled" ) ;
177
+
178
+ // Trigger the pat-update event to notify other patterns about disabling.
108
179
this . $el . trigger ( "pat-update" , {
109
180
pattern : "depends" ,
110
181
action : "attribute-changed" ,
0 commit comments