Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new Action Option :self #546

Merged
merged 9 commits into from Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/actions.md
Expand Up @@ -107,6 +107,7 @@ Custom action option | Description
-------------------- | -----------
`:stop` | calls `.stopPropagation()` on the event before invoking the method
`:prevent` | calls `.preventDefault()` on the event before invoking the method
`:self` | only invokes the method if the event was fired by the element itself

## Event Objects

Expand Down
10 changes: 9 additions & 1 deletion src/core/binding.ts
Expand Up @@ -31,7 +31,7 @@ export class Binding {
}

handleEvent(event: Event) {
if (this.willBeInvokedByEvent(event)) {
if (this.willBeInvokedByEvent(event) && this.shouldBeInvokedPerSelf(event)) {
this.processStopPropagation(event);
this.processPreventDefault(event);

Expand Down Expand Up @@ -77,6 +77,14 @@ export class Binding {
}
}

private shouldBeInvokedPerSelf(event: Event): boolean {
if (this.action.eventOptions.self === true) {
return this.action.element === event.target
} else {
return true
}
}

private willBeInvokedByEvent(event: Event): boolean {
const eventTarget = event.target
if (this.element === eventTarget) {
Expand Down
1 change: 1 addition & 0 deletions src/core/event_modifiers.ts
@@ -1,4 +1,5 @@
export interface EventModifiers extends AddEventListenerOptions {
stop?: boolean;
prevent?: boolean;
self?: boolean;
}
20 changes: 20 additions & 0 deletions src/tests/modules/core/event_options_tests.ts
Expand Up @@ -190,6 +190,26 @@ export default class EventOptionsTests extends LogControllerTestCase {
)
}

async "test self option"() {
this.setAction(this.buttonElement, "click->c#log:self")
await this.nextFrame

await this.triggerEvent(this.buttonElement, "click")

this.assertActions(
{ name: "log", eventType: "click" }
)
}

async "test self option on parent"() {
this.setAction(this.element, "click->c#log:self")
await this.nextFrame

await this.triggerEvent(this.buttonElement, "click")

this.assertNoActions()
}

setAction(element: Element, value: string) {
element.setAttribute("data-action", value)
}
Expand Down