2024-09-13 01:31:18 +00:00
|
|
|
import htmx from "htmx.org";
|
2024-09-13 15:47:18 +00:00
|
|
|
import "./htmxextensions/pathdeps";
|
|
|
|
|
import "./htmxextensions/trigger-children";
|
|
|
|
|
import "./htmxextensions/debug";
|
|
|
|
|
import "./htmxextensions/response-targets";
|
|
|
|
|
import "./htmxextensions/mutation-error";
|
2024-09-17 17:13:22 +00:00
|
|
|
import "./htmxextensions/livereload"
|
2024-09-22 15:46:38 +00:00
|
|
|
import "./htmxextensions/htmgo";
|
2024-10-02 03:26:03 +00:00
|
|
|
import "./htmxextensions/sse"
|
2024-11-09 18:05:53 +00:00
|
|
|
import "./htmxextensions/ws"
|
|
|
|
|
import "./htmxextensions/ws-event-handler"
|
2024-09-12 17:15:17 +00:00
|
|
|
|
2024-10-07 17:57:24 +00:00
|
|
|
// @ts-ignore
|
|
|
|
|
window.htmx = htmx;
|
|
|
|
|
|
2024-09-12 17:15:17 +00:00
|
|
|
function watchUrl(callback: (oldUrl: string, newUrl: string) => void) {
|
|
|
|
|
let lastUrl = window.location.href;
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
if (window.location.href !== lastUrl) {
|
|
|
|
|
callback(lastUrl, window.location.href);
|
|
|
|
|
lastUrl = window.location.href;
|
|
|
|
|
}
|
2024-10-11 14:07:56 +00:00
|
|
|
}, 101);
|
2024-09-12 17:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watchUrl((_, newUrl) => {
|
|
|
|
|
onUrlChange(newUrl);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function onUrlChange(newUrl: string) {
|
|
|
|
|
let url = new URL(newUrl);
|
|
|
|
|
|
|
|
|
|
document.querySelectorAll("[hx-trigger]").forEach(function (element) {
|
|
|
|
|
const triggers = element.getAttribute("hx-trigger");
|
|
|
|
|
if (!triggers) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const split = triggers.split(", ");
|
|
|
|
|
if (split.find((s) => s === "url")) {
|
2024-09-13 01:31:18 +00:00
|
|
|
htmx.swap(element, "url", {
|
|
|
|
|
swapStyle: "outerHTML",
|
|
|
|
|
swapDelay: 0,
|
|
|
|
|
settleDelay: 0,
|
|
|
|
|
});
|
2024-09-12 17:15:17 +00:00
|
|
|
} else {
|
|
|
|
|
for (let [key, values] of url.searchParams) {
|
|
|
|
|
let eventName = "qs:" + key;
|
|
|
|
|
if (triggers.includes(eventName)) {
|
|
|
|
|
htmx.trigger(element, eventName, null);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
document.querySelectorAll("[hx-match-qp]").forEach((el) => {
|
|
|
|
|
let hasMatch = false;
|
|
|
|
|
for (let name of el.getAttributeNames()) {
|
|
|
|
|
if (name.startsWith("hx-match-qp-mapping:")) {
|
|
|
|
|
let match = name.replace("hx-match-qp-mapping:", "");
|
|
|
|
|
let value = url.searchParams.get(match);
|
|
|
|
|
if (value) {
|
|
|
|
|
htmx.swap(el, el.getAttribute(name) ?? "", {
|
|
|
|
|
swapStyle: "innerHTML",
|
2024-09-13 01:31:18 +00:00
|
|
|
swapDelay: 0,
|
|
|
|
|
settleDelay: 0,
|
2024-09-12 17:15:17 +00:00
|
|
|
});
|
|
|
|
|
hasMatch = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!hasMatch) {
|
|
|
|
|
let defaultKey = el.getAttribute("hx-match-qp-default");
|
|
|
|
|
if (defaultKey) {
|
|
|
|
|
htmx.swap(
|
|
|
|
|
el,
|
|
|
|
|
el.getAttribute("hx-match-qp-mapping:" + defaultKey) ?? "",
|
2024-09-13 01:31:18 +00:00
|
|
|
{ swapStyle: "innerHTML", swapDelay: 0, settleDelay: 0 },
|
2024-09-12 17:15:17 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-10-20 12:48:19 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
400s should allow swapping by default, as it's useful to show error messages
|
|
|
|
|
*/
|
|
|
|
|
document.addEventListener('htmx:beforeSwap', function(evt) {
|
|
|
|
|
if(evt instanceof CustomEvent) {
|
|
|
|
|
// Allow 422 and 400 responses to swap
|
|
|
|
|
// We treat these as form validation errors
|
|
|
|
|
if (evt.detail.xhr.status === 422 || evt.detail.xhr.status === 400) {
|
|
|
|
|
evt.detail.shouldSwap = true;
|
|
|
|
|
evt.detail.isError = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|