code.gitea.io/gitea@v1.21.7/web_src/js/features/repo-diff-commit.js (about)

     1  import {hideElem, showElem, toggleElem} from '../utils/dom.js';
     2  import {GET} from '../modules/fetch.js';
     3  
     4  async function loadBranchesAndTags(area, loadingButton) {
     5    loadingButton.classList.add('disabled');
     6    try {
     7      const res = await GET(loadingButton.getAttribute('data-fetch-url'));
     8      const data = await res.json();
     9      hideElem(loadingButton);
    10      addTags(area, data.tags);
    11      addBranches(area, data.branches, data.default_branch);
    12      showElem(area.querySelectorAll('.branch-and-tag-detail'));
    13    } finally {
    14      loadingButton.classList.remove('disabled');
    15    }
    16  }
    17  
    18  function addTags(area, tags) {
    19    const tagArea = area.querySelector('.tag-area');
    20    toggleElem(tagArea.parentElement, tags.length > 0);
    21    for (const tag of tags) {
    22      addLink(tagArea, tag.web_link, tag.name);
    23    }
    24  }
    25  
    26  function addBranches(area, branches, defaultBranch) {
    27    const defaultBranchTooltip = area.getAttribute('data-text-default-branch-tooltip');
    28    const branchArea = area.querySelector('.branch-area');
    29    toggleElem(branchArea.parentElement, branches.length > 0);
    30    for (const branch of branches) {
    31      const tooltip = defaultBranch === branch.name ? defaultBranchTooltip : null;
    32      addLink(branchArea, branch.web_link, branch.name, tooltip);
    33    }
    34  }
    35  
    36  function addLink(parent, href, text, tooltip) {
    37    const link = document.createElement('a');
    38    link.classList.add('muted', 'gt-px-2');
    39    link.href = href;
    40    link.textContent = text;
    41    if (tooltip) {
    42      link.classList.add('gt-border-secondary', 'gt-rounded');
    43      link.setAttribute('data-tooltip-content', tooltip);
    44    }
    45    parent.append(link);
    46  }
    47  
    48  export function initRepoDiffCommitBranchesAndTags() {
    49    for (const area of document.querySelectorAll('.branch-and-tag-area')) {
    50      const btn = area.querySelector('.load-branches-and-tags');
    51      btn.addEventListener('click', () => loadBranchesAndTags(area, btn));
    52    }
    53  }