gitea源码

repo-branch.ts 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {toggleElem} from '../utils/dom.ts';
  2. import {fomanticQuery} from '../modules/fomantic/base.ts';
  3. export function initRepoBranchButton() {
  4. initRepoCreateBranchButton();
  5. initRepoRenameBranchButton();
  6. }
  7. function initRepoCreateBranchButton() {
  8. // 2 pages share this code, one is the branch list page, the other is the commit view page: create branch/tag from current commit (dirty code)
  9. for (const el of document.querySelectorAll('.show-create-branch-modal')) {
  10. el.addEventListener('click', () => {
  11. const modalFormName = el.getAttribute('data-modal-form') || '#create-branch-form';
  12. const modalForm = document.querySelector<HTMLFormElement>(modalFormName);
  13. if (!modalForm) return;
  14. modalForm.action = `${modalForm.getAttribute('data-base-action')}${el.getAttribute('data-branch-from-urlcomponent')}`;
  15. const fromSpanName = el.getAttribute('data-modal-from-span') || '#modal-create-branch-from-span';
  16. document.querySelector(fromSpanName).textContent = el.getAttribute('data-branch-from');
  17. fomanticQuery(el.getAttribute('data-modal')).modal('show');
  18. });
  19. }
  20. }
  21. function initRepoRenameBranchButton() {
  22. for (const el of document.querySelectorAll('.show-rename-branch-modal')) {
  23. el.addEventListener('click', () => {
  24. const target = el.getAttribute('data-modal');
  25. const modal = document.querySelector(target);
  26. const oldBranchName = el.getAttribute('data-old-branch-name');
  27. modal.querySelector<HTMLInputElement>('input[name=from]').value = oldBranchName;
  28. // display the warning that the branch which is chosen is the default branch
  29. const warn = modal.querySelector('.default-branch-warning');
  30. toggleElem(warn, el.getAttribute('data-is-default-branch') === 'true');
  31. const text = modal.querySelector('[data-rename-branch-to]');
  32. text.textContent = text.getAttribute('data-rename-branch-to').replace('%s', oldBranchName);
  33. });
  34. }
  35. }