gitea源码

modal.ts 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import $ from 'jquery';
  2. import type {FomanticInitFunction} from '../../types.ts';
  3. import {queryElems} from '../../utils/dom.ts';
  4. import {hideToastsFrom} from '../toast.ts';
  5. const fomanticModalFn = $.fn.modal;
  6. // use our own `$.fn.modal` to patch Fomantic's modal module
  7. export function initAriaModalPatch() {
  8. if ($.fn.modal === ariaModalFn) throw new Error('initAriaModalPatch could only be called once');
  9. $.fn.modal = ariaModalFn;
  10. (ariaModalFn as FomanticInitFunction).settings = fomanticModalFn.settings;
  11. $.fn.fomanticExt.onModalBeforeHidden = onModalBeforeHidden;
  12. $.fn.modal.settings.onApprove = onModalApproveDefault;
  13. }
  14. // the patched `$.fn.modal` modal function
  15. // * it does the one-time attaching on the first call
  16. function ariaModalFn(this: any, ...args: Parameters<FomanticInitFunction>) {
  17. const ret = fomanticModalFn.apply(this, args);
  18. if (args[0] === 'show' || args[0]?.autoShow) {
  19. for (const el of this) {
  20. // If there is a form in the modal, there might be a "cancel" button before "ok" button (all buttons are "type=submit" by default).
  21. // In such case, the "Enter" key will trigger the "cancel" button instead of "ok" button, then the dialog will be closed.
  22. // It breaks the user experience - the "Enter" key should confirm the dialog and submit the form.
  23. // So, all "cancel" buttons without "[type]" must be marked as "type=button".
  24. for (const button of el.querySelectorAll('form button.cancel:not([type])')) {
  25. button.setAttribute('type', 'button');
  26. }
  27. }
  28. }
  29. return ret;
  30. }
  31. function onModalBeforeHidden(this: any) {
  32. const $modal = $(this);
  33. const elModal = $modal[0];
  34. hideToastsFrom(elModal.closest('.ui.dimmer') ?? document.body);
  35. // reset the form after the modal is hidden, after other modal events and handlers (e.g. "onApprove", form submit)
  36. setTimeout(() => {
  37. queryElems(elModal, 'form', (form: HTMLFormElement) => form.reset());
  38. }, 0);
  39. }
  40. function onModalApproveDefault(this: any) {
  41. const $modal = $(this);
  42. const selectors = $modal.modal('setting', 'selector');
  43. const elModal = $modal[0];
  44. const elApprove = elModal.querySelector(selectors.approve);
  45. const elForm = elApprove?.closest('form');
  46. if (!elForm) return true; // no form, just allow closing the modal
  47. // "form-fetch-action" can handle network errors gracefully,
  48. // so keep the modal dialog to make users can re-submit the form if anything wrong happens.
  49. if (elForm.matches('.form-fetch-action')) return false;
  50. // There is an abuse for the "modal" + "form" combination, the "Approve" button is a traditional form submit button in the form.
  51. // Then "approve" and "submit" occur at the same time, the modal will be closed immediately before the form is submitted.
  52. // So here we prevent the modal from closing automatically by returning false, add the "is-loading" class to the form element.
  53. elForm.classList.add('is-loading');
  54. return false;
  55. }