| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
(() => { |
| 8 |
let api; |
| 9 |
let fns = new Set(); |
| 10 |
let pending = false; |
| 11 |
let dbSym = Symbol(); |
| 12 |
let observer = null; |
| 13 |
let recomputeBound = null; |
| 14 |
let swaps = 0; |
| 15 |
let i = 0; |
| 16 |
let start = 0; |
| 17 |
let warned = false; |
| 18 |
|
| 19 |
const OBSERVE_OPTIONS = { childList: true, subtree: true, attributes: true, characterData: true }; |
| 20 |
|
| 21 |
function ensureActive() { |
| 22 |
if (observer) return; |
| 23 |
recomputeBound = () => schedule(); |
| 24 |
document.addEventListener('input', recomputeBound, true); |
| 25 |
document.addEventListener('change', recomputeBound, true); |
| 26 |
observer = new MutationObserver(recomputeBound); |
| 27 |
observer.observe(document.documentElement, OBSERVE_OPTIONS); |
| 28 |
} |
| 29 |
|
| 30 |
function deactivate() { |
| 31 |
if (!observer) return; |
| 32 |
document.removeEventListener('input', recomputeBound, true); |
| 33 |
document.removeEventListener('change', recomputeBound, true); |
| 34 |
observer.disconnect(); |
| 35 |
observer = null; |
| 36 |
recomputeBound = null; |
| 37 |
} |
| 38 |
|
| 39 |
function schedule() { |
| 40 |
if (pending) return; |
| 41 |
if (swaps > 0) return; |
| 42 |
let now = Date.now(); |
| 43 |
if (now - start > 1000) { |
| 44 |
start = now; |
| 45 |
i = 0; |
| 46 |
warned = false; |
| 47 |
} |
| 48 |
if (++i > 50 && !warned) { |
| 49 |
console.warn('htmx: hx-live recompute exceeded 50/sec.'); |
| 50 |
warned = true; |
| 51 |
} |
| 52 |
pending = true; |
| 53 |
queueMicrotask(() => { |
| 54 |
|
| 55 |
observer?.disconnect(); |
| 56 |
fns.forEach(f => f()); |
| 57 |
if (fns.size === 0) { |
| 58 |
deactivate(); |
| 59 |
} else { |
| 60 |
observer.observe(document.documentElement, OBSERVE_OPTIONS); |
| 61 |
} |
| 62 |
pending = false; |
| 63 |
}); |
| 64 |
} |
| 65 |
|
| 66 |
let BOOLEAN_ATTRS = new Set([ |
| 67 |
'disabled','hidden','required','readonly','open','inert', |
| 68 |
'multiple','autofocus','novalidate','default','reversed', |
| 69 |
'loop','muted','controls','autoplay','playsinline', |
| 70 |
'formnovalidate','async','defer','ismap','typemustmatch', |
| 71 |
'allowfullscreen','itemscope','nomodule' |
| 72 |
]); |
| 73 |
let PROPERTY_ATTRS = new Set(['checked','value','selected']); |
| 74 |
let STRINGY_BOOLEAN_ATTRS = new Set(['contenteditable','draggable','spellcheck']); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
function applyAttr(elts, name, ...rest) { |
| 97 |
let isClass = name.startsWith('.'); |
| 98 |
let isMultiClass = name === 'class'; |
| 99 |
let isAria = name.startsWith('aria-'); |
| 100 |
let isPropAttr = PROPERTY_ATTRS.has(name); |
| 101 |
|
| 102 |
if (rest.length === 0) { |
| 103 |
let e = elts[0]; |
| 104 |
if (!e) return undefined; |
| 105 |
if (isClass) return e.classList.contains(name.slice(1)); |
| 106 |
if (isMultiClass) return e.getAttribute('class'); |
| 107 |
if (isAria) return e.getAttribute(name) === 'true'; |
| 108 |
if (BOOLEAN_ATTRS.has(name)) return e.hasAttribute(name); |
| 109 |
if (isPropAttr) return e[name]; |
| 110 |
return e.getAttribute(name); |
| 111 |
} |
| 112 |
|
| 113 |
let value = rest[0]; |
| 114 |
for (let e of elts) { |
| 115 |
if (isClass) { |
| 116 |
e.classList.toggle(name.slice(1), !!value); |
| 117 |
if (e.classList.length === 0) e.removeAttribute('class'); |
| 118 |
} else if (isMultiClass) { |
| 119 |
applyMultiClass(e, value); |
| 120 |
} else if (isAria) { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
let attrVal = (typeof value === 'string' || typeof value === 'number') |
| 125 |
? String(value) |
| 126 |
: (value ? 'true' : 'false'); |
| 127 |
e.setAttribute(name, attrVal); |
| 128 |
} else if (isPropAttr) { |
| 129 |
if (value === false || value == null) { |
| 130 |
e[name] = (typeof e[name] === 'boolean') ? false : ''; |
| 131 |
e.removeAttribute(name); |
| 132 |
} else if (value === true) { |
| 133 |
e[name] = true; |
| 134 |
e.setAttribute(name, ''); |
| 135 |
} else { |
| 136 |
e[name] = value; |
| 137 |
e.setAttribute(name, String(value)); |
| 138 |
} |
| 139 |
} else if (BOOLEAN_ATTRS.has(name)) { |
| 140 |
if (value) e.setAttribute(name, ''); |
| 141 |
else e.removeAttribute(name); |
| 142 |
} else if (STRINGY_BOOLEAN_ATTRS.has(name)) { |
| 143 |
if (value === null || value === undefined) e.removeAttribute(name); |
| 144 |
else if (value === true) e.setAttribute(name, 'true'); |
| 145 |
else if (value === false) e.setAttribute(name, 'false'); |
| 146 |
else e.setAttribute(name, String(value)); |
| 147 |
} else { |
| 148 |
if (value === null || value === undefined || value === false) e.removeAttribute(name); |
| 149 |
else e.setAttribute(name, value === true ? '' : String(value)); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
function applyStyleBinding(elt, value) { |
| 155 |
let prop = api.htmxProp(elt); |
| 156 |
let oldManaged = prop.liveStyles || new Set(); |
| 157 |
let newManaged = new Set(); |
| 158 |
|
| 159 |
if (typeof value === 'string') { |
| 160 |
for (let decl of value.split(';')) { |
| 161 |
let idx = decl.indexOf(':'); |
| 162 |
if (idx < 0) continue; |
| 163 |
let k = decl.slice(0, idx).trim(); |
| 164 |
let v = decl.slice(idx + 1).trim(); |
| 165 |
if (k) { |
| 166 |
newManaged.add(k); |
| 167 |
elt.style.setProperty(k, v); |
| 168 |
} |
| 169 |
} |
| 170 |
} else if (value && typeof value === 'object') { |
| 171 |
for (let [k, v] of Object.entries(value)) { |
| 172 |
let cssProp = camelToKebab(k); |
| 173 |
newManaged.add(cssProp); |
| 174 |
if (v == null || v === '') elt.style.removeProperty(cssProp); |
| 175 |
else elt.style.setProperty(cssProp, String(v)); |
| 176 |
} |
| 177 |
} |
| 178 |
for (let k of oldManaged) if (!newManaged.has(k)) elt.style.removeProperty(k); |
| 179 |
if (elt.style.length === 0) elt.removeAttribute('style'); |
| 180 |
prop.liveStyles = newManaged; |
| 181 |
} |
| 182 |
|
| 183 |
function camelToKebab(s) { |
| 184 |
return s.replace(/[A-Z]/g, m => '-' + m.toLowerCase()); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
function makeDataProxy(elt) { |
| 191 |
return new Proxy({}, { |
| 192 |
get: (_, prop) => { |
| 193 |
if (typeof prop !== 'string') return undefined; |
| 194 |
let kebab = camelToKebab(prop); |
| 195 |
let ancestor = elt.closest('[data-' + kebab + ']'); |
| 196 |
if (!ancestor) return undefined; |
| 197 |
let raw = ancestor.dataset[prop]; |
| 198 |
try { return JSON.parse(raw); } catch { return raw; } |
| 199 |
}, |
| 200 |
set: (_, prop, val) => { |
| 201 |
if (typeof prop !== 'string') return false; |
| 202 |
let kebab = camelToKebab(prop); |
| 203 |
let target = elt.closest('[data-' + kebab + ']') || elt; |
| 204 |
target.dataset[prop] = typeof val === 'string' ? val : JSON.stringify(val); |
| 205 |
return true; |
| 206 |
}, |
| 207 |
has: (_, prop) => { |
| 208 |
if (typeof prop !== 'string') return false; |
| 209 |
let kebab = camelToKebab(prop); |
| 210 |
return !!elt.closest('[data-' + kebab + ']'); |
| 211 |
}, |
| 212 |
ownKeys: () => { |
| 213 |
let result = []; |
| 214 |
let seen = new Set(); |
| 215 |
for (let node = elt; node; node = node.parentElement) { |
| 216 |
for (let key of Object.keys(node.dataset)) { |
| 217 |
if (key !== 'htmxPowered' && !seen.has(key)) { |
| 218 |
seen.add(key); |
| 219 |
result.push(key); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
return result; |
| 224 |
}, |
| 225 |
getOwnPropertyDescriptor: (_, prop) => { |
| 226 |
if (typeof prop !== 'string' || prop === 'htmxPowered') return; |
| 227 |
let kebab = camelToKebab(prop); |
| 228 |
if (elt.closest('[data-' + kebab + ']')) return { enumerable: true, configurable: true }; |
| 229 |
} |
| 230 |
}); |
| 231 |
} |
| 232 |
|
| 233 |
function applyMultiClass(elt, value) { |
| 234 |
let prop = api.htmxProp(elt); |
| 235 |
let oldManaged = prop.liveClasses || new Set(); |
| 236 |
let newManaged = new Set(); |
| 237 |
|
| 238 |
if (typeof value === 'string') { |
| 239 |
for (let c of value.trim().split(/\s+/).filter(Boolean)) { |
| 240 |
newManaged.add(c); |
| 241 |
elt.classList.add(c); |
| 242 |
} |
| 243 |
} else if (value && typeof value === 'object') { |
| 244 |
for (let [key, cond] of Object.entries(value)) { |
| 245 |
for (let c of key.trim().split(/\s+/).filter(Boolean)) { |
| 246 |
newManaged.add(c); |
| 247 |
elt.classList.toggle(c, !!cond); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
for (let c of oldManaged) if (!newManaged.has(c)) elt.classList.remove(c); |
| 252 |
if (elt.classList.length === 0) elt.removeAttribute('class'); |
| 253 |
prop.liveClasses = newManaged; |
| 254 |
} |
| 255 |
|
| 256 |
function applyTake(targets, name, scope) { |
| 257 |
let isClass = name.startsWith('.'); |
| 258 |
let key = isClass ? name.slice(1) : name; |
| 259 |
let isAria = name.startsWith('aria-'); |
| 260 |
let auto = isClass ? '.' + key : '[' + name + ']'; |
| 261 |
let root = scope == null ? targets[0]?.parentElement |
| 262 |
: scope.nodeType ? scope : null; |
| 263 |
let sources = root |
| 264 |
? [root, ...root.querySelectorAll(auto)] |
| 265 |
: document.querySelectorAll(typeof scope === 'string' ? scope : scope?.from || auto); |
| 266 |
let targetSet = new Set(targets); |
| 267 |
for (let s of sources) { |
| 268 |
if (targetSet.has(s)) continue; |
| 269 |
if (isClass) { |
| 270 |
s.classList?.remove(key); |
| 271 |
if (s.classList?.length === 0) s.removeAttribute('class'); |
| 272 |
} else if (isAria) { |
| 273 |
s.setAttribute(name, 'false'); |
| 274 |
} else { |
| 275 |
s.removeAttribute(name); |
| 276 |
} |
| 277 |
} |
| 278 |
for (let t of targets) { |
| 279 |
if (isClass) t.classList?.add(key); |
| 280 |
else if (isAria) t.setAttribute(name, 'true'); |
| 281 |
else t.setAttribute(name, ''); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
function forEvent(elt, ...args) { |
| 286 |
let target = elt || document; |
| 287 |
for (let a of args) if (a?.nodeType) target = a; |
| 288 |
return new Promise(resolve => { |
| 289 |
let cleanups = [], done = false; |
| 290 |
let fire = v => { if (done) return; done = true; for (let c of cleanups) c(); resolve(v); }; |
| 291 |
for (let a of args) { |
| 292 |
if (a == null || a?.nodeType) continue; |
| 293 |
let ms = typeof a === 'number' ? a |
| 294 |
: (typeof a === 'string' ? htmx.parseInterval(a) : undefined); |
| 295 |
if (ms !== undefined && ms > 0) { |
| 296 |
let id = setTimeout(() => fire(a), ms); |
| 297 |
cleanups.push(() => clearTimeout(id)); |
| 298 |
} else if (typeof a === 'string') { |
| 299 |
let h = evt => fire(evt); |
| 300 |
target.addEventListener(a, h, { once: true }); |
| 301 |
cleanups.push(() => target.removeEventListener(a, h)); |
| 302 |
} |
| 303 |
} |
| 304 |
}); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
e" |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
ot |
| 321 |
|
| 322 |
function applyToggle(name, values, element) { |
| 323 |
let isClass = name.startsWith('.'); |
| 324 |
let key = isClass ? name.slice(1) : name; |
| 325 |
let isAria = name.startsWith('aria-'); |
| 326 |
let asArray = values && (typeof values === 'string' |
| 327 |
? values.split('|').map(v => v.trim()) |
| 328 |
: values); |
| 329 |
|
| 330 |
if (!asArray) { |
| 331 |
if (isClass) element.classList.toggle(key); |
| 332 |
else if (isAria) { |
| 333 |
let cur = element.getAttribute(name); |
| 334 |
element.setAttribute(name, cur === 'true' ? 'false' : 'true'); |
| 335 |
} else { |
| 336 |
element.toggleAttribute(name); |
| 337 |
} |
| 338 |
return; |
| 339 |
} |
| 340 |
if (isClass) { |
| 341 |
let cur = asArray.findIndex(v => v && element.classList.contains(v)); |
| 342 |
if (cur >= 0) element.classList.remove(asArray[cur]); |
| 343 |
let next = asArray[(cur + 1) % asArray.length]; |
| 344 |
if (next) element.classList.add(next); |
| 345 |
} else { |
| 346 |
let curVal = element.getAttribute(name) ?? ''; |
| 347 |
let cur = asArray.indexOf(curVal); |
| 348 |
let next = asArray[(cur + 1) % asArray.length]; |
| 349 |
if (next === '') element.removeAttribute(name); |
| 350 |
else element.setAttribute(name, next); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
function makeDebounce() { |
| 355 |
|
| 356 |
let channels = new Map(); |
| 357 |
let chan = key => channels.get(key) || (channels.set(key, { last: 0, reject: null }), channels.get(key)); |
| 358 |
return (ms, fn) => { |
| 359 |
let ch = chan(fn ? fn.toString() : null); |
| 360 |
ch.reject?.(dbSym); |
| 361 |
ch.reject = null; |
| 362 |
let id = ++ch.last; |
| 363 |
if (fn) { |
| 364 |
setTimeout(() => id === ch.last && fn(), ms); |
| 365 |
return; |
| 366 |
} |
| 367 |
return new Promise((res, rej) => { |
| 368 |
ch.reject = rej; |
| 369 |
setTimeout(() => { |
| 370 |
if (id !== ch.last) return; |
| 371 |
ch.reject = null; |
| 372 |
res(); |
| 373 |
}, ms); |
| 374 |
}); |
| 375 |
}; |
| 376 |
} |
| 377 |
|
| 378 |
function getDebounce(elt) { |
| 379 |
let prop = api.htmxProp(elt); |
| 380 |
return prop.debounce || (prop.debounce = makeDebounce()); |
| 381 |
} |
| 382 |
|
| 383 |
function makeQ(ctx, defaultRoot = document) { |
| 384 |
return selectorOrElt => { |
| 385 |
if (typeof selectorOrElt !== 'string') { |
| 386 |
return qProxy( |
| 387 |
selectorOrElt?.nodeType ? [selectorOrElt] : [...(selectorOrElt || [])] |
| 388 |
); |
| 389 |
} |
| 390 |
let sel = selectorOrElt; |
| 391 |
let inMatch = sel.match(/^(.+)\s+in\s+(.+)$/); |
| 392 |
let roots = [defaultRoot]; |
| 393 |
if (inMatch) { |
| 394 |
sel = inMatch[1]; |
| 395 |
if (inMatch[2] === 'this' || inMatch[2] === 'me') { |
| 396 |
roots = [ctx]; |
| 397 |
} else { |
| 398 |
roots = [...document.querySelectorAll(inMatch[2])]; |
| 399 |
} |
| 400 |
} |
| 401 |
if (!roots.length) return qProxy([]); |
| 402 |
let qsa = s => { |
| 403 |
if (roots.length === 1) return [...roots[0].querySelectorAll(s)]; |
| 404 |
let out = [], seen = new Set(); |
| 405 |
for (let r of roots) for (let e of r.querySelectorAll(s)) { |
| 406 |
if (!seen.has(e)) { seen.add(e); out.push(e); } |
| 407 |
} |
| 408 |
return out.sort((a, b) => a.compareDocumentPosition(b) & 4 ? -1 : 1); |
| 409 |
}; |
| 410 |
let dirMatch = sel.match(/^(next|previous|closest|first|last)\s+(.+)$/); |
| 411 |
let elts; |
| 412 |
if (dirMatch) { |
| 413 |
let [, dir, s] = dirMatch; |
| 414 |
let cdp = e => ctx.compareDocumentPosition(e); |
| 415 |
if (dir === 'closest') { |
| 416 |
let c = ctx.closest?.(s); |
| 417 |
elts = c ? [c] : []; |
| 418 |
} else { |
| 419 |
let all = qsa(s); |
| 420 |
if (dir === 'first') elts = all.slice(0, 1); |
| 421 |
else if (dir === 'last') elts = all.slice(-1); |
| 422 |
else if (dir === 'next') { |
| 423 |
let n = all.find(e => cdp(e) & 4); |
| 424 |
elts = n ? [n] : []; |
| 425 |
} else { |
| 426 |
let p = all.reverse().find(e => cdp(e) & 2); |
| 427 |
elts = p ? [p] : []; |
| 428 |
} |
| 429 |
} |
| 430 |
} else { |
| 431 |
elts = qsa(sel); |
| 432 |
} |
| 433 |
return qProxy(elts); |
| 434 |
}; |
| 435 |
} |
| 436 |
|
| 437 |
let arrayMethods = new Set(['map', 'filter', 'reduce', 'reduceRight', 'forEach', 'some', 'every', |
| 438 |
'find', 'findIndex', 'findLast', 'findLastIndex', 'flatMap', 'flat', |
| 439 |
'slice', 'indexOf', 'lastIndexOf', 'includes', 'join', 'at']); |
| 440 |
|
| 441 |
let positions = { before: 'beforebegin', after: 'afterend', start: 'afterbegin', end: 'beforeend' }; |
| 442 |
|
| 443 |
function qProxy(elts) { |
| 444 |
let proxy = new Proxy({}, { |
| 445 |
get: (_, p) => { |
| 446 |
if (p === 'count') return elts.length; |
| 447 |
if (p === 'arr') return () => elts.slice(); |
| 448 |
if (p === Symbol.iterator) return () => elts.values(); |
| 449 |
if (p === 'q') return s => { |
| 450 |
let out = new Set(); |
| 451 |
for (let e of elts) for (let r of makeQ(e, e)(s).arr()) out.add(r); |
| 452 |
return qProxy([...out]); |
| 453 |
}; |
| 454 |
if (p === 'trigger') return (t, d, b) => { elts.forEach(e => htmx.trigger(e, t, d, b)); return proxy; }; |
| 455 |
if (p === 'insert') return (pos, s) => { elts.forEach(e => e.insertAdjacentHTML(positions[pos], s)); return proxy; }; |
| 456 |
if (p === 'take') return (name, scope) => { applyTake(elts, name, scope); return proxy; }; |
| 457 |
if (p === 'toggle') return (name, values) => { elts.forEach(e => applyToggle(name, values, e)); return proxy; }; |
| 458 |
if (p === 'attr') return (name, ...rest) => { |
| 459 |
if (rest.length === 0) return applyAttr(elts, name); |
| 460 |
applyAttr(elts, name, ...rest); |
| 461 |
return proxy; |
| 462 |
}; |
| 463 |
if (p === 'data') return elts[0] ? makeDataProxy(elts[0]) : undefined; |
| 464 |
if (arrayMethods.has(p)) return elts[p].bind(elts); |
| 465 |
let v = elts[0]?.[p]; |
| 466 |
if (typeof v === 'function') return (...a) => elts.map(e => e[p](...a))[0]; |
| 467 |
if (v && typeof v === 'object') return qProxy(elts.map(e => e[p])); |
| 468 |
return v; |
| 469 |
}, |
| 470 |
set: (_, p, v) => { |
| 471 |
elts.forEach(e => e[p] = v); |
| 472 |
schedule(); |
| 473 |
return true; |
| 474 |
} |
| 475 |
}); |
| 476 |
return proxy; |
| 477 |
} |
| 478 |
|
| 479 |
let liveQuery, bindPrefixes, bodyAttrs; |
| 480 |
|
| 481 |
function buildLiveQuery() { |
| 482 |
let mc = htmx.config.metaCharacter || ':'; |
| 483 |
let p = htmx.config.prefix; |
| 484 |
bindPrefixes = ['hx-live' + mc]; |
| 485 |
if (p) bindPrefixes.push(p + 'live' + mc); |
| 486 |
let extra = htmx.config.live?.bindPrefix; |
| 487 |
if (extra === undefined) { |
| 488 |
if (window.Alpine) { |
| 489 |
extra = ''; |
| 490 |
console.warn('hx-live: Alpine.js detected — ":" short-form bindings disabled. Set htmx.config.live.bindPrefix to configure.'); |
| 491 |
} else { |
| 492 |
extra = ':'; |
| 493 |
} |
| 494 |
} |
| 495 |
if (extra) bindPrefixes.push(extra); |
| 496 |
bodyAttrs = ['hx-live']; |
| 497 |
if (p) bodyAttrs.push(p + 'live'); |
| 498 |
let bind = bindPrefixes.map(bp => `starts-with(name(), "${bp}")`).join(' or '); |
| 499 |
let body = bodyAttrs.map(n => `@${n}`).join(' or '); |
| 500 |
liveQuery = new XPathEvaluator().createExpression(`.//*[@*[${bind}] or ${body}]`); |
| 501 |
} |
| 502 |
|
| 503 |
function extractBindingName(attrName) { |
| 504 |
for (let p of bindPrefixes) { |
| 505 |
if (attrName.startsWith(p) && attrName.length > p.length) return attrName.slice(p.length); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
function cleanupLive(elt) { |
| 510 |
let prop = elt._htmx; |
| 511 |
if (!prop?.liveRuns) return; |
| 512 |
for (let run of prop.liveRuns) fns.delete(run); |
| 513 |
delete prop.liveRuns; |
| 514 |
delete prop.liveRegistered; |
| 515 |
delete prop.liveAttrs; |
| 516 |
} |
| 517 |
|
| 518 |
function processElement(elt) { |
| 519 |
if (elt.closest('[hx-ignore]')) return; |
| 520 |
let prop = api.htmxProp(elt); |
| 521 |
if (!prop.liveRegistered) { |
| 522 |
let bodyAttr = bodyAttrs.find(n => elt.hasAttribute(n)); |
| 523 |
if (bodyAttr) { |
| 524 |
prop.liveRegistered = true; |
| 525 |
ensureActive(); |
| 526 |
let code = elt.getAttribute(bodyAttr) |
| 527 |
let debounce = getDebounce(elt); |
| 528 |
let run = async () => { |
| 529 |
if (!elt.isConnected) { |
| 530 |
fns.delete(run); |
| 531 |
return; |
| 532 |
} |
| 533 |
try { |
| 534 |
await api.executeJavaScript(elt, { debounce }, code, false); |
| 535 |
} catch (e) { |
| 536 |
if (e !== dbSym) console.error('htmx: hx-live expression threw', e, { elt }); |
| 537 |
} |
| 538 |
}; |
| 539 |
fns.add(run); |
| 540 |
prop.liveRuns = prop.liveRuns || new Set(); |
| 541 |
prop.liveRuns.add(run); |
| 542 |
run(); |
| 543 |
} |
| 544 |
} |
| 545 |
prop.liveAttrs ||= new Set(); |
| 546 |
for (let a of elt.attributes) { |
| 547 |
let name = extractBindingName(a.name); |
| 548 |
if (!name || prop.liveAttrs.has(name)) continue; |
| 549 |
prop.liveAttrs.add(name); |
| 550 |
registerSimpleLive(elt, name, a.value); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
function processLive(root) { |
| 555 |
if (!liveQuery) buildLiveQuery(); |
| 556 |
if (root.nodeType === 1) processElement(root); |
| 557 |
let iter = liveQuery.evaluate(root), node, nodes = []; |
| 558 |
while (node = iter.iterateNext()) nodes.push(node); |
| 559 |
for (node of nodes) processElement(node); |
| 560 |
} |
| 561 |
|
| 562 |
function registerSimpleLive(elt, attrName, code) { |
| 563 |
ensureActive(); |
| 564 |
let debounce = getDebounce(elt); |
| 565 |
let isAsync = /\bawait\b/.test(code); |
| 566 |
let run = isAsync ? async () => { |
| 567 |
if (!elt.isConnected) { |
| 568 |
fns.delete(run); |
| 569 |
return; |
| 570 |
} |
| 571 |
try { |
| 572 |
let value = await api.executeJavaScript(elt, { debounce }, code, true); |
| 573 |
writeAttrBinding(elt, attrName, value); |
| 574 |
observer?.takeRecords(); |
| 575 |
} catch (e) { |
| 576 |
if (e !== dbSym) console.error('htmx: hx-live expression threw', e, { elt, attr: attrName }); |
| 577 |
} |
| 578 |
} : () => { |
| 579 |
if (!elt.isConnected) { |
| 580 |
fns.delete(run); |
| 581 |
return; |
| 582 |
} |
| 583 |
try { |
| 584 |
let value = api.executeJavaScript(elt, { debounce }, code, true, false); |
| 585 |
writeAttrBinding(elt, attrName, value); |
| 586 |
} catch (e) { |
| 587 |
if (e !== dbSym) console.error('htmx: hx-live expression threw', e, { elt, attr: attrName }); |
| 588 |
} |
| 589 |
}; |
| 590 |
fns.add(run); |
| 591 |
let prop = api.htmxProp(elt); |
| 592 |
prop.liveRuns = prop.liveRuns || new Set(); |
| 593 |
prop.liveRuns.add(run); |
| 594 |
run(); |
| 595 |
} |
| 596 |
|
| 597 |
function writeAttrBinding(elt, attrName, value) { |
| 598 |
if (attrName === 'text') { elt.textContent = value == null ? '' : String(value); return; } |
| 599 |
if (attrName === 'html') { elt.innerHTML = value == null ? '' : String(value); return; } |
| 600 |
if (attrName === 'style') { applyStyleBinding(elt, value); return; } |
| 601 |
r. |
| 602 |
applyAttr([elt], attrName, value); |
| 603 |
} |
| 604 |
|
| 605 |
let asTargets = t => t == null ? [] |
| 606 |
: typeof t === 'string' ? document.querySelectorAll(t) |
| 607 |
: t.nodeType ? [t] |
| 608 |
: t; |
| 609 |
|
| 610 |
htmx.live = { |
| 611 |
q: s => makeQ(document.documentElement)(s), |
| 612 |
debounce: makeDebounce(), |
| 613 |
refresh: () => schedule(), |
| 614 |
take: (target, name, scope) => applyTake([...asTargets(target)], name, scope), |
| 615 |
toggle: (target, name, values) => [...asTargets(target)].forEach(e => applyToggle(name, values, e)), |
| 616 |
attr: (target, name, ...rest) => applyAttr([...asTargets(target)], name, ...rest), |
| 617 |
forEvent: (...args) => forEvent(null, ...args), |
| 618 |
nextFrame: () => new Promise(r => requestAnimationFrame(r)) |
| 619 |
}; |
| 620 |
|
| 621 |
htmx.registerExtension('hx-live', { |
| 622 |
init: (internalAPI) => { |
| 623 |
api = internalAPI; |
| 624 |
}, |
| 625 |
htmx_before_cleanup: (elt) => { |
| 626 |
cleanupLive(elt); |
| 627 |
}, |
| 628 |
htmx_before_morph_attr: (elt, detail) => { |
| 629 |
if (bindPrefixes.some(p => detail.attrName.startsWith(p))) cleanupLive(elt); |
| 630 |
}, |
| 631 |
htmx_after_process: (elt) => { |
| 632 |
processLive(elt); |
| 633 |
}, |
| 634 |
htmx_before_swap: () => { |
| 635 |
swaps++; |
| 636 |
}, |
| 637 |
htmx_swap_finally: () => { |
| 638 |
if (--swaps === 0 && fns.size > 0) schedule(); |
| 639 |
}, |
| 640 |
htmx_scope: (elt, detail) => { |
| 641 |
Object.assign(detail.scope, { |
| 642 |
q: makeQ(elt), |
| 643 |
forEvent: (...args) => forEvent(elt, ...args), |
| 644 |
nextFrame: () => new Promise(r => requestAnimationFrame(r)), |
| 645 |
trigger: (type, detail, bubbles) => htmx.trigger(elt, type, detail, bubbles), |
| 646 |
debounce: getDebounce(elt), |
| 647 |
take: (name, scope) => applyTake([elt], name, scope), |
| 648 |
toggle: (name, values) => applyToggle(name, values, elt), |
| 649 |
attr: (name, ...rest) => applyAttr([elt], name, ...rest), |
| 650 |
insert: (pos, html) => elt.insertAdjacentHTML(positions[pos], html), |
| 651 |
matches: (sel) => elt.matches(sel), |
| 652 |
style: elt.style, |
| 653 |
classList: elt.classList, |
| 654 |
data: makeDataProxy(elt) |
| 655 |
}); |
| 656 |
} |
| 657 |
}); |
| 658 |
})(); |