-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract morph action and add children-only option to stream-element
- Loading branch information
Showing
3 changed files
with
73 additions
and
74 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Idiomorph } from "idiomorph/dist/idiomorph.esm" | ||
import { dispatch } from "../../../util" | ||
|
||
export default function morph(streamElement) { | ||
const morphStyle = streamElement.hasAttribute("children-only") ? "innerHTML" : "outerHTML" | ||
streamElement.targetElements.forEach((element) => { | ||
try { | ||
Idiomorph.morph(element, streamElement.templateContent, { | ||
morphStyle: morphStyle, | ||
ignoreActiveValue: true, | ||
callbacks: { | ||
beforeNodeAdded, | ||
beforeNodeMorphed, | ||
beforeAttributeUpdated, | ||
beforeNodeRemoved, | ||
afterNodeMorphed, | ||
}, | ||
}) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
}) | ||
} | ||
|
||
function beforeNodeAdded(node) { | ||
return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id)) | ||
} | ||
|
||
function beforeNodeRemoved(node) { | ||
return beforeNodeAdded(node) | ||
} | ||
|
||
function beforeNodeMorphed(target, newElement) { | ||
if (target instanceof HTMLElement && !target.hasAttribute("data-turbo-permanent")) { | ||
const event = dispatch("turbo:before-morph-element", { | ||
cancelable: true, | ||
detail: { | ||
target, | ||
newElement, | ||
}, | ||
}) | ||
return !event.defaultPrevented | ||
} | ||
return false | ||
} | ||
|
||
function beforeAttributeUpdated(attributeName, target, mutationType) { | ||
const event = dispatch("turbo:before-morph-attribute", { | ||
cancelable: true, | ||
target, | ||
detail: { | ||
attributeName, | ||
mutationType, | ||
}, | ||
}) | ||
return !event.defaultPrevented | ||
} | ||
|
||
function afterNodeMorphed(target, newElement) { | ||
if (newElement instanceof HTMLElement) { | ||
dispatch("turbo:morph-element", { | ||
target, | ||
detail: { | ||
newElement, | ||
}, | ||
}) | ||
} | ||
} |
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