@rubenlangeweg/mero-ui 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/README.md +50 -11
- package/agent-docs/components/accordion.md +1 -1
- package/agent-docs/components/action-bar.md +1 -1
- package/agent-docs/components/action-button.md +1 -1
- package/agent-docs/components/agenda.md +1 -1
- package/agent-docs/components/alert-dialog.md +1 -1
- package/agent-docs/components/alert.md +1 -1
- package/agent-docs/components/app-layout.md +1 -1
- package/agent-docs/components/area-chart.md +1 -1
- package/agent-docs/components/aura.md +1 -1
- package/agent-docs/components/autocomplete.md +1 -1
- package/agent-docs/components/avatar-group.md +1 -1
- package/agent-docs/components/avatar.md +1 -1
- package/agent-docs/components/badge.md +1 -1
- package/agent-docs/components/bar-chart.md +1 -1
- package/agent-docs/components/breadcrumbs.md +1 -1
- package/agent-docs/components/browser-mockup.md +1 -1
- package/agent-docs/components/button-group.md +1 -1
- package/agent-docs/components/button.md +1 -1
- package/agent-docs/components/calendar.md +3 -3
- package/agent-docs/components/card.md +1 -1
- package/agent-docs/components/carousel.md +1 -1
- package/agent-docs/components/cell-color-picker.md +1 -1
- package/agent-docs/components/cell-select.md +1 -1
- package/agent-docs/components/cell-slider.md +1 -1
- package/agent-docs/components/cell-switch.md +1 -1
- package/agent-docs/components/chain-of-thought.md +1 -1
- package/agent-docs/components/chart-tooltip.md +1 -1
- package/agent-docs/components/chat-attachment.md +1 -1
- package/agent-docs/components/chat-conversation.md +1 -1
- package/agent-docs/components/chat-list-view.md +1 -1
- package/agent-docs/components/chat-loader.md +1 -1
- package/agent-docs/components/chat-message-actions.md +1 -1
- package/agent-docs/components/chat-message.md +1 -1
- package/agent-docs/components/chat-source.md +1 -1
- package/agent-docs/components/chat-tool.md +1 -1
- package/agent-docs/components/checkbox-button-group.md +1 -1
- package/agent-docs/components/checkbox-group.md +1 -1
- package/agent-docs/components/checkbox.md +1 -1
- package/agent-docs/components/chip.md +1 -1
- package/agent-docs/components/close-button.md +1 -1
- package/agent-docs/components/code-block.md +1 -1
- package/agent-docs/components/code-mockup.md +1 -1
- package/agent-docs/components/color-area.md +1 -1
- package/agent-docs/components/color-field.md +1 -1
- package/agent-docs/components/color-picker.md +1 -1
- package/agent-docs/components/color-slider.md +1 -1
- package/agent-docs/components/color-swatch.md +1 -1
- package/agent-docs/components/combo-box.md +41 -1
- package/agent-docs/components/command-menu.md +1 -1
- package/agent-docs/components/command.md +1 -1
- package/agent-docs/components/composed-chart.md +1 -1
- package/agent-docs/components/context-menu.md +1 -1
- package/agent-docs/components/countdown.md +1 -1
- package/agent-docs/components/data-grid.md +1 -1
- package/agent-docs/components/date-field.md +1 -1
- package/agent-docs/components/date-picker.md +1 -1
- package/agent-docs/components/date-range-picker.md +1 -1
- package/agent-docs/components/description-list.md +1 -1
- package/agent-docs/components/description.md +1 -1
- package/agent-docs/components/diff.md +1 -1
- package/agent-docs/components/disclosure-group.md +1 -1
- package/agent-docs/components/disclosure.md +1 -1
- package/agent-docs/components/dock.md +1 -1
- package/agent-docs/components/drawer.md +1 -1
- package/agent-docs/components/drop-zone.md +1 -1
- package/agent-docs/components/dropdown.md +1 -1
- package/agent-docs/components/emoji-picker.md +1 -1
- package/agent-docs/components/emoji-reaction-button.md +1 -1
- package/agent-docs/components/empty-state.md +1 -1
- package/agent-docs/components/error-message.md +1 -1
- package/agent-docs/components/fab.md +1 -1
- package/agent-docs/components/field-error.md +1 -1
- package/agent-docs/components/fieldset.md +1 -1
- package/agent-docs/components/file-input.md +1 -1
- package/agent-docs/components/file-tree.md +1 -1
- package/agent-docs/components/filter-chips.md +1 -1
- package/agent-docs/components/floating-toc.md +1 -1
- package/agent-docs/components/footer.md +1 -1
- package/agent-docs/components/form.md +1 -1
- package/agent-docs/components/hero.md +1 -1
- package/agent-docs/components/holo-card.md +1 -1
- package/agent-docs/components/hover-card.md +1 -1
- package/agent-docs/components/hover-gallery.md +1 -1
- package/agent-docs/components/indicator.md +1 -1
- package/agent-docs/components/inline-select.md +1 -1
- package/agent-docs/components/input-group.md +1 -1
- package/agent-docs/components/input-otp.md +1 -1
- package/agent-docs/components/input.md +1 -1
- package/agent-docs/components/item-card-group.md +1 -1
- package/agent-docs/components/item-card.md +1 -1
- package/agent-docs/components/join.md +1 -1
- package/agent-docs/components/joined-filter.md +1 -1
- package/agent-docs/components/kanban.md +1 -1
- package/agent-docs/components/kbd.md +1 -1
- package/agent-docs/components/kpi-group.md +1 -1
- package/agent-docs/components/kpi.md +1 -1
- package/agent-docs/components/label.md +1 -1
- package/agent-docs/components/line-chart.md +1 -1
- package/agent-docs/components/link.md +1 -1
- package/agent-docs/components/list-box.md +1 -1
- package/agent-docs/components/list-view.md +1 -1
- package/agent-docs/components/live-progress.md +1 -1
- package/agent-docs/components/map.md +1 -1
- package/agent-docs/components/markdown.md +1 -1
- package/agent-docs/components/mask.md +1 -1
- package/agent-docs/components/megamenu.md +1 -1
- package/agent-docs/components/menu-button.md +1 -1
- package/agent-docs/components/menu.md +1 -1
- package/agent-docs/components/meter.md +1 -1
- package/agent-docs/components/modal.md +1 -1
- package/agent-docs/components/native-select.md +1 -1
- package/agent-docs/components/navbar.md +1 -1
- package/agent-docs/components/number-field.md +1 -1
- package/agent-docs/components/number-stepper.md +1 -1
- package/agent-docs/components/number-value.md +1 -1
- package/agent-docs/components/page-header.md +1 -1
- package/agent-docs/components/pagination.md +1 -1
- package/agent-docs/components/phone-mockup.md +1 -1
- package/agent-docs/components/pie-chart.md +1 -1
- package/agent-docs/components/popover.md +1 -1
- package/agent-docs/components/pressable-feedback.md +1 -1
- package/agent-docs/components/progress-bar.md +1 -1
- package/agent-docs/components/progress-circle.md +1 -1
- package/agent-docs/components/prompt-input.md +1 -1
- package/agent-docs/components/prompt-suggestion.md +1 -1
- package/agent-docs/components/radar-chart.md +1 -1
- package/agent-docs/components/radial-chart.md +1 -1
- package/agent-docs/components/radio-button-group.md +1 -1
- package/agent-docs/components/radio-group.md +1 -1
- package/agent-docs/components/range-calendar.md +1 -1
- package/agent-docs/components/rating.md +1 -1
- package/agent-docs/components/resizable.md +1 -1
- package/agent-docs/components/rich-text-editor.md +1 -1
- package/agent-docs/components/scroll-shadow.md +1 -1
- package/agent-docs/components/search-field.md +1 -1
- package/agent-docs/components/segment.md +1 -1
- package/agent-docs/components/select.md +1 -1
- package/agent-docs/components/separator.md +1 -1
- package/agent-docs/components/sheet.md +1 -1
- package/agent-docs/components/sidebar.md +1 -1
- package/agent-docs/components/skeleton.md +1 -1
- package/agent-docs/components/slider.md +1 -1
- package/agent-docs/components/sortable-table.md +1 -1
- package/agent-docs/components/spinner.md +1 -1
- package/agent-docs/components/stack.md +1 -1
- package/agent-docs/components/status.md +1 -1
- package/agent-docs/components/stepper.md +1 -1
- package/agent-docs/components/surface.md +1 -1
- package/agent-docs/components/swap.md +1 -1
- package/agent-docs/components/switch.md +1 -1
- package/agent-docs/components/table.md +1 -1
- package/agent-docs/components/tabs.md +1 -1
- package/agent-docs/components/tag-group.md +1 -1
- package/agent-docs/components/text-area.md +1 -1
- package/agent-docs/components/text-field.md +1 -1
- package/agent-docs/components/text-rotate.md +1 -1
- package/agent-docs/components/text-shimmer.md +1 -1
- package/agent-docs/components/theme-controller.md +1 -1
- package/agent-docs/components/time-field.md +1 -1
- package/agent-docs/components/timeline.md +1 -1
- package/agent-docs/components/toast.md +1 -1
- package/agent-docs/components/toggle-button-group.md +1 -1
- package/agent-docs/components/toggle-button.md +1 -1
- package/agent-docs/components/toolbar.md +1 -1
- package/agent-docs/components/tooltip.md +1 -1
- package/agent-docs/components/trend-chip.md +1 -1
- package/agent-docs/components/typed-confirmation.md +1 -1
- package/agent-docs/components/typography.md +1 -1
- package/agent-docs/components/validator.md +23 -2
- package/agent-docs/components/visually-hidden.md +1 -1
- package/agent-docs/components/widget.md +1 -1
- package/agent-docs/components/window-mockup.md +1 -1
- package/agent-docs/llms-components.txt +237 -176
- package/agent-docs/llms-full.txt +363 -194
- package/agent-docs/llms-patterns.txt +127 -19
- package/agent-docs/llms.txt +1 -1
- package/bin/mero-ui.mjs +505 -0
- package/dist/manifest.json +230 -0
- package/dist/ui/mero-collections.min.css +3 -0
- package/dist/ui/mero-collections.min.js +2 -0
- package/dist/ui/mero-composition.min.css +3 -0
- package/dist/ui/mero-composition.min.js +2 -0
- package/dist/ui/mero-forms.min.js +2 -0
- package/dist/ui/mero-gallery.min.css +3 -0
- package/dist/ui/mero-gallery.min.js +2 -0
- package/dist/ui/mero-insights.min.css +3 -0
- package/dist/ui/mero-insights.min.js +2 -0
- package/dist/ui/mero-layouts.min.css +3 -0
- package/dist/ui/mero-live.min.js +2 -0
- package/dist/ui/mero-themes.min.css +3 -0
- package/dist/ui/mero-workspace.min.css +3 -0
- package/dist/ui/mero-workspace.min.js +4 -0
- package/dist/ui/mero.min.css +3 -0
- package/dist/ui/mero.min.js +2 -0
- package/package.json +27 -5
- package/ui/insight-charts.d.ts +20 -0
- package/ui/insight-charts.mjs +1 -1
- package/ui/insight-markdown.d.ts +2 -0
- package/ui/insight-markdown.mjs +2 -2
- package/ui/mero-collections.css +3 -1
- package/ui/mero-collections.js +1 -1
- package/ui/mero-composition.css +3 -1
- package/ui/mero-composition.js +2 -2
- package/ui/mero-forms.js +384 -72
- package/ui/mero-gallery.css +4 -16
- package/ui/mero-gallery.js +1 -1
- package/ui/mero-insights.css +3 -1
- package/ui/mero-insights.js +2 -2
- package/ui/mero-layouts.css +3 -1
- package/ui/mero-live.js +68 -10
- package/ui/mero-themes.css +186 -0
- package/ui/mero-workspace.css +3 -1
- package/ui/mero-workspace.js +1 -1
- package/ui/mero.css +104 -16
- package/ui/mero.d.ts +162 -0
- package/ui/mero.js +451 -62
- package/ui/mero-inventory.css +0 -182
- package/ui/mero-inventory.js +0 -56
- package/ui/studio-lamp.webp +0 -0
package/ui/mero-forms.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @rubenlangeweg/mero-ui 0.
|
|
1
|
+
/*! @rubenlangeweg/mero-ui 0.10.0 | MIT */
|
|
2
2
|
/**
|
|
3
3
|
* Optional HTMX form behavior. Server responses:
|
|
4
4
|
* 422 + replacement form for validation, or HX-Trigger:
|
|
@@ -11,9 +11,40 @@
|
|
|
11
11
|
* data-mero-action-expect turns a success without the expected content into unknown.
|
|
12
12
|
*/
|
|
13
13
|
(() => {
|
|
14
|
+
const lifecycle = (globalThis[Symbol.for("mero.lifecycle")] ??= { init: new Set(), cleanup: new Set(), defaults: {}, config: { locale: null, dialogBackdrop: "all", messages: {} } });
|
|
15
|
+
const config = (lifecycle.config ??= { locale: null, dialogBackdrop: "all", messages: {} });
|
|
16
|
+
const defaults = (lifecycle.defaults ??= {});
|
|
17
|
+
const defaultMessages = {
|
|
18
|
+
confirmTypePhrase: "Type the phrase exactly to enable confirmation.",
|
|
19
|
+
confirmEnterReason: "Phrase matches. Enter a reason to enable confirmation.",
|
|
20
|
+
confirmReady: "Ready to confirm.",
|
|
21
|
+
confirmUnknown: "Outcome unknown. Check the current state before retrying.",
|
|
22
|
+
confirmRejected: "The server rejected this action.",
|
|
23
|
+
formUnreachable: "The server could not be reached. Check whether the change was saved before retrying.",
|
|
24
|
+
formFailed: "The request could not be completed. Check the current state and try again.",
|
|
25
|
+
actionSucceeded: "Done.",
|
|
26
|
+
actionRejected: "The server refused this action. Nothing changed.",
|
|
27
|
+
actionUnknown: "The result is unknown. Check the current state before you try again.",
|
|
28
|
+
searchError: "Couldn't update the results. The list still shows the previous results.",
|
|
29
|
+
holdWarning: "Copy what is shown before you leave this page. It cannot be shown again.",
|
|
30
|
+
dirtyLeaveWarning: "You have unsaved changes. Leave without saving?",
|
|
31
|
+
};
|
|
32
|
+
Object.assign(defaults, defaultMessages);
|
|
33
|
+
const msg = key => config.messages[key] ?? defaultMessages[key] ?? "";
|
|
14
34
|
const formSelector = "form[data-mero-form]";
|
|
15
35
|
const confirmSelector = "form[data-mero-form][data-mero-confirm]";
|
|
16
36
|
const requesting = new WeakMap();
|
|
37
|
+
const lockedControls = new WeakSet();
|
|
38
|
+
const isLocked = el => {
|
|
39
|
+
for (let node = el; node; node = node.parentElement) if (lockedControls.has(node)) return true;
|
|
40
|
+
return false;
|
|
41
|
+
};
|
|
42
|
+
document.addEventListener("click", event => {
|
|
43
|
+
const label = event.target.closest?.("label");
|
|
44
|
+
if (label?.control && isLocked(label.control) && !event.target.closest?.("a[href], button")) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
}
|
|
47
|
+
}, true);
|
|
17
48
|
const formRequests = new WeakMap();
|
|
18
49
|
// A swap can detach the button before finally:request, so the request remembers its button and status.
|
|
19
50
|
const actionRequests = new WeakMap();
|
|
@@ -73,8 +104,8 @@
|
|
|
73
104
|
button.disabled = !ready || requesting.has(form);
|
|
74
105
|
}
|
|
75
106
|
const hint = form.querySelector("[data-mero-confirm-hint]");
|
|
76
|
-
const message = !matches ? "
|
|
77
|
-
: !ready ? "
|
|
107
|
+
const message = !matches ? msg("confirmTypePhrase")
|
|
108
|
+
: !ready ? msg("confirmEnterReason") : msg("confirmReady");
|
|
78
109
|
if (hint && hint.textContent !== message) hint.textContent = message;
|
|
79
110
|
return ready;
|
|
80
111
|
}
|
|
@@ -86,16 +117,38 @@
|
|
|
86
117
|
document.addEventListener("htmx:after:init", event => {
|
|
87
118
|
if (event.target.matches?.(confirmSelector)) checkConfirmation(event.target);
|
|
88
119
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
120
|
+
function clearFormError(form) {
|
|
121
|
+
for (const error of form.querySelectorAll("[data-mero-form-error]")) {
|
|
122
|
+
error.textContent = "";
|
|
123
|
+
error.hidden = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function clearDialogForms(dialog) {
|
|
127
|
+
if (!(dialog instanceof Element)) return;
|
|
128
|
+
for (const form of dialog.querySelectorAll(formSelector)) clearFormError(form);
|
|
129
|
+
for (const form of dialog.querySelectorAll(confirmSelector)) {
|
|
95
130
|
const input = form.querySelector("[data-mero-confirm-input]");
|
|
96
131
|
if (input) input.value = "";
|
|
97
132
|
checkConfirmation(form);
|
|
98
133
|
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
document.addEventListener("reset", event => {
|
|
137
|
+
const form = event.target;
|
|
138
|
+
if (!(form instanceof HTMLFormElement)) return;
|
|
139
|
+
// Wait for a task so bubbling window reset listeners can cancel before cleanup runs.
|
|
140
|
+
setTimeout(() => {
|
|
141
|
+
if (event.defaultPrevented || !form.isConnected) return;
|
|
142
|
+
if (form.matches(formSelector)) clearFormError(form);
|
|
143
|
+
if (form.matches(confirmSelector)) checkConfirmation(form);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
document.addEventListener("mero:dialog", event => {
|
|
147
|
+
if (event.detail?.open === false) clearDialogForms(event.detail.dialog);
|
|
148
|
+
});
|
|
149
|
+
document.addEventListener("close", event => {
|
|
150
|
+
if (!event.target.matches?.("dialog") || event.target.open) return;
|
|
151
|
+
clearDialogForms(event.target);
|
|
99
152
|
}, true);
|
|
100
153
|
|
|
101
154
|
// Guard implicit and scripted submissions too, before htmx can queue a duplicate.
|
|
@@ -104,6 +157,8 @@
|
|
|
104
157
|
if (form?.matches(confirmSelector) && (!checkConfirmation(form) || requesting.has(form))) event.preventDefault();
|
|
105
158
|
});
|
|
106
159
|
|
|
160
|
+
const lockedWidgets = "[data-mero-combo], [data-mero-multiselect], [data-mero-listbox], [data-mero-ws-number], [data-mero-ws-emoji], [data-mero-ws-editor] [data-format], [contenteditable]:not([contenteditable='false'])";
|
|
161
|
+
|
|
107
162
|
document.addEventListener("htmx:before:request", (event) => {
|
|
108
163
|
const form = requestForm(event);
|
|
109
164
|
if (!form) return;
|
|
@@ -113,17 +168,30 @@
|
|
|
113
168
|
}
|
|
114
169
|
const { ctx } = event.detail;
|
|
115
170
|
formRequests.set(ctx, form);
|
|
171
|
+
if (form.matches("[data-mero-dirty]")) submittedSnapshots.set(form, snapshotForm(form));
|
|
116
172
|
const buttons = [...form.elements].filter(control => !control.disabled && (control.type === "submit" || control.type === "image"));
|
|
173
|
+
const keepEditable = (form.getAttribute("data-mero-form") || "").split(/\s+/).includes("keep-editable");
|
|
174
|
+
const widgetNodes = keepEditable ? [] : [...form.querySelectorAll(lockedWidgets)].filter(node => {
|
|
175
|
+
const outer = node.parentElement?.closest?.(lockedWidgets);
|
|
176
|
+
return !node.closest?.("[inert]") && (!outer || outer === form || !form.contains(outer));
|
|
177
|
+
});
|
|
178
|
+
const inertNodes = keepEditable ? [] : [
|
|
179
|
+
...[...form.elements].filter(control => !control.closest?.("[inert]") && !widgetNodes.some(node => node.contains(control)) &&
|
|
180
|
+
control.matches("input, select, textarea, [type=reset]") && !control.matches("[type=submit], [type=button], [type=image], [type=hidden]")),
|
|
181
|
+
...widgetNodes,
|
|
182
|
+
];
|
|
183
|
+
const activeEl = document.activeElement;
|
|
184
|
+
const displacedFocus = activeEl && inertNodes.some(node => node === activeEl || node.contains(activeEl)) ? activeEl : null;
|
|
117
185
|
const submitter = ctx.request.submitter?.form === form ? ctx.request.submitter : null;
|
|
118
|
-
requesting.set(form, { buttons, ctx, owners: [submitter, form] });
|
|
186
|
+
requesting.set(form, { buttons, inertNodes, displacedFocus, ctx, owners: [submitter, form] });
|
|
119
187
|
form.setAttribute("aria-busy", "true");
|
|
120
188
|
buttons.forEach((button) => { button.disabled = true; });
|
|
121
|
-
|
|
122
|
-
|
|
189
|
+
inertNodes.forEach((node) => { node.inert = true; lockedControls.add(node); });
|
|
190
|
+
clearFormError(form);
|
|
123
191
|
const panel = unknownPanel([submitter, form]);
|
|
124
192
|
if (panel) panel.hidden = true;
|
|
125
193
|
(submitter ?? form).dataset.meroActionState = "pending";
|
|
126
|
-
form.dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state: "pending" } }));
|
|
194
|
+
form.dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state: "pending", source: form, id: form.id || null } }));
|
|
127
195
|
});
|
|
128
196
|
|
|
129
197
|
// Decide before htmx applies HX-Trigger: a response that is not a confirmed
|
|
@@ -153,16 +221,17 @@
|
|
|
153
221
|
}
|
|
154
222
|
const status = ctx.response?.status;
|
|
155
223
|
if (!(status >= 400) || !requestForm(event)) return;
|
|
156
|
-
// htmx 4 swaps every error response. Only
|
|
157
|
-
if (status !== 422) event.preventDefault();
|
|
224
|
+
// htmx 4 swaps every error response. Only 422, and 409 when opted in, carry a replacement form.
|
|
225
|
+
if (status !== 422 && !(status === 409 && form?.hasAttribute("data-mero-conflict-swap"))) event.preventDefault();
|
|
158
226
|
});
|
|
159
227
|
|
|
160
228
|
// after:swap, not after:settle: htmx 4 applies [autofocus] in the new form after settling.
|
|
161
229
|
// It fires on the source, which htmx points at the new form when the swap replaced it.
|
|
162
230
|
document.addEventListener("htmx:after:swap", (event) => {
|
|
163
|
-
|
|
231
|
+
const status = event.detail.ctx.response?.status;
|
|
164
232
|
const form = event.target.closest?.(formSelector);
|
|
165
|
-
|
|
233
|
+
if (status !== 422 && !(status === 409 && form?.hasAttribute("data-mero-conflict-swap"))) return;
|
|
234
|
+
const field = (status === 409 && form?.querySelector("[data-mero-conflict-focus]")) || form?.querySelector('[aria-invalid="true"]');
|
|
166
235
|
field?.focus();
|
|
167
236
|
});
|
|
168
237
|
|
|
@@ -179,11 +248,17 @@
|
|
|
179
248
|
const form = formRequests.get(ctx);
|
|
180
249
|
// A cancelled duplicate has no entry, so it never releases the request still in flight.
|
|
181
250
|
if (!form) return;
|
|
182
|
-
const
|
|
251
|
+
const active = requesting.get(form);
|
|
252
|
+
const owners = active?.owners ?? [form];
|
|
183
253
|
const holder = owners.find(Boolean);
|
|
184
254
|
form.removeAttribute("aria-busy");
|
|
185
|
-
|
|
255
|
+
active?.buttons.forEach((button) => { button.disabled = false; });
|
|
256
|
+
active?.inertNodes.forEach((node) => { node.inert = false; lockedControls.delete(node); });
|
|
186
257
|
requesting.delete(form);
|
|
258
|
+
const focusAfter = document.activeElement;
|
|
259
|
+
if (form.isConnected && active?.displacedFocus?.isConnected && (!focusAfter || focusAfter === document.body || focusAfter === form.closest("dialog"))) {
|
|
260
|
+
active.displacedFocus.focus({ preventScroll: true });
|
|
261
|
+
}
|
|
187
262
|
const typed = form.matches(confirmSelector);
|
|
188
263
|
if (typed) checkConfirmation(form);
|
|
189
264
|
if (cancelled(ctx)) {
|
|
@@ -194,7 +269,10 @@
|
|
|
194
269
|
const live = form.isConnected ? form : ctx.sourceElement?.matches?.(formSelector) && ctx.sourceElement.isConnected ? ctx.sourceElement : null;
|
|
195
270
|
if (form.hasAttribute("data-mero-clear-passwords")) {
|
|
196
271
|
for (const node of new Set([form, live])) {
|
|
197
|
-
node?.querySelectorAll('input[type="password"]').forEach(input => {
|
|
272
|
+
node?.querySelectorAll('input[type="password"]').forEach(input => {
|
|
273
|
+
input.value = "";
|
|
274
|
+
input.defaultValue = "";
|
|
275
|
+
});
|
|
198
276
|
}
|
|
199
277
|
}
|
|
200
278
|
const state = outcomeOf(ctx, owners);
|
|
@@ -205,8 +283,8 @@
|
|
|
205
283
|
let message = headerMessage;
|
|
206
284
|
if (state === "succeeded") {
|
|
207
285
|
if (message) window.MeroUI?.toast(message, { tone: "success", trigger: live ?? undefined });
|
|
208
|
-
} else if (state === "rejected" && status === 422 && !explicit) {
|
|
209
|
-
// The replacement form shows its own field
|
|
286
|
+
} else if (state === "rejected" && (status === 422 || (status === 409 && form.hasAttribute("data-mero-conflict-swap"))) && !explicit) {
|
|
287
|
+
// The replacement form shows its own field or conflict details.
|
|
210
288
|
} else {
|
|
211
289
|
if (state === "unknown") {
|
|
212
290
|
message ||= unknownText(owners);
|
|
@@ -217,14 +295,14 @@
|
|
|
217
295
|
if (!message) {
|
|
218
296
|
const signal = ctx.request.signal;
|
|
219
297
|
message = typed
|
|
220
|
-
? (state === "unknown" ? "
|
|
298
|
+
? msg(state === "unknown" ? "confirmUnknown" : "confirmRejected")
|
|
221
299
|
: !ctx.response && (signal?.reason === "timeout" || !signal?.aborted)
|
|
222
|
-
? "
|
|
223
|
-
: "
|
|
300
|
+
? msg("formUnreachable")
|
|
301
|
+
: msg("formFailed");
|
|
224
302
|
}
|
|
225
303
|
if (live) showError(live, message);
|
|
226
304
|
}
|
|
227
|
-
(live ?? document).dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state, status, message } }));
|
|
305
|
+
(live ?? document).dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state, status, message, source: form, id: form.id || null } }));
|
|
228
306
|
});
|
|
229
307
|
|
|
230
308
|
document.addEventListener("mero:form-saved", (event) => {
|
|
@@ -244,15 +322,23 @@
|
|
|
244
322
|
window.MeroUI.close(dialog, { focus: parent ? null : focus });
|
|
245
323
|
dialog = parent;
|
|
246
324
|
}
|
|
325
|
+
if (form.isConnected && form.matches("[data-mero-dirty]")) {
|
|
326
|
+
if (form.getAttribute("data-mero-dirty") === "dirty") form.setAttribute("data-mero-dirty", "");
|
|
327
|
+
const submitted = submittedSnapshots.get(form) ?? snapshotForm(form);
|
|
328
|
+
submittedSnapshots.delete(form);
|
|
329
|
+
formBaselines.set(form, submitted);
|
|
330
|
+
syncSavedDefaults(form, submitted);
|
|
331
|
+
updateForm(form);
|
|
332
|
+
}
|
|
247
333
|
focus?.focus({ preventScroll: true });
|
|
248
334
|
if (event.detail.message) window.MeroUI.toast(event.detail.message, { tone: "success" });
|
|
249
335
|
});
|
|
250
336
|
|
|
251
337
|
const actionOf = event => event.target.closest?.("[data-mero-action]");
|
|
252
|
-
const
|
|
253
|
-
succeeded: ["
|
|
254
|
-
rejected: ["
|
|
255
|
-
unknown: ["
|
|
338
|
+
const actionTones = {
|
|
339
|
+
succeeded: ["actionSucceeded", "success"],
|
|
340
|
+
rejected: ["actionRejected", "warning"],
|
|
341
|
+
unknown: ["actionUnknown", "danger"],
|
|
256
342
|
};
|
|
257
343
|
const actionStatus = action => action.parentElement?.querySelector("[data-mero-action-status]");
|
|
258
344
|
|
|
@@ -275,7 +361,7 @@
|
|
|
275
361
|
if (output) { output.textContent = ""; delete output.dataset.tone; }
|
|
276
362
|
const panel = unknownPanel([action]);
|
|
277
363
|
if (panel) panel.hidden = true;
|
|
278
|
-
action.dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state: "pending" } }));
|
|
364
|
+
action.dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state: "pending", source: action, id: action.id || null } }));
|
|
279
365
|
});
|
|
280
366
|
|
|
281
367
|
// htmx fires finally:request after load, error, abort and timeout alike.
|
|
@@ -294,9 +380,9 @@
|
|
|
294
380
|
}
|
|
295
381
|
const status = ctx.response?.status ?? 0;
|
|
296
382
|
const state = outcomeOf(ctx, [action]);
|
|
297
|
-
const [
|
|
383
|
+
const [fallbackKey, tone] = actionTones[state];
|
|
298
384
|
const headerMessage = state === serverOutcome(ctx) && ctx.response?.headers.get("Mero-Action-Message");
|
|
299
|
-
const message = headerMessage || (state === "unknown" && unknownText([action])) ||
|
|
385
|
+
const message = headerMessage || (state === "unknown" && unknownText([action])) || msg(fallbackKey);
|
|
300
386
|
if (state === "unknown") {
|
|
301
387
|
const panel = unknownPanel([action]);
|
|
302
388
|
if (panel) panel.hidden = false;
|
|
@@ -312,66 +398,262 @@
|
|
|
312
398
|
} else {
|
|
313
399
|
window.MeroUI?.toast(message, { tone, trigger: action.isConnected ? action : event.target });
|
|
314
400
|
}
|
|
315
|
-
(action.isConnected ? action : document).dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state, status, message } }));
|
|
401
|
+
(action.isConnected ? action : document).dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state, status, message, source: action, id: action.id || null } }));
|
|
316
402
|
});
|
|
317
403
|
// Form state helpers. [data-mero-show-when="field=value other"] shows a branch only while the form's
|
|
318
404
|
// field has one of the values, and disables its controls while hidden so they neither validate nor submit.
|
|
319
|
-
// form[data-mero-dirty] keeps its submit buttons disabled until a control differs from the
|
|
405
|
+
// form[data-mero-dirty] keeps its submit buttons disabled until a control differs from the acknowledged baseline.
|
|
320
406
|
const disabledByBranch = new WeakSet();
|
|
321
|
-
|
|
322
|
-
|
|
407
|
+
const dirtyStates = new WeakMap();
|
|
408
|
+
const formBaselines = new WeakMap();
|
|
409
|
+
const submittedSnapshots = new WeakMap();
|
|
410
|
+
const submittingNative = new WeakSet();
|
|
411
|
+
const multiSnapshot = multi => [...multi.querySelectorAll('[data-mero-multiselect-chips] input[type="hidden"]')].map(i => i.value).join("\0");
|
|
412
|
+
const selectSnapshot = select => [...select.selectedOptions].map(option => option.value).join("\0");
|
|
413
|
+
const isTrackableField = control => control.matches("input, select, textarea") && !control.matches("[type=submit], [type=button], [type=reset], [type=image]");
|
|
414
|
+
|
|
415
|
+
function syncSavedDefaults(form, snapshot) {
|
|
416
|
+
const clearPwd = form.hasAttribute("data-mero-clear-passwords");
|
|
417
|
+
for (const [control, saved] of snapshot.fields) {
|
|
418
|
+
if (!form.contains(control)) continue;
|
|
419
|
+
if (clearPwd && control.matches("input[type=password]")) continue;
|
|
420
|
+
if (control.matches("input[type=checkbox], input[type=radio]")) {
|
|
421
|
+
control.defaultChecked = Boolean(saved);
|
|
422
|
+
} else if (control.matches("select")) {
|
|
423
|
+
const remaining = saved === "" ? [] : String(saved).split("\0");
|
|
424
|
+
for (const option of control.options) {
|
|
425
|
+
const idx = remaining.indexOf(option.value);
|
|
426
|
+
option.defaultSelected = idx !== -1;
|
|
427
|
+
if (idx !== -1) remaining.splice(idx, 1);
|
|
428
|
+
}
|
|
429
|
+
} else if (control.matches("input, textarea")) {
|
|
430
|
+
// Hidden inputs share value and defaultValue; skip if a mid-flight edit changed value.
|
|
431
|
+
if (control.type === "hidden" && control.value !== String(saved)) continue;
|
|
432
|
+
control.defaultValue = String(saved);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
for (const [multi, chips] of snapshot.multiChips ?? []) {
|
|
436
|
+
if (form.contains(multi)) lifecycle.setMultiBaseline?.(multi, chips);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function snapshotForm(form) {
|
|
441
|
+
const clearPwd = form.hasAttribute("data-mero-clear-passwords");
|
|
442
|
+
const fields = new Map();
|
|
443
|
+
for (const control of form.elements) {
|
|
444
|
+
if (control.closest?.("[data-mero-multiselect]") || !isTrackableField(control)) continue;
|
|
445
|
+
if (clearPwd && control.matches("input[type=password]")) continue;
|
|
446
|
+
if (control.matches("input[type=checkbox], input[type=radio]")) fields.set(control, control.checked);
|
|
447
|
+
else if (control.matches("select")) fields.set(control, selectSnapshot(control));
|
|
448
|
+
else fields.set(control, control.value);
|
|
449
|
+
}
|
|
450
|
+
const multis = new Map();
|
|
451
|
+
const multiChips = new Map();
|
|
452
|
+
for (const multi of form.querySelectorAll("[data-mero-multiselect]")) {
|
|
453
|
+
multis.set(multi, multiSnapshot(multi));
|
|
454
|
+
const chips = lifecycle.captureMultiChips?.(multi);
|
|
455
|
+
if (chips) multiChips.set(multi, chips);
|
|
456
|
+
}
|
|
457
|
+
return { fields, multis, multiChips };
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function indexFormControls(form, names) {
|
|
461
|
+
const byName = new Map();
|
|
323
462
|
for (const control of form.elements) {
|
|
324
|
-
if (control.name
|
|
325
|
-
|
|
326
|
-
|
|
463
|
+
if (!names.has(control.name)) continue;
|
|
464
|
+
let list = byName.get(control.name);
|
|
465
|
+
if (!list) byName.set(control.name, list = []);
|
|
466
|
+
list.push(control);
|
|
467
|
+
}
|
|
468
|
+
return byName;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function valuesFor(controls) {
|
|
472
|
+
const values = [];
|
|
473
|
+
for (const control of controls ?? []) {
|
|
474
|
+
if (control.disabled || control.matches(":disabled")) continue;
|
|
475
|
+
if (control.type === "checkbox" || control.type === "radio") { if (control.checked) values.push(control.value); }
|
|
476
|
+
else if (control.tagName === "SELECT") { for (const option of control.selectedOptions) values.push(option.value); }
|
|
327
477
|
else values.push(control.value);
|
|
328
478
|
}
|
|
329
479
|
return values;
|
|
330
480
|
}
|
|
481
|
+
|
|
331
482
|
function changed(form) {
|
|
483
|
+
if (form.getAttribute("data-mero-dirty") === "dirty") return true;
|
|
484
|
+
let baseline = formBaselines.get(form);
|
|
485
|
+
if (!baseline) {
|
|
486
|
+
baseline = snapshotForm(form);
|
|
487
|
+
formBaselines.set(form, baseline);
|
|
488
|
+
}
|
|
489
|
+
for (const multi of form.querySelectorAll("[data-mero-multiselect]")) {
|
|
490
|
+
if (!baseline.multis.has(multi)) baseline.multis.set(multi, multiSnapshot(multi));
|
|
491
|
+
if (multiSnapshot(multi) !== baseline.multis.get(multi)) return true;
|
|
492
|
+
}
|
|
493
|
+
const clearPwd = form.hasAttribute("data-mero-clear-passwords");
|
|
332
494
|
return [...form.elements].some(control => {
|
|
495
|
+
if (control.closest?.("[data-mero-multiselect]") || !isTrackableField(control)) return false;
|
|
496
|
+
if (clearPwd && control.matches("input[type=password]")) return control.value !== "";
|
|
497
|
+
if (baseline.fields.has(control)) {
|
|
498
|
+
if (control.matches("input[type=checkbox], input[type=radio]")) return control.checked !== baseline.fields.get(control);
|
|
499
|
+
if (control.matches("select")) return selectSnapshot(control) !== baseline.fields.get(control);
|
|
500
|
+
return control.value !== baseline.fields.get(control);
|
|
501
|
+
}
|
|
333
502
|
if (control.matches("input[type=checkbox], input[type=radio]")) return control.checked !== control.defaultChecked;
|
|
334
503
|
if (control.matches("select")) return [...control.options].some(option => option.selected !== option.defaultSelected);
|
|
335
|
-
return control.
|
|
504
|
+
return control.value !== control.defaultValue;
|
|
336
505
|
});
|
|
337
506
|
}
|
|
338
|
-
|
|
507
|
+
|
|
508
|
+
function updateShowWhen(form, changedName = null) {
|
|
509
|
+
const branches = form.querySelectorAll("[data-mero-show-when]");
|
|
510
|
+
if (!branches.length) return;
|
|
511
|
+
const names = new Set();
|
|
512
|
+
const specs = [...branches].map(branch => {
|
|
513
|
+
const expr = branch.getAttribute("data-mero-show-when") || "";
|
|
514
|
+
const negated = expr.includes("!=");
|
|
515
|
+
const [raw, wanted = ""] = expr.split(negated ? "!=" : "=");
|
|
516
|
+
const name = raw.trim();
|
|
517
|
+
const trimmed = wanted.trim();
|
|
518
|
+
names.add(name);
|
|
519
|
+
return [branch, name, negated, trimmed === "" ? [""] : trimmed.split(/\s+/)];
|
|
520
|
+
});
|
|
521
|
+
if (changedName !== null && !names.has(changedName)) return;
|
|
522
|
+
const byName = indexFormControls(form, names);
|
|
523
|
+
const cache = new Map();
|
|
339
524
|
// Outer branches first, so a nested branch reads the state its parent left.
|
|
340
|
-
for (const branch of
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
525
|
+
for (const [branch, name, negated, targets] of specs) {
|
|
526
|
+
let values = cache.get(name);
|
|
527
|
+
if (!values) cache.set(name, values = valuesFor(byName.get(name)));
|
|
528
|
+
const matched = targets.some(value => values.includes(value));
|
|
529
|
+
const show = (negated ? !matched : matched) && !branch.parentElement.closest("[data-mero-show-when][hidden]");
|
|
344
530
|
branch.hidden = !show;
|
|
345
|
-
if (branch.
|
|
346
|
-
|
|
347
|
-
if (
|
|
348
|
-
|
|
531
|
+
if (branch.hasAttribute("data-mero-show-when-keep-enabled")) continue;
|
|
532
|
+
if (branch.tagName === "FIELDSET") {
|
|
533
|
+
if (branch.disabled !== !show) {
|
|
534
|
+
branch.disabled = !show;
|
|
535
|
+
for (const control of branch.elements) if (names.has(control.name)) cache.delete(control.name);
|
|
536
|
+
}
|
|
537
|
+
} else {
|
|
538
|
+
for (const control of branch.querySelectorAll("input, select, textarea, button")) {
|
|
539
|
+
if (!show && !control.disabled) { control.disabled = true; disabledByBranch.add(control); if (names.has(control.name)) cache.delete(control.name); }
|
|
540
|
+
else if (show && disabledByBranch.delete(control)) { control.disabled = false; if (names.has(control.name)) cache.delete(control.name); }
|
|
541
|
+
}
|
|
349
542
|
}
|
|
350
543
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function updateForm(form, changedName = null) {
|
|
547
|
+
updateShowWhen(form, changedName);
|
|
548
|
+
if (form.matches("[data-mero-dirty]")) {
|
|
549
|
+
const dirty = changed(form);
|
|
550
|
+
if (!requesting.has(form)) {
|
|
551
|
+
for (const button of form.querySelectorAll("button:not([type]), [type=submit], [type=image]")) {
|
|
552
|
+
if (!button.closest("[data-mero-show-when][hidden]")) button.disabled = !dirty;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
const previous = dirtyStates.get(form);
|
|
556
|
+
dirtyStates.set(form, dirty);
|
|
557
|
+
if (previous !== undefined && previous !== dirty) {
|
|
558
|
+
form.dispatchEvent(new CustomEvent("mero:dirty", { bubbles: true, detail: { dirty, form } }));
|
|
355
559
|
}
|
|
356
560
|
}
|
|
357
561
|
}
|
|
358
|
-
|
|
359
|
-
|
|
562
|
+
|
|
563
|
+
function dirtyGuardedForms() {
|
|
564
|
+
return [...document.querySelectorAll("form[data-mero-dirty][data-mero-dirty-guard]")]
|
|
565
|
+
.filter(form => form.isConnected && !requesting.has(form) && !submittingNative.has(form) && changed(form));
|
|
566
|
+
}
|
|
567
|
+
const dirtyGuarded = () => dirtyGuardedForms()[0] ?? null;
|
|
568
|
+
|
|
569
|
+
let dirtyHistoryDecision = null;
|
|
570
|
+
function setDirtyHistoryDecision(value) {
|
|
571
|
+
dirtyHistoryDecision = value;
|
|
572
|
+
setTimeout(() => { if (dirtyHistoryDecision === value) dirtyHistoryDecision = null; }, 50);
|
|
573
|
+
}
|
|
574
|
+
function confirmLeaveDirty(guarded) {
|
|
575
|
+
return window.confirm(guarded.getAttribute("data-mero-dirty-guard") || msg("dirtyLeaveWarning"));
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
document.addEventListener("submit", event => {
|
|
579
|
+
if (event.defaultPrevented || !(event.target instanceof HTMLFormElement)) return;
|
|
580
|
+
const target = (event.submitter?.getAttribute("formtarget") || event.target.getAttribute("target") || "").trim().toLowerCase();
|
|
581
|
+
// Only exempt submissions that navigate this document (not iframe or _blank targets).
|
|
582
|
+
if (target && target !== "_self" && target !== "_top" && target !== "_parent") return;
|
|
583
|
+
submittingNative.add(event.target);
|
|
584
|
+
setTimeout(() => submittingNative.delete(event.target), 2000);
|
|
585
|
+
});
|
|
586
|
+
addEventListener("pageshow", () => {
|
|
587
|
+
for (const form of document.querySelectorAll("form[data-mero-dirty-guard]")) submittingNative.delete(form);
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
document.addEventListener("htmx:config:request", event => {
|
|
591
|
+
const { ctx } = event.detail;
|
|
592
|
+
if (event.defaultPrevented || ctx.request.method.toUpperCase() !== "GET") return;
|
|
593
|
+
const candidates = dirtyGuardedForms();
|
|
594
|
+
if (!candidates.length) return;
|
|
595
|
+
const target = typeof ctx.target === "string" ? document.querySelector(ctx.target) : ctx.target;
|
|
596
|
+
const isHistory = Boolean(ctx.request.headers["HX-History-Restore-Request"]);
|
|
597
|
+
const fullLeave = Boolean(ctx.request.headers["HX-Boosted"] || isHistory || target === document.body || target === document.documentElement);
|
|
598
|
+
const guarded = fullLeave ? candidates[0] : candidates.find(form => target?.contains(form));
|
|
599
|
+
if (!guarded) return;
|
|
600
|
+
if (isHistory && dirtyHistoryDecision === "allow") return;
|
|
601
|
+
if (isHistory && dirtyHistoryDecision === "block") { event.preventDefault(); return; }
|
|
602
|
+
if (!confirmLeaveDirty(guarded)) {
|
|
603
|
+
event.preventDefault();
|
|
604
|
+
if (isHistory) {
|
|
605
|
+
setDirtyHistoryDecision("block");
|
|
606
|
+
if (location.href !== steady.url) restoreSteady();
|
|
607
|
+
}
|
|
608
|
+
} else if (isHistory) {
|
|
609
|
+
setDirtyHistoryDecision("allow");
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
addEventListener("beforeunload", event => {
|
|
613
|
+
if (!dirtyGuarded()) return;
|
|
614
|
+
event.preventDefault();
|
|
615
|
+
event.returnValue = "";
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
const stateForms = root => {
|
|
619
|
+
const found = new Set();
|
|
620
|
+
const ancestor = root instanceof Element ? root.closest("form") : null;
|
|
621
|
+
if (ancestor) found.add(ancestor);
|
|
622
|
+
if (root.matches?.("form")) found.add(root);
|
|
623
|
+
for (const form of root.querySelectorAll?.("form") ?? []) found.add(form);
|
|
624
|
+
return [...found].filter(form => form.matches("[data-mero-dirty]") || form.querySelector("[data-mero-show-when]"));
|
|
625
|
+
};
|
|
626
|
+
function initForms(root = document) {
|
|
627
|
+
if (!(root instanceof Element || root instanceof Document)) return;
|
|
628
|
+
stateForms(root).forEach(form => updateForm(form));
|
|
629
|
+
const confirms = new Set();
|
|
630
|
+
const ancestor = root instanceof Element ? root.closest(confirmSelector) : null;
|
|
631
|
+
if (ancestor) confirms.add(ancestor);
|
|
632
|
+
if (root.matches?.(confirmSelector)) confirms.add(root);
|
|
633
|
+
for (const form of root.querySelectorAll?.(confirmSelector) ?? []) confirms.add(form);
|
|
634
|
+
confirms.forEach(checkConfirmation);
|
|
635
|
+
}
|
|
636
|
+
lifecycle.init.add(initForms);
|
|
360
637
|
for (const type of ["input", "change"]) document.addEventListener(type, event => {
|
|
361
638
|
const form = event.target.form ?? event.target.closest?.("form");
|
|
362
|
-
if (form
|
|
639
|
+
if (!form || !(form.matches("[data-mero-dirty]") || form.querySelector("[data-mero-show-when]"))) return;
|
|
640
|
+
const multi = event.target.closest?.("[data-mero-multiselect]");
|
|
641
|
+
const changedName = multi
|
|
642
|
+
? (multi.getAttribute("data-mero-multiselect-name") || null)
|
|
643
|
+
: (typeof event.target.name === "string" && event.target.name !== "" ? event.target.name : null);
|
|
644
|
+
updateForm(form, changedName);
|
|
363
645
|
});
|
|
364
646
|
document.addEventListener("reset", event => {
|
|
365
647
|
if (stateForms(event.target).length) setTimeout(() => updateForm(event.target));
|
|
366
648
|
});
|
|
367
649
|
// Swapped-in forms, and forms released after a request, which re-enables their submit buttons.
|
|
368
|
-
document.addEventListener("htmx:after:settle", event =>
|
|
650
|
+
document.addEventListener("htmx:after:settle", event => initForms(event.target));
|
|
369
651
|
document.addEventListener("htmx:finally:request", event => {
|
|
370
652
|
const form = event.detail.ctx.sourceElement?.closest?.("form");
|
|
371
653
|
if (form && stateForms(form).length) queueMicrotask(() => updateForm(form));
|
|
372
654
|
});
|
|
373
|
-
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () =>
|
|
374
|
-
else
|
|
655
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => initForms(document), { once: true });
|
|
656
|
+
else initForms(document);
|
|
375
657
|
// data-mero-action-reread="#region": after an unknown outcome, read the current page again with a GET
|
|
376
658
|
// and morph that region, so the person sees what actually happened. Mero never re-sends the action.
|
|
377
659
|
// The answer is dropped when the page moved on, the region is gone or a newer read started.
|
|
@@ -413,7 +695,7 @@
|
|
|
413
695
|
|
|
414
696
|
document.addEventListener("htmx:before:swap", event => {
|
|
415
697
|
const { ctx, tasks } = event.detail;
|
|
416
|
-
const id = ctx.request
|
|
698
|
+
const id = ctx.request?.headers?.["Mero-Reread"];
|
|
417
699
|
if (!id) return;
|
|
418
700
|
const target = typeof ctx.target === "string" ? document.querySelector(ctx.target) : ctx.target;
|
|
419
701
|
const url = new URL(ctx.request.action, location.href);
|
|
@@ -489,7 +771,7 @@
|
|
|
489
771
|
if (failed && error) {
|
|
490
772
|
error.textContent = "";
|
|
491
773
|
error.textContent = error.getAttribute("data-mero-search-error") || ctx.response?.headers.get("Mero-Form-Error") ||
|
|
492
|
-
"
|
|
774
|
+
msg("searchError");
|
|
493
775
|
error.hidden = false;
|
|
494
776
|
}
|
|
495
777
|
if (pending.size) return;
|
|
@@ -508,16 +790,16 @@
|
|
|
508
790
|
const pendingHolds = new Map();
|
|
509
791
|
let steady = { url: location.href, key: window.navigation?.currentEntry?.key };
|
|
510
792
|
const remember = () => { steady = { url: location.href, key: window.navigation?.currentEntry?.key }; };
|
|
511
|
-
const holdFallback =
|
|
793
|
+
const holdFallback = () => msg("holdWarning");
|
|
512
794
|
const shown = node => node.isConnected && (node.childElementCount > 0 || node.textContent.trim() !== "");
|
|
513
795
|
const heldEphemeral = () => [...document.querySelectorAll("[data-mero-ephemeral][data-mero-hold]")].filter(shown);
|
|
514
796
|
|
|
515
797
|
function hold() {
|
|
516
798
|
for (const [ctx, owner] of pendingHolds) {
|
|
517
|
-
return { elements: [owner, ...heldEphemeral()], message: owner.getAttribute("data-mero-hold") || holdFallback, ctx };
|
|
799
|
+
return { elements: [owner, ...heldEphemeral()], message: owner.getAttribute("data-mero-hold") || holdFallback(), ctx };
|
|
518
800
|
}
|
|
519
801
|
const elements = heldEphemeral();
|
|
520
|
-
return elements.length ? { elements, message: elements[0].getAttribute("data-mero-hold") || holdFallback } : null;
|
|
802
|
+
return elements.length ? { elements, message: elements[0].getAttribute("data-mero-hold") || holdFallback() } : null;
|
|
521
803
|
}
|
|
522
804
|
|
|
523
805
|
function warnHeld(message) {
|
|
@@ -566,18 +848,48 @@
|
|
|
566
848
|
window.navigation?.addEventListener("currententrychange", event => {
|
|
567
849
|
if (event.navigationType !== "traverse") remember();
|
|
568
850
|
});
|
|
851
|
+
function restoreSteady() {
|
|
852
|
+
const entry = steady.key && window.navigation?.entries().find(item => item.key === steady.key);
|
|
853
|
+
// Let the interrupted traversal settle before going back to the held/guarded entry.
|
|
854
|
+
setTimeout(() => entry ? window.navigation.traverseTo(entry.key) : history.pushState({ htmx: true }, "", steady.url));
|
|
855
|
+
}
|
|
569
856
|
// popstate covers every same-document traversal, including entries htmx did not stamp.
|
|
570
857
|
addEventListener("popstate", () => {
|
|
571
858
|
const held = hold();
|
|
572
|
-
if (
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
859
|
+
if (held) {
|
|
860
|
+
if (location.href === steady.url) return;
|
|
861
|
+
warnHeld(held.message);
|
|
862
|
+
restoreSteady();
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
const guarded = dirtyGuarded();
|
|
866
|
+
if (!guarded || location.href === steady.url) {
|
|
867
|
+
if (location.href === steady.url) dirtyHistoryDecision = null;
|
|
868
|
+
return remember();
|
|
869
|
+
}
|
|
870
|
+
if (dirtyHistoryDecision === "allow") return remember();
|
|
871
|
+
if (dirtyHistoryDecision === "block") return restoreSteady();
|
|
872
|
+
if (confirmLeaveDirty(guarded)) {
|
|
873
|
+
setDirtyHistoryDecision("allow");
|
|
874
|
+
remember();
|
|
875
|
+
} else {
|
|
876
|
+
setDirtyHistoryDecision("block");
|
|
877
|
+
restoreSteady();
|
|
878
|
+
}
|
|
578
879
|
});
|
|
579
880
|
document.addEventListener("htmx:before:history:restore", event => {
|
|
580
|
-
if (hold()) event.preventDefault();
|
|
881
|
+
if (hold()) { event.preventDefault(); return; }
|
|
882
|
+
const guarded = dirtyGuarded();
|
|
883
|
+
if (!guarded) return;
|
|
884
|
+
if (dirtyHistoryDecision === "allow") return;
|
|
885
|
+
if (dirtyHistoryDecision === "block") { event.preventDefault(); return; }
|
|
886
|
+
if (confirmLeaveDirty(guarded)) {
|
|
887
|
+
setDirtyHistoryDecision("allow");
|
|
888
|
+
} else {
|
|
889
|
+
setDirtyHistoryDecision("block");
|
|
890
|
+
event.preventDefault();
|
|
891
|
+
if (location.href !== steady.url) restoreSteady();
|
|
892
|
+
}
|
|
581
893
|
});
|
|
582
894
|
|
|
583
895
|
addEventListener("beforeunload", event => {
|