gitea源码

htmx.ts 919B

123456789101112131415161718192021222324252627
  1. import htmx from 'htmx.org';
  2. import 'idiomorph/htmx';
  3. import type {HtmxResponseInfo} from 'htmx.org';
  4. import {showErrorToast} from './modules/toast.ts';
  5. type HtmxEvent = Event & {detail: HtmxResponseInfo};
  6. export function initHtmx() {
  7. window.htmx = htmx;
  8. // https://htmx.org/reference/#config
  9. htmx.config.requestClass = 'is-loading';
  10. htmx.config.scrollIntoViewOnBoost = false;
  11. // https://htmx.org/events/#htmx:sendError
  12. document.body.addEventListener('htmx:sendError', (event: Partial<HtmxEvent>) => {
  13. // TODO: add translations
  14. showErrorToast(`Network error when calling ${event.detail.requestConfig.path}`);
  15. });
  16. // https://htmx.org/events/#htmx:responseError
  17. document.body.addEventListener('htmx:responseError', (event: Partial<HtmxEvent>) => {
  18. // TODO: add translations
  19. showErrorToast(`Error ${event.detail.xhr.status} when calling ${event.detail.requestConfig.path}`);
  20. });
  21. }