forked from hotwired/turbo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Replace LinkInterceptor with LinkClickObserver"
This reverts commit 9bef653.
- Loading branch information
1 parent
5871173
commit c212ae7
Showing
6 changed files
with
92 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { TurboClickEvent, TurboBeforeVisitEvent } from "../session" | ||
|
||
export interface LinkInterceptorDelegate { | ||
shouldInterceptLinkClick(element: Element, url: string, originalEvent: MouseEvent): boolean | ||
linkClickIntercepted(element: Element, url: string, originalEvent: MouseEvent): void | ||
} | ||
|
||
export class LinkInterceptor { | ||
readonly delegate: LinkInterceptorDelegate | ||
readonly element: Element | ||
private clickEvent?: Event | ||
|
||
constructor(delegate: LinkInterceptorDelegate, element: Element) { | ||
this.delegate = delegate | ||
this.element = element | ||
} | ||
|
||
start() { | ||
this.element.addEventListener("click", this.clickBubbled) | ||
document.addEventListener("turbo:click", this.linkClicked) | ||
document.addEventListener("turbo:before-visit", this.willVisit) | ||
} | ||
|
||
stop() { | ||
this.element.removeEventListener("click", this.clickBubbled) | ||
document.removeEventListener("turbo:click", this.linkClicked) | ||
document.removeEventListener("turbo:before-visit", this.willVisit) | ||
} | ||
|
||
clickBubbled = (event: Event) => { | ||
if (this.respondsToEventTarget(event.target)) { | ||
this.clickEvent = event | ||
} else { | ||
delete this.clickEvent | ||
} | ||
} | ||
|
||
linkClicked = <EventListener>((event: TurboClickEvent) => { | ||
if (this.clickEvent && this.respondsToEventTarget(event.target) && event.target instanceof Element) { | ||
if (this.delegate.shouldInterceptLinkClick(event.target, event.detail.url, event.detail.originalEvent)) { | ||
this.clickEvent.preventDefault() | ||
event.preventDefault() | ||
this.delegate.linkClickIntercepted(event.target, event.detail.url, event.detail.originalEvent) | ||
} | ||
} | ||
delete this.clickEvent | ||
}) | ||
|
||
willVisit = <EventListener>((_event: TurboBeforeVisitEvent) => { | ||
delete this.clickEvent | ||
}) | ||
|
||
respondsToEventTarget(target: EventTarget | null) { | ||
const element = target instanceof Element ? target : target instanceof Node ? target.parentElement : null | ||
return element && element.closest("turbo-frame, html") == this.element | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters