-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathhx-optimistic.js
More file actions
101 lines (87 loc) · 3.37 KB
/
hx-optimistic.js
File metadata and controls
101 lines (87 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(() =>{
function normalizeSwapStyle(style) {
return style === 'before' ? 'beforebegin' :
style === 'after' ? 'afterend' :
style === 'prepend' ? 'afterbegin' :
style === 'append' ? 'beforeend' : style;
}
let api;
function insertOptimisticContent(ctx) {
ctx.optimistic = api.attributeValue(ctx.sourceElement, "hx-optimistic");
if (!ctx.optimistic) {
return
}
let sourceElt = document.querySelector(ctx.optimistic);
if (!sourceElt) return;
let target = ctx.target;
if (typeof target === 'string') {
target = document.querySelector(target);
}
if (!target) return;
// Create optimistic div with reset styling
let optimisticDiv = document.createElement('div');
optimisticDiv.style.cssText = 'all: initial';
optimisticDiv.classList.add('hx-optimistic');
optimisticDiv.innerHTML = sourceElt.innerHTML;
// Set data-* for each request param
if (ctx.optimisticBody) {
let keys = new Set(ctx.optimisticBody.keys());
for (let k of keys) {
let values = ctx.optimisticBody.getAll(k).filter(v => typeof v === 'string');
if (!values.length) continue;
let val = values.length === 1 ? values[0] : JSON.stringify(values);
try {
optimisticDiv.dataset[k] = val;
} catch (e) {
try {
optimisticDiv.setAttribute('data-' + k, val);
} catch (e2) { /* truly invalid name, skip */ }
}
}
}
let swapStyle = normalizeSwapStyle(ctx.swap);
ctx.optHidden = [];
if (swapStyle === 'innerHTML') {
// Hide children of target
for (let child of target.children) {
child.style.display = 'none';
ctx.optHidden.push(child);
}
target.appendChild(optimisticDiv);
} else if (['beforebegin', 'afterbegin', 'beforeend', 'afterend'].includes(swapStyle)) {
target.insertAdjacentElement(swapStyle, optimisticDiv);
} else {
// Assume outerHTML-like behavior, Hide target and insert div after it
target.style.display = 'none';
ctx.optHidden.push(target);
target.after(optimisticDiv);
}
ctx.optimisticDiv = optimisticDiv;
htmx.process(optimisticDiv);
}
function removeOptimisticContent(ctx) {
if (!ctx.optimisticDiv) return;
// Remove optimistic div
ctx.optimisticDiv.remove();
// Unhide any hidden elements
for (let elt of ctx.optHidden) {
elt.style.display = '';
}
}
htmx.registerExtension('hx-optimistic', {
init: (internalAPI) => { api = internalAPI; },
htmx_config_request: (elt, detail) => {
let body = detail.ctx.request.body;
if (body?.entries) detail.ctx.optimisticBody = body;
},
htmx_before_request: (elt, detail) => {
insertOptimisticContent(detail.ctx);
},
htmx_error : (elt, detail) => {
removeOptimisticContent(detail.ctx)
},
htmx_before_swap : (elt, detail) => {
removeOptimisticContent(detail.ctx)
}
});
})();