Files
buuh/packages/component/onload.js
T
Bendik Aagaard LynghaugandClaude Fable 5 ce1ec9e4a9 rebrand: buuh — a friendly public fork under the uhhm org
Packages renamed to the @uhhm scope (@uhhm/buuh, @uhhm/buuh-html,
@uhhm/buuh-component, @uhhm/buuh-devtools, @uhhm/buuh-migrate,
@uhhm/bankai). Scoping is load-bearing twice over: npm routes registries
per scope so @uhhm/* resolves against project.uhhm.no while everything
else stays on npmjs, and it means this fork never squats upstream's
names anywhere. The codemod now migrates choo v7 apps to the @uhhm
names. README rewritten with the fork framing and full upstream credit;
the choojs RFC moves to docs/upstream-rfc-draft.md, in the drawer for if
this work ever goes home. API unchanged — choo() is still choo().

Also: Gitea Actions CI + release workflows (npm publish to the uhhm
registry on tag push, CDN bundle uploaded as a generic package),
npm run bundle producing dist-cdn/buuh.js (the whole framework as one
minified ES module for import-map use), docs/publishing.md explaining
what Gitea Packages is (a real npm registry) and is not (a CDN — serve
the bundle from a static host with module-safe MIME instead), and
onload.js constructing window.MutationObserver to match its own guard
(surfaced by smoke-testing the bundle outside a full browser).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014NgfSjHE11oFpoSnLVKLXd
2026-09-08 19:55:20 +02:00

107 lines
2.8 KiB
JavaScript

// Ported from on-load 3.4.1 (MIT) — https://github.com/shama/on-load
// Fire load/unload callbacks when watched nodes enter or leave the DOM,
// driven by a single document-wide MutationObserver.
const watch = Object.create(null)
const KEY_ID = 'onloadid' + Math.random().toString(36).slice(2, 8)
const KEY_ATTR = 'data-' + KEY_ID
let INDEX = 0
if (typeof window !== 'undefined' && window.MutationObserver) {
const observer = new window.MutationObserver(function (mutations) {
if (Object.keys(watch).length < 1) return
for (let i = 0; i < mutations.length; i++) {
if (mutations[i].attributeName === KEY_ATTR) {
eachAttr(mutations[i], turnon, turnoff)
continue
}
eachMutation(mutations[i].removedNodes, turnoff)
eachMutation(mutations[i].addedNodes, turnon)
}
})
if (document.body) {
beginObserve(observer)
} else {
document.addEventListener('DOMContentLoaded', function () {
beginObserve(observer)
})
}
}
function beginObserve (observer) {
observer.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true,
attributeOldValue: true,
attributeFilter: [KEY_ATTR]
})
}
export default function onload (el, on, off, caller) {
if (!document.body) throw new Error('onload: will not work prior to DOMContentLoaded')
on = on || function () {}
off = off || function () {}
el.setAttribute(KEY_ATTR, 'o' + INDEX)
watch['o' + INDEX] = [on, off, 0, caller]
INDEX += 1
return el
}
onload.KEY_ATTR = KEY_ATTR
onload.KEY_ID = KEY_ID
export { KEY_ATTR, KEY_ID }
function turnon (index, el) {
if (watch[index][0] && watch[index][2] === 0) {
watch[index][0](el)
watch[index][2] = 1
}
}
function turnoff (index, el) {
if (watch[index][1] && watch[index][2] === 1) {
watch[index][1](el)
watch[index][2] = 0
}
}
function eachAttr (mutation, on, off) {
const newValue = mutation.target.getAttribute(KEY_ATTR)
if (sameOrigin(mutation.oldValue, newValue)) {
watch[newValue] = watch[mutation.oldValue]
return
}
if (watch[mutation.oldValue]) {
off(mutation.oldValue, mutation.target)
}
if (watch[newValue]) {
on(newValue, mutation.target)
}
}
function sameOrigin (oldValue, newValue) {
if (!oldValue || !newValue) return false
if (!watch[oldValue] || !watch[newValue]) return false
return watch[oldValue][3] === watch[newValue][3]
}
function eachMutation (nodes, fn) {
const keys = Object.keys(watch)
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
if (node && node.getAttribute && node.getAttribute(KEY_ATTR)) {
const onloadid = node.getAttribute(KEY_ATTR)
keys.forEach(function (k) {
if (onloadid === k) {
fn(k, node)
}
})
}
if (node.childNodes && node.childNodes.length > 0) {
eachMutation(node.childNodes, fn)
}
}
}