Files
Atay-Makhzan/web_src/js/htmx.ts
T

27 lines
922 B
TypeScript
Raw Normal View History

2025-07-06 12:55:16 +08:00
import htmx from 'htmx.org';
2025-03-16 10:04:18 +01:00
import 'idiomorph/htmx';
2024-08-28 18:32:38 +02:00
import type {HtmxResponseInfo} from 'htmx.org';
2025-07-06 12:55:16 +08:00
import {showErrorToast} from './modules/toast.ts';
2024-08-28 18:32:38 +02:00
type HtmxEvent = Event & {detail: HtmxResponseInfo};
2025-07-06 12:55:16 +08:00
export function initHtmx() {
window.htmx = htmx;
// https://htmx.org/reference/#config
htmx.config.requestClass = 'is-loading';
htmx.config.scrollIntoViewOnBoost = false;
2025-07-06 12:55:16 +08:00
// https://htmx.org/events/#htmx:sendError
document.body.addEventListener('htmx:sendError', (event: Partial<HtmxEvent>) => {
// TODO: add translations
2025-12-03 03:13:16 +01:00
showErrorToast(`Network error when calling ${event.detail!.requestConfig.path}`);
2025-07-06 12:55:16 +08:00
});
2025-07-06 12:55:16 +08:00
// https://htmx.org/events/#htmx:responseError
document.body.addEventListener('htmx:responseError', (event: Partial<HtmxEvent>) => {
// TODO: add translations
2025-12-03 03:13:16 +01:00
showErrorToast(`Error ${event.detail!.xhr.status} when calling ${event.detail!.requestConfig.path}`);
2025-07-06 12:55:16 +08:00
});
}