Skip to content

Commit

Permalink
refactor out duplication, made a bit awkward by the use of static met…
Browse files Browse the repository at this point in the history
…hods.
  • Loading branch information
botandrose-machine committed Sep 15, 2024
1 parent 6b319e2 commit db817a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
16 changes: 6 additions & 10 deletions src/core/drive/morphing_page_renderer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FrameElement } from "../../elements/frame_element"
import { PageRenderer } from "./page_renderer"
import { dispatch } from "../../util"
import { morphElements } from "../morphing"
Expand All @@ -7,12 +6,16 @@ export class MorphingPageRenderer extends PageRenderer {
static renderElement(currentElement, newElement) {
morphElements(currentElement, newElement, {
callbacks: {
beforeNodeMorphed: element => !canRefreshFrame(element)
beforeNodeMorphed: element => {
return !MorphingPageRenderer.shouldRefreshChildFrameWithMorphing(null, element)
}
}
})

for (const frame of currentElement.querySelectorAll("turbo-frame")) {
if (canRefreshFrame(frame)) frame.reload()
if (MorphingPageRenderer.shouldRefreshChildFrameWithMorphing(null, frame)) {
frame.reload()
}
}

dispatch("turbo:morph", { detail: { currentElement, newElement } })
Expand All @@ -31,10 +34,3 @@ export class MorphingPageRenderer extends PageRenderer {
}
}

function canRefreshFrame(frame) {
return frame instanceof FrameElement &&
frame.src &&
frame.refresh === "morph" &&
!frame.closest("[data-turbo-permanent]") &&
!frame.parentElement.closest("turbo-frame[src][refresh=morph]")
}
16 changes: 6 additions & 10 deletions src/core/frames/morphing_frame_renderer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FrameElement } from "../../elements/frame_element"
import { FrameRenderer } from "./frame_renderer"
import { morphChildren } from "../morphing"
import { dispatch } from "../../util"
Expand All @@ -12,11 +11,15 @@ export class MorphingFrameRenderer extends FrameRenderer {

morphChildren(currentElement, newElement, {
callbacks: {
beforeNodeMorphed: element => !canRefreshFrame(element, currentElement)
beforeNodeMorphed: element => {
return !MorphingFrameRenderer.shouldRefreshChildFrameWithMorphing(currentElement, element)
}
}
})
for (const frame of currentElement.querySelectorAll("turbo-frame")) {
if (canRefreshFrame(frame, currentElement)) frame.reload()
if (MorphingFrameRenderer.shouldRefreshChildFrameWithMorphing(currentElement, frame)) {
frame.reload()
}
}
}

Expand All @@ -25,10 +28,3 @@ export class MorphingFrameRenderer extends FrameRenderer {
}
}

function canRefreshFrame(frame, currentFrame) {
return frame instanceof FrameElement &&
frame.src &&
frame.refresh === "morph" &&
!frame.closest("[data-turbo-permanent]") &&
frame.parentElement.closest("turbo-frame[src][refresh=morph]").id === currentFrame.id
}
8 changes: 8 additions & 0 deletions src/core/renderer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FrameElement } from "../elements/frame_element"
import { Bardo } from "./bardo"

export class Renderer {
Expand All @@ -7,6 +8,13 @@ export class Renderer {
// Abstract method
}

static shouldRefreshChildFrameWithMorphing(parentFrame, frame) {
return frame instanceof FrameElement &&
frame.shouldReloadWithMorph &&
!frame.closest("[data-turbo-permanent]") &&
frame.parentElement.closest("turbo-frame[src][refresh=morph]") === parentFrame
}

constructor(currentSnapshot, newSnapshot, isPreview, willRender = true) {
this.currentSnapshot = currentSnapshot
this.newSnapshot = newSnapshot
Expand Down

0 comments on commit db817a3

Please sign in to comment.