code.gitea.io/gitea@v1.22.3/web_src/js/features/repo-graph.js (about)

     1  import $ from 'jquery';
     2  import {hideElem, showElem} from '../utils/dom.js';
     3  import {GET} from '../modules/fetch.js';
     4  
     5  export function initRepoGraphGit() {
     6    const graphContainer = document.getElementById('git-graph-container');
     7    if (!graphContainer) return;
     8  
     9    document.getElementById('flow-color-monochrome')?.addEventListener('click', () => {
    10      document.getElementById('flow-color-monochrome').classList.add('active');
    11      document.getElementById('flow-color-colored')?.classList.remove('active');
    12      graphContainer.classList.remove('colored');
    13      graphContainer.classList.add('monochrome');
    14      const params = new URLSearchParams(window.location.search);
    15      params.set('mode', 'monochrome');
    16      const queryString = params.toString();
    17      if (queryString) {
    18        window.history.replaceState({}, '', `?${queryString}`);
    19      } else {
    20        window.history.replaceState({}, '', window.location.pathname);
    21      }
    22      for (const link of document.querySelectorAll('.pagination a')) {
    23        const href = link.getAttribute('href');
    24        if (!href) continue;
    25        const url = new URL(href, window.location);
    26        const params = url.searchParams;
    27        params.set('mode', 'monochrome');
    28        url.search = `?${params.toString()}`;
    29        link.setAttribute('href', url.href);
    30      }
    31    });
    32  
    33    document.getElementById('flow-color-colored')?.addEventListener('click', () => {
    34      document.getElementById('flow-color-colored').classList.add('active');
    35      document.getElementById('flow-color-monochrome')?.classList.remove('active');
    36      graphContainer.classList.add('colored');
    37      graphContainer.classList.remove('monochrome');
    38      for (const link of document.querySelectorAll('.pagination a')) {
    39        const href = link.getAttribute('href');
    40        if (!href) continue;
    41        const url = new URL(href, window.location);
    42        const params = url.searchParams;
    43        params.delete('mode');
    44        url.search = `?${params.toString()}`;
    45        link.setAttribute('href', url.href);
    46      }
    47      const params = new URLSearchParams(window.location.search);
    48      params.delete('mode');
    49      const queryString = params.toString();
    50      if (queryString) {
    51        window.history.replaceState({}, '', `?${queryString}`);
    52      } else {
    53        window.history.replaceState({}, '', window.location.pathname);
    54      }
    55    });
    56    const url = new URL(window.location);
    57    const params = url.searchParams;
    58    const updateGraph = () => {
    59      const queryString = params.toString();
    60      const ajaxUrl = new URL(url);
    61      ajaxUrl.searchParams.set('div-only', 'true');
    62      window.history.replaceState({}, '', queryString ? `?${queryString}` : window.location.pathname);
    63      document.getElementById('pagination').innerHTML = '';
    64      hideElem('#rel-container');
    65      hideElem('#rev-container');
    66      showElem('#loading-indicator');
    67      (async () => {
    68        const response = await GET(String(ajaxUrl));
    69        const html = await response.text();
    70        const div = document.createElement('div');
    71        div.innerHTML = html;
    72        document.getElementById('pagination').innerHTML = div.querySelector('#pagination').innerHTML;
    73        document.getElementById('rel-container').innerHTML = div.querySelector('#rel-container').innerHTML;
    74        document.getElementById('rev-container').innerHTML = div.querySelector('#rev-container').innerHTML;
    75        hideElem('#loading-indicator');
    76        showElem('#rel-container');
    77        showElem('#rev-container');
    78      })();
    79    };
    80    const dropdownSelected = params.getAll('branch');
    81    if (params.has('hide-pr-refs') && params.get('hide-pr-refs') === 'true') {
    82      dropdownSelected.splice(0, 0, '...flow-hide-pr-refs');
    83    }
    84  
    85    const flowSelectRefsDropdown = document.getElementById('flow-select-refs-dropdown');
    86    $(flowSelectRefsDropdown).dropdown('set selected', dropdownSelected);
    87    $(flowSelectRefsDropdown).dropdown({
    88      clearable: true,
    89      fullTextSeach: 'exact',
    90      onRemove(toRemove) {
    91        if (toRemove === '...flow-hide-pr-refs') {
    92          params.delete('hide-pr-refs');
    93        } else {
    94          const branches = params.getAll('branch');
    95          params.delete('branch');
    96          for (const branch of branches) {
    97            if (branch !== toRemove) {
    98              params.append('branch', branch);
    99            }
   100          }
   101        }
   102        updateGraph();
   103      },
   104      onAdd(toAdd) {
   105        if (toAdd === '...flow-hide-pr-refs') {
   106          params.set('hide-pr-refs', true);
   107        } else {
   108          params.append('branch', toAdd);
   109        }
   110        updateGraph();
   111      },
   112    });
   113  
   114    graphContainer.addEventListener('mouseenter', (e) => {
   115      if (e.target.matches('#rev-list li')) {
   116        const flow = e.target.getAttribute('data-flow');
   117        if (flow === '0') return;
   118        document.getElementById(`flow-${flow}`)?.classList.add('highlight');
   119        e.target.classList.add('hover');
   120        for (const item of document.querySelectorAll(`#rev-list li[data-flow='${flow}']`)) {
   121          item.classList.add('highlight');
   122        }
   123      } else if (e.target.matches('#rel-container .flow-group')) {
   124        e.target.classList.add('highlight');
   125        const flow = e.target.getAttribute('data-flow');
   126        for (const item of document.querySelectorAll(`#rev-list li[data-flow='${flow}']`)) {
   127          item.classList.add('highlight');
   128        }
   129      } else if (e.target.matches('#rel-container .flow-commit')) {
   130        const rev = e.target.getAttribute('data-rev');
   131        document.querySelector(`#rev-list li#commit-${rev}`)?.classList.add('hover');
   132      }
   133    });
   134  
   135    graphContainer.addEventListener('mouseleave', (e) => {
   136      if (e.target.matches('#rev-list li')) {
   137        const flow = e.target.getAttribute('data-flow');
   138        if (flow === '0') return;
   139        document.getElementById(`flow-${flow}`)?.classList.remove('highlight');
   140        e.target.classList.remove('hover');
   141        for (const item of document.querySelectorAll(`#rev-list li[data-flow='${flow}']`)) {
   142          item.classList.remove('highlight');
   143        }
   144      } else if (e.target.matches('#rel-container .flow-group')) {
   145        e.target.classList.remove('highlight');
   146        const flow = e.target.getAttribute('data-flow');
   147        for (const item of document.querySelectorAll(`#rev-list li[data-flow='${flow}']`)) {
   148          item.classList.remove('highlight');
   149        }
   150      } else if (e.target.matches('#rel-container .flow-commit')) {
   151        const rev = e.target.getAttribute('data-rev');
   152        document.querySelector(`#rev-list li#commit-${rev}`)?.classList.remove('hover');
   153      }
   154    });
   155  }