|
| 1 | +import { Idiomorph } from "idiomorph/dist/idiomorph.esm.js" |
| 2 | +import { FrameElement } from "../elements/frame_element" |
| 3 | +import { dispatch } from "../util" |
| 4 | + |
| 5 | +export function morphRefresh(currentElement, newElement) { |
| 6 | + idiomorph(currentElement, newElement, { |
| 7 | + willMorphElement: element => !canRefreshFrame(element) |
| 8 | + }) |
| 9 | + |
| 10 | + for (const frame of document.querySelectorAll("turbo-frame")) { |
| 11 | + if (canRefreshFrame(frame)) refreshFrame(frame) |
| 12 | + } |
| 13 | + |
| 14 | + dispatch("turbo:morph", { detail: { currentElement, newElement } }) |
| 15 | +} |
| 16 | + |
| 17 | +export function morphFrames(currentElement, newElement) { |
| 18 | + dispatch("turbo:before-frame-morph", { |
| 19 | + target: currentElement, |
| 20 | + detail: { currentElement, newElement } |
| 21 | + }) |
| 22 | + |
| 23 | + morphChildren(currentElement, newElement) |
| 24 | +} |
| 25 | + |
| 26 | +export function morphChildren(currentElement, newElement) { |
| 27 | + idiomorph(currentElement, newElement.children, { |
| 28 | + morphStyle: "innerHTML" |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +export function morphElements(currentElement, newElement) { |
| 33 | + idiomorph(currentElement, newElement) |
| 34 | +} |
| 35 | + |
| 36 | +function idiomorph(currentElement, newElement, { willMorphElement, ...options } = {}) { |
| 37 | + const callbacks = new IdiomorphCallbacks(willMorphElement) |
| 38 | + |
| 39 | + Idiomorph.morph(currentElement, newElement, { ...options, callbacks }) |
| 40 | +} |
| 41 | + |
| 42 | +class IdiomorphCallbacks { |
| 43 | + constructor(willMorphElement) { |
| 44 | + this.willMorphElement = willMorphElement || ((element) => true) |
| 45 | + } |
| 46 | + |
| 47 | + beforeNodeAdded = (node) => { |
| 48 | + return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id)) |
| 49 | + } |
| 50 | + |
| 51 | + beforeNodeMorphed = (target, newElement) => { |
| 52 | + if (target instanceof HTMLElement) { |
| 53 | + if (!target.hasAttribute("data-turbo-permanent") && this.willMorphElement(target)) { |
| 54 | + const event = dispatch("turbo:before-morph-element", { cancelable: true, target, detail: { newElement } }) |
| 55 | + |
| 56 | + return !event.defaultPrevented |
| 57 | + } else { |
| 58 | + return false |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + beforeAttributeUpdated = (attributeName, target, mutationType) => { |
| 64 | + const event = dispatch("turbo:before-morph-attribute", { cancelable: true, target, detail: { attributeName, mutationType } }) |
| 65 | + |
| 66 | + return !event.defaultPrevented |
| 67 | + } |
| 68 | + |
| 69 | + beforeNodeRemoved = (node) => { |
| 70 | + return this.beforeNodeMorphed(node) |
| 71 | + } |
| 72 | + |
| 73 | + afterNodeMorphed = (target, newNode) => { |
| 74 | + if (newNode instanceof HTMLElement) { |
| 75 | + dispatch("turbo:morph-element", { target, detail: { newElement: newNode } }) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function canRefreshFrame(frame) { |
| 81 | + return frame instanceof FrameElement && |
| 82 | + frame.src && |
| 83 | + frame.refresh === "morph" && |
| 84 | + !frame.closest("[data-turbo-permanent]") |
| 85 | +} |
| 86 | + |
| 87 | +function refreshFrame(frame) { |
| 88 | + frame.addEventListener("turbo:before-frame-render", ({ detail }) => { |
| 89 | + detail.render = morphFrames |
| 90 | + }, { once: true }) |
| 91 | + |
| 92 | + frame.reload() |
| 93 | +} |
0 commit comments