gitea源码

WebHookEditor.ts 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {POST} from '../../modules/fetch.ts';
  2. import {hideElem, showElem, toggleElem} from '../../utils/dom.ts';
  3. export function initCompWebHookEditor() {
  4. if (!document.querySelectorAll('.new.webhook').length) {
  5. return;
  6. }
  7. for (const input of document.querySelectorAll<HTMLInputElement>('.events.checkbox input')) {
  8. input.addEventListener('change', function () {
  9. if (this.checked) {
  10. showElem('.events.fields');
  11. }
  12. });
  13. }
  14. for (const input of document.querySelectorAll<HTMLInputElement>('.non-events.checkbox input')) {
  15. input.addEventListener('change', function () {
  16. if (this.checked) {
  17. hideElem('.events.fields');
  18. }
  19. });
  20. }
  21. // some webhooks (like Gitea) allow to set the request method (GET/POST), and it would toggle the "Content Type" field
  22. const httpMethodInput = document.querySelector<HTMLInputElement>('#http_method');
  23. if (httpMethodInput) {
  24. const updateContentType = function () {
  25. const visible = httpMethodInput.value === 'POST';
  26. toggleElem(document.querySelector('#content_type').closest('.field'), visible);
  27. };
  28. updateContentType();
  29. httpMethodInput.addEventListener('change', updateContentType);
  30. }
  31. // Test delivery
  32. document.querySelector<HTMLButtonElement>('#test-delivery')?.addEventListener('click', async function () {
  33. this.classList.add('is-loading', 'disabled');
  34. await POST(this.getAttribute('data-link'));
  35. setTimeout(() => {
  36. window.location.href = this.getAttribute('data-redirect');
  37. }, 5000);
  38. });
  39. }