From 96b06cf5e3303e8b9460cf5da99e232dc3a8a6a9 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Tue, 21 Sep 2021 10:56:16 -0400 Subject: [PATCH] Expand the FrameElementDelegate interface Expand the `FrameElementDelegate` to extend from both `FormInterceptorDelegate` and `LinkInterceptorDelegate`. After that change, replace conditionals in the `FrameRedirector` with calls to the `FrameElement` instance's delegate when one is found. This reduces the surface area for duplicate logic and improves guarantees of consistency across semantically similar situations. --- src/core/frames/frame_redirector.ts | 28 ++++++++++++++-------------- src/elements/frame_element.ts | 5 +++-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/core/frames/frame_redirector.ts b/src/core/frames/frame_redirector.ts index 96fb89bcc..4c83f5153 100644 --- a/src/core/frames/frame_redirector.ts +++ b/src/core/frames/frame_redirector.ts @@ -24,41 +24,41 @@ export class FrameRedirector implements LinkInterceptorDelegate, FormInterceptor } shouldInterceptLinkClick(element: Element, url: string) { - return this.shouldRedirect(element) + const frame = this.findFrameElement(element) + if (frame) { + return frame.delegate.shouldInterceptLinkClick(element, url) + } else { + return false + } } linkClickIntercepted(element: Element, url: string) { const frame = this.findFrameElement(element) if (frame) { - frame.setAttribute("reloadable", "") - frame.src = url + frame.delegate.linkClickIntercepted(element, url) } } shouldInterceptFormSubmission(element: HTMLFormElement, submitter?: HTMLElement) { - return this.shouldRedirect(element, submitter) + const frame = this.findFrameElement(element, submitter) + if (frame) { + return frame.delegate.shouldInterceptFormSubmission(element, submitter) + } else { + return false + } } formSubmissionIntercepted(element: HTMLFormElement, submitter?: HTMLElement) { const frame = this.findFrameElement(element, submitter) if (frame) { - frame.removeAttribute("reloadable") frame.delegate.formSubmissionIntercepted(element, submitter) } } - private shouldRedirect(element: Element, submitter?: HTMLElement) { - const frame = this.findFrameElement(element, submitter) - return frame ? frame != element.closest("turbo-frame") : false - } - private findFrameElement(element: Element, submitter?: HTMLElement) { const id = submitter?.getAttribute("data-turbo-frame") || element.getAttribute("data-turbo-frame") if (id && id != "_top") { - const frame = this.element.querySelector(`#${id}:not([disabled])`) - if (frame instanceof FrameElement) { - return frame - } + return this.element.querySelector(`turbo-frame#${id}:not([disabled])`) } } } diff --git a/src/elements/frame_element.ts b/src/elements/frame_element.ts index 59c8cc402..2f8d312a3 100644 --- a/src/elements/frame_element.ts +++ b/src/elements/frame_element.ts @@ -1,14 +1,15 @@ import { FetchResponse } from "../http/fetch_response" +import { FormInterceptorDelegate } from "../core/frames/form_interceptor" +import { LinkInterceptorDelegate } from "../core/frames/link_interceptor" export enum FrameLoadingStyle { eager = "eager", lazy = "lazy" } -export interface FrameElementDelegate { +export interface FrameElementDelegate extends LinkInterceptorDelegate, FormInterceptorDelegate { connect(): void disconnect(): void loadingStyleChanged(): void sourceURLChanged(): void disabledChanged(): void - formSubmissionIntercepted(element: HTMLFormElement, submitter?: HTMLElement): void loadResponse(response: FetchResponse): void isLoading: boolean }