Debian Codegraph @

src/ext/hx-alpine-compat.js

raw symbols SCIP
1 //==========================================================
2 // hx-alpine-compat.js
3 //
4 // Alpine.js compatibility extension for htmx
5 // Initializes Alpine on fragments before swap
6 // and preserves Alpine state during morph operations
7 //==========================================================
8 (() => {
9 let api;
10 let deferCount = 0;
11
12 function maybeFlush() {
13 if (deferCount > 0) deferCount--;
14 if (deferCount === 0 && window.Alpine?.flushAndStopDeferringMutations) {
15 window.Alpine.flushAndStopDeferringMutations();
16 }
17 }
18
19 htmx.registerExtension('alpine-compat', {
20 init: (internalAPI) => {
21 api = internalAPI;
22
23 // Override isSoftMatch to handle Alpine reactive IDs
24 let originalIsSoftMatch = api.isSoftMatch;
25 api.isSoftMatch = function(oldNode, newNode) {
26 // If both have Alpine reactive ID bindings, ignore ID mismatch
27 if (oldNode._x_bindings?.id && newNode.matches?.('[\\:id], [x-bind\\:id]')) {
28 return oldNode instanceof Element && oldNode.tagName === newNode.tagName;
29 }
30 return originalIsSoftMatch(oldNode, newNode);
31 };
32 },
33
34 htmx_before_swap: (elt, detail) => {
35 if (!window.Alpine?.closestDataStack || !window.Alpine?.cloneNode || !window.Alpine?.deferMutations) {
36 return;
37 }
38 if (deferCount === 0) {
39 window.Alpine.deferMutations();
40 }
41 deferCount++;
42
43 let {tasks} = detail;
44 for (let task of tasks) {
45 if (task.swapSpec.style === 'innerMorph' || task.swapSpec.style === 'outerMorph') {
46 if (!task.fragment || !task.target) continue;
47
48 let target = typeof task.target === 'string'
49 ? document.querySelector(task.target)
50 : task.target;
51 if (!target) continue;
52 }
53 }
54 },
55
56 htmx_before_morph_node: (elt, detail) => {
57 if (!window.Alpine?.closestDataStack || !window.Alpine?.cloneNode) {
58 return;
59 }
60 let {oldNode, newNode} = detail;
61
62 let oldDataStack = window.Alpine.closestDataStack(oldNode);
63 newNode._x_dataStack = oldDataStack;
64
65 // skip cloneNode for template children that will have errors as they can not have reactive content
66 if (!oldNode.isConnected) return;
67
68 window.Alpine.cloneNode(oldNode, newNode);
69
70 // If both have _x_teleport, morph the teleport target
71 if (oldNode._x_teleport && newNode._x_teleport) {
72 let fragment = document.createDocumentFragment();
73 fragment.append(newNode._x_teleport);
74 api.morph(oldNode._x_teleport, fragment, false);
75 }
76 },
77
78 htmx_history_cache_before_save: (elt, detail) => {
79 if (!window.Alpine?.destroyTree) return;
80 detail.target.querySelectorAll('[x-data]').forEach(el => {
81 if (el._x_dataStack) {
82 el.setAttribute('data-alpine-state', JSON.stringify(el._x_dataStack[0]));
83 }
84 });
85 window.Alpine.destroyTree(detail.target);
86 },
87
88 htmx_history_cache_after_restore: (elt, detail) => {
89 if (!window.Alpine) return;
90 document.querySelectorAll('[data-alpine-state]').forEach(el => {
91 let saved = JSON.parse(el.getAttribute('data-alpine-state'));
92 el.removeAttribute('data-alpine-state');
93 if (el._x_dataStack) {
94 for (let key in saved) {
95 try { el._x_dataStack[0][key] = saved[key]; } catch {}
96 }
97 }
98 });
99 },
100
101 htmx_after_swap: (elt, detail) => {
102 detail.ctx._alpineFlushed = true;
103 maybeFlush();
104 },
105
106 htmx_finally_request: (elt, detail) => {
107 if (!detail.ctx._alpineFlushed) maybeFlush();
108 }
109 });
110 })();