gitea源码

repo-release.ts 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {hideElem, showElem, type DOMEvent} from '../utils/dom.ts';
  2. export function initRepoRelease() {
  3. document.addEventListener('click', (e: DOMEvent<MouseEvent>) => {
  4. if (e.target.matches('.remove-rel-attach')) {
  5. const uuid = e.target.getAttribute('data-uuid');
  6. const id = e.target.getAttribute('data-id');
  7. document.querySelector<HTMLInputElement>(`input[name='attachment-del-${uuid}']`).value = 'true';
  8. hideElem(`#attachment-${id}`);
  9. }
  10. });
  11. }
  12. export function initRepoReleaseNew() {
  13. if (!document.querySelector('.repository.new.release')) return;
  14. initTagNameEditor();
  15. }
  16. function initTagNameEditor() {
  17. const el = document.querySelector('#tag-name-editor');
  18. if (!el) return;
  19. const existingTags = JSON.parse(el.getAttribute('data-existing-tags'));
  20. if (!Array.isArray(existingTags)) return;
  21. const defaultTagHelperText = el.getAttribute('data-tag-helper');
  22. const newTagHelperText = el.getAttribute('data-tag-helper-new');
  23. const existingTagHelperText = el.getAttribute('data-tag-helper-existing');
  24. const tagNameInput = document.querySelector<HTMLInputElement>('#tag-name');
  25. const hideTargetInput = function(tagNameInput: HTMLInputElement) {
  26. const value = tagNameInput.value;
  27. const tagHelper = document.querySelector('#tag-helper');
  28. if (existingTags.includes(value)) {
  29. // If the tag already exists, hide the target branch selector.
  30. hideElem('#tag-target-selector');
  31. tagHelper.textContent = existingTagHelperText;
  32. } else {
  33. showElem('#tag-target-selector');
  34. tagHelper.textContent = value ? newTagHelperText : defaultTagHelperText;
  35. }
  36. };
  37. hideTargetInput(tagNameInput); // update on page load because the input may have a value
  38. tagNameInput.addEventListener('input', (e) => {
  39. hideTargetInput(e.target as HTMLInputElement);
  40. });
  41. }