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.
- Loading branch information
1 parent
e94307f
commit 660af78
Showing
6 changed files
with
101 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,18 @@ | ||
import Idiomorph from "idiomorph" | ||
import { dispatch, nextAnimationFrame } from "../../util" | ||
import { Renderer } from "../renderer" | ||
import { Morph } from "../morph" | ||
|
||
export class MorphRenderer extends Renderer { | ||
static renderElement(currentElement, newElement) { | ||
Morph.render(currentElement, newElement) | ||
} | ||
|
||
async render() { | ||
if (this.willRender) await this.#morphBody() | ||
if (this.willRender) { | ||
this.renderElement(this.currentElement, this.newElement) | ||
} | ||
} | ||
|
||
get renderMethod() { | ||
return "morph" | ||
} | ||
|
||
// Private | ||
|
||
async #morphBody() { | ||
this.#morphElements(this.currentElement, this.newElement) | ||
this.#reloadRemoteFrames() | ||
|
||
dispatch("turbo:morph", { | ||
detail: { | ||
currentElement: this.currentElement, | ||
newElement: this.newElement | ||
} | ||
}) | ||
} | ||
|
||
#morphElements(currentElement, newElement, morphStyle = "outerHTML") { | ||
Idiomorph.morph(currentElement, newElement, { | ||
morphStyle: morphStyle, | ||
callbacks: { | ||
beforeNodeMorphed: this.#shouldMorphElement, | ||
beforeNodeRemoved: this.#shouldRemoveElement, | ||
afterNodeMorphed: this.#reloadStimulusControllers | ||
} | ||
}) | ||
} | ||
|
||
#reloadRemoteFrames() { | ||
this.#remoteFrames().forEach((frame) => { | ||
if (this.#isFrameReloadedWithMorph(frame)) { | ||
this.#renderFrameWithMorph(frame) | ||
} | ||
frame.reload() | ||
}) | ||
} | ||
|
||
#renderFrameWithMorph(frame) { | ||
frame.addEventListener("turbo:before-frame-render", (event) => { | ||
event.detail.render = this.#morphFrameUpdate | ||
}, { once: true }) | ||
} | ||
|
||
#morphFrameUpdate = (currentElement, newElement) => { | ||
dispatch("turbo:before-frame-morph", { | ||
target: currentElement, | ||
detail: { currentElement, newElement } | ||
}) | ||
this.#morphElements(currentElement, newElement, "innerHTML") | ||
} | ||
|
||
#shouldRemoveElement = (node) => { | ||
return this.#shouldMorphElement(node) | ||
} | ||
|
||
#shouldMorphElement = (node) => { | ||
if (node instanceof HTMLElement) { | ||
return !node.hasAttribute("data-turbo-permanent") | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
#reloadStimulusControllers = async (node) => { | ||
if (node instanceof HTMLElement && node.hasAttribute("data-controller")) { | ||
const originalAttribute = node.getAttribute("data-controller") | ||
node.removeAttribute("data-controller") | ||
await nextAnimationFrame() | ||
node.setAttribute("data-controller", originalAttribute) | ||
} | ||
} | ||
|
||
#isFrameReloadedWithMorph(element) { | ||
return element.src && element.refresh === "morph" | ||
} | ||
|
||
#remoteFrames() { | ||
return document.querySelectorAll("turbo-frame[src]") | ||
} | ||
} |
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,58 @@ | ||
import Idiomorph from "idiomorph" | ||
import { nextAnimationFrame } from "../util" | ||
|
||
export class Morph { | ||
static render(currentElement, newElement, morphStyle) { | ||
const morph = new this(currentElement, newElement) | ||
|
||
morph.render(morphStyle) | ||
} | ||
|
||
constructor(currentElement, newElement) { | ||
this.currentElement = currentElement | ||
this.newElement = newElement | ||
} | ||
|
||
render(morphStyle = "outerHTML") { | ||
Idiomorph.morph(this.currentElement, this.newElement, { | ||
morphStyle: morphStyle, | ||
callbacks: { | ||
beforeNodeMorphed: this.#shouldMorphElement, | ||
beforeNodeRemoved: this.#shouldRemoveElement, | ||
afterNodeMorphed: this.#reloadStimulusControllers | ||
} | ||
}) | ||
this.#reloadRemoteFrames() | ||
} | ||
|
||
#shouldRemoveElement = (node) => { | ||
return this.#shouldMorphElement(node) | ||
} | ||
|
||
#shouldMorphElement = (node) => { | ||
if (node instanceof HTMLElement) { | ||
return !node.hasAttribute("data-turbo-permanent") | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
#reloadStimulusControllers = async (node) => { | ||
if (node instanceof HTMLElement && node.hasAttribute("data-controller")) { | ||
const originalAttribute = node.getAttribute("data-controller") | ||
node.removeAttribute("data-controller") | ||
await nextAnimationFrame() | ||
node.setAttribute("data-controller", originalAttribute) | ||
} | ||
} | ||
|
||
#reloadRemoteFrames() { | ||
this.#remoteFrames().forEach((frame) => { | ||
frame.reload() | ||
}) | ||
} | ||
|
||
#remoteFrames() { | ||
return this.currentElement.querySelectorAll("turbo-frame[src]") | ||
} | ||
} |
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