File tree Expand file tree Collapse file tree 4 files changed +31
-1
lines changed Expand file tree Collapse file tree 4 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -107,6 +107,7 @@ Custom action option | Description
107
107
-------------------- | -----------
108
108
` :stop ` | calls ` .stopPropagation() ` on the event before invoking the method
109
109
` :prevent ` | calls ` .preventDefault() ` on the event before invoking the method
110
+ ` :self ` | only invokes the method if the event was fired by the element itself
110
111
111
112
## Event Objects
112
113
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ export class Binding {
31
31
}
32
32
33
33
handleEvent ( event : Event ) {
34
- if ( this . willBeInvokedByEvent ( event ) ) {
34
+ if ( this . willBeInvokedByEvent ( event ) && this . shouldBeInvokedPerSelf ( event ) ) {
35
35
this . processStopPropagation ( event ) ;
36
36
this . processPreventDefault ( event ) ;
37
37
@@ -77,6 +77,14 @@ export class Binding {
77
77
}
78
78
}
79
79
80
+ private shouldBeInvokedPerSelf ( event : Event ) : boolean {
81
+ if ( this . action . eventOptions . self === true ) {
82
+ return this . action . element === event . target
83
+ } else {
84
+ return true
85
+ }
86
+ }
87
+
80
88
private willBeInvokedByEvent ( event : Event ) : boolean {
81
89
const eventTarget = event . target
82
90
if ( this . element === eventTarget ) {
Original file line number Diff line number Diff line change 1
1
export interface EventModifiers extends AddEventListenerOptions {
2
2
stop ?: boolean ;
3
3
prevent ?: boolean ;
4
+ self ?: boolean ;
4
5
}
Original file line number Diff line number Diff line change @@ -190,6 +190,26 @@ export default class EventOptionsTests extends LogControllerTestCase {
190
190
)
191
191
}
192
192
193
+ async "test self option" ( ) {
194
+ this . setAction ( this . buttonElement , "click->c#log:self" )
195
+ await this . nextFrame
196
+
197
+ await this . triggerEvent ( this . buttonElement , "click" )
198
+
199
+ this . assertActions (
200
+ { name : "log" , eventType : "click" }
201
+ )
202
+ }
203
+
204
+ async "test self option on parent" ( ) {
205
+ this . setAction ( this . element , "click->c#log:self" )
206
+ await this . nextFrame
207
+
208
+ await this . triggerEvent ( this . buttonElement , "click" )
209
+
210
+ this . assertNoActions ( )
211
+ }
212
+
193
213
setAction ( element : Element , value : string ) {
194
214
element . setAttribute ( "data-action" , value )
195
215
}
You can’t perform that action at this time.
0 commit comments