-
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.
- Loading branch information
Showing
3 changed files
with
79 additions
and
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,16 @@ | ||
import { FrameRenderer } from "./frame_renderer" | ||
import { MorphPageRenderer } from "../drive/morph_page_renderer" | ||
import { MorphElements } from "../morph_elements" | ||
import { dispatch } from "../../util" | ||
|
||
export class MorphFrameRenderer extends FrameRenderer { | ||
static renderElement(currentElement, newElement) { | ||
MorphPageRenderer.renderElement(currentElement, newElement.children) | ||
static renderElement(currentElement, newParentElement) { | ||
const newElement = newParentElement.children | ||
|
||
// console.log(`dispatching turbo:before-frame-morph on `, currentElement.id) | ||
// console.log(`and new = `, newElement.id) | ||
// console.log(`and children = `, newElement.children) | ||
|
||
// const morphStyle = 'innerHTML' | ||
|
||
// dispatch("turbo:before-frame-morph", { | ||
// target: currentElement, | ||
// detail: { currentElement, newElement } | ||
// }) | ||
|
||
// this.isMorphingTurboFrame = this.#isFrameReloadedWithMorph(currentElement) | ||
|
||
// Idiomorph.morph(currentElement, newElement, { | ||
// ignoreActiveValue: true, | ||
// morphStyle: morphStyle, | ||
// callbacks: { | ||
// beforeNodeAdded: this.#shouldAddElement, | ||
// beforeNodeMorphed: this.#shouldMorphElement, | ||
// beforeAttributeUpdated: this.#shouldUpdateAttribute, | ||
// beforeNodeRemoved: this.#shouldRemoveElement, | ||
// afterNodeMorphed: this.#didMorphElement | ||
// } | ||
// }) | ||
} | ||
|
||
static #isFrameReloadedWithMorph(element) { | ||
return element.src && element.refresh === "morph" | ||
} | ||
|
||
static #shouldAddElement = (node) => { | ||
return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id)) | ||
} | ||
|
||
static #shouldMorphElement = (oldNode, newNode) => { | ||
if (!(oldNode instanceof HTMLElement)) return | ||
|
||
if (oldNode.hasAttribute("data-turbo-permanent")) return false | ||
|
||
if (!this.isMorphingTurboFrame && this.#isFrameReloadedWithMorph(oldNode)) return false | ||
|
||
const event = dispatch("turbo:before-morph-element", { | ||
cancelable: true, | ||
target: oldNode, | ||
detail: { | ||
newElement: newNode | ||
} | ||
dispatch("turbo:before-frame-morph", { | ||
target: currentElement, | ||
detail: { currentElement, newElement } | ||
}) | ||
|
||
return !event.defaultPrevented | ||
} | ||
|
||
static #shouldUpdateAttribute = (attributeName, target, mutationType) => { | ||
const event = dispatch("turbo:before-morph-attribute", { cancelable: true, target, detail: { attributeName, mutationType } }) | ||
|
||
return !event.defaultPrevented | ||
} | ||
|
||
static #shouldRemoveElement = (node) => { | ||
return this.#shouldMorphElement(node) | ||
} | ||
|
||
static #didMorphElement = (oldNode, newNode) => { | ||
if (newNode instanceof HTMLElement) { | ||
dispatch("turbo:morph-element", { | ||
target: oldNode, | ||
detail: { | ||
newElement: newNode | ||
} | ||
}) | ||
} | ||
MorphElements.morph(currentElement, newElement, 'innerHTML') | ||
} | ||
} |
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,66 @@ | ||
import { Idiomorph } from "idiomorph/dist/idiomorph.esm.js" | ||
import { dispatch } from "../util" | ||
|
||
export class MorphElements { | ||
static morph(currentElement, newElement, morphStyle = "outerHTML") { | ||
this.isMorphingTurboFrame = this.isFrameReloadedWithMorph(currentElement) | ||
|
||
Idiomorph.morph(currentElement, newElement, { | ||
morphStyle: morphStyle, | ||
callbacks: { | ||
beforeNodeAdded: this.shouldAddElement, | ||
beforeNodeMorphed: this.shouldMorphElement, | ||
beforeAttributeUpdated: this.shouldUpdateAttribute, | ||
beforeNodeRemoved: this.shouldRemoveElement, | ||
afterNodeMorphed: this.didMorphElement | ||
} | ||
}) | ||
} | ||
|
||
static isFrameReloadedWithMorph(element) { | ||
return element.src && element.refresh === "morph" | ||
} | ||
|
||
static shouldAddElement = (node) => { | ||
return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id)) | ||
} | ||
|
||
static shouldMorphElement = (oldNode, newNode) => { | ||
if (!(oldNode instanceof HTMLElement)) return | ||
|
||
if (oldNode.hasAttribute("data-turbo-permanent")) return false | ||
|
||
if (!this.isMorphingTurboFrame && this.isFrameReloadedWithMorph(oldNode)) return false | ||
|
||
const event = dispatch("turbo:before-morph-element", { | ||
cancelable: true, | ||
target: oldNode, | ||
detail: { | ||
newElement: newNode | ||
} | ||
}) | ||
|
||
return !event.defaultPrevented | ||
} | ||
|
||
static shouldUpdateAttribute = (attributeName, target, mutationType) => { | ||
const event = dispatch("turbo:before-morph-attribute", { cancelable: true, target, detail: { attributeName, mutationType } }) | ||
|
||
return !event.defaultPrevented | ||
} | ||
|
||
static shouldRemoveElement = (node) => { | ||
return this.shouldMorphElement(node) | ||
} | ||
|
||
static didMorphElement = (oldNode, newNode) => { | ||
if (newNode instanceof HTMLElement) { | ||
dispatch("turbo:morph-element", { | ||
target: oldNode, | ||
detail: { | ||
newElement: newNode | ||
} | ||
}) | ||
} | ||
} | ||
} |