-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In an effort to match the patterns established by `LinkInterceptor` and `FormInterceptor`, this commit introduces a `FormLinkInterceptor` and `FormLinkInterceptorDelegate`. Behind the scenes, the `FormLinkInterceptor` relies on an instance of the `LinkInterceptor` to intervene in `<a>` element clicks when `[data-turbo-method]` or `[data-turbo-stream]` are present. When those clicks are detected, it creates a `<form hidden>` element, attaches it to the document, delegates to a `FormLinkInterceptorDelegate` to map the `<a>` element's attributes to the `<form>` element, submits the form through the polyfilled [HTMLFormElement.requestSubmit][] method, then removes the `<form>` from the document. The `Session` serves as a `FormLinkInterceptorDelegate`, making sure to start and stop the observer _before_ its `LinkInterceptor` instance, so that clicks that are intercepted by the `FormLinkInterceptor` are not also intercepted by the `LinkInterceptor`. [HTMLFormElement.requestSubmit]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/requestSubmit
- Loading branch information
1 parent
6eb2cde
commit e4c1a36
Showing
3 changed files
with
86 additions
and
54 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,52 @@ | ||
import { LinkInterceptor, LinkInterceptorDelegate } from "../core/frames/link_interceptor" | ||
|
||
export type FormLinkInterceptorDelegate = { | ||
shouldInterceptFormLinkClick(link: Element): boolean | ||
formLinkClickIntercepted(link: Element, form: HTMLFormElement): void | ||
} | ||
|
||
export class FormLinkInterceptor implements LinkInterceptorDelegate { | ||
readonly linkInterceptor: LinkInterceptor | ||
readonly delegate: FormLinkInterceptorDelegate | ||
|
||
constructor(delegate: FormLinkInterceptorDelegate, element: HTMLElement) { | ||
this.delegate = delegate | ||
this.linkInterceptor = new LinkInterceptor(this, element) | ||
} | ||
|
||
start() { | ||
this.linkInterceptor.start() | ||
} | ||
|
||
stop() { | ||
this.linkInterceptor.stop() | ||
} | ||
|
||
shouldInterceptLinkClick(link: Element): boolean { | ||
return ( | ||
this.delegate.shouldInterceptFormLinkClick(link) && | ||
(link.hasAttribute("data-turbo-method") || link.hasAttribute("data-turbo-stream")) | ||
) | ||
} | ||
|
||
linkClickIntercepted(link: Element, action: string): void { | ||
const form = document.createElement("form") | ||
form.setAttribute("action", action) | ||
form.hidden = true | ||
|
||
const method = link.getAttribute("data-turbo-method") | ||
if (method) form.setAttribute("method", method) | ||
|
||
const turboConfirm = link.getAttribute("data-turbo-confirm") | ||
if (turboConfirm) form.setAttribute("data-turbo-confirm", turboConfirm) | ||
|
||
const turboStream = link.getAttribute("data-turbo-stream") | ||
if (turboStream) form.setAttribute("data-turbo-stream", turboStream) | ||
|
||
this.delegate.formLinkClickIntercepted(link, form) | ||
|
||
document.body.appendChild(form) | ||
form.requestSubmit() | ||
form.remove() | ||
} | ||
} |