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.
Replace LinkInterceptor with LinkClickObserver
Follow-up to hotwired#382 --- In the same style as [hotwired#382][], replace instances of `LinkInterceptor` with `LinkClickObserver`, making the necessary interface changes in classes that used to extend `LinkInterceptorDelegate`. Conditional logic that was once covered in the `LinkInterceptor` is moved into the predicate methods of the delegates (for example, the `FrameController.willFollowLinkToLocation` and `FrameRedirector.willFollowLinkToLocation`). Since the recently introduced [FormLinkInterceptor][] was named after the `LinkInterceptor`, this commit also renames that class (and its call sites) to match the `LinkClickObserver`-suffix, along with the `LinkInterceptorDelegate` method structure. [hotwired#382]: hotwired#382 [FormLinkInterceptor]: hotwired@f8a94c5
- Loading branch information
1 parent
5a2029a
commit 719137f
Showing
9 changed files
with
109 additions
and
162 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 was deleted.
Oops, something went wrong.
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,56 @@ | ||
import { LinkClickObserver, LinkClickObserverDelegate } from "./link_click_observer" | ||
|
||
export type FormLinkClickObserverDelegate = { | ||
willSubmitFormLinkClickToLocation(link: Element, location: URL, event: MouseEvent): boolean | ||
submittedFormLinkToLocation(link: Element, location: URL, form: HTMLFormElement): void | ||
} | ||
|
||
export class FormLinkClickObserver implements LinkClickObserverDelegate { | ||
readonly linkClickObserver: LinkClickObserver | ||
readonly delegate: FormLinkClickObserverDelegate | ||
|
||
constructor(delegate: FormLinkClickObserverDelegate, eventTarget: EventTarget) { | ||
this.delegate = delegate | ||
this.linkClickObserver = new LinkClickObserver(this, eventTarget) | ||
} | ||
|
||
start() { | ||
this.linkClickObserver.start() | ||
} | ||
|
||
stop() { | ||
this.linkClickObserver.stop() | ||
} | ||
|
||
willFollowLinkToLocation(link: Element, action: URL, originalEvent: MouseEvent): boolean { | ||
return ( | ||
this.delegate.willSubmitFormLinkClickToLocation(link, action, originalEvent) && | ||
(link.hasAttribute("data-turbo-method") || link.hasAttribute("data-turbo-stream")) | ||
) | ||
} | ||
|
||
followedLinkToLocation(link: Element, action: URL): void { | ||
const form = document.createElement("form") | ||
form.setAttribute("data-turbo", "true") | ||
form.setAttribute("action", action.href) | ||
form.setAttribute("hidden", "") | ||
|
||
const method = link.getAttribute("data-turbo-method") | ||
if (method) form.setAttribute("method", method) | ||
|
||
const turboFrame = link.getAttribute("data-turbo-frame") | ||
if (turboFrame) form.setAttribute("data-turbo-frame", turboFrame) | ||
|
||
const turboConfirm = link.getAttribute("data-turbo-confirm") | ||
if (turboConfirm) form.setAttribute("data-turbo-confirm", turboConfirm) | ||
|
||
const turboStream = link.hasAttribute("data-turbo-stream") | ||
if (turboStream) form.setAttribute("data-turbo-stream", "") | ||
|
||
this.delegate.submittedFormLinkToLocation(link, action, form) | ||
|
||
document.body.appendChild(form) | ||
form.requestSubmit() | ||
form.remove() | ||
} | ||
} |
Oops, something went wrong.