gitea源码

repo-graph.ts 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {toggleElemClass} from '../utils/dom.ts';
  2. import {GET} from '../modules/fetch.ts';
  3. import {fomanticQuery} from '../modules/fomantic/base.ts';
  4. export function initRepoGraphGit() {
  5. const graphContainer = document.querySelector<HTMLElement>('#git-graph-container');
  6. if (!graphContainer) return;
  7. const elColorMonochrome = document.querySelector<HTMLElement>('#flow-color-monochrome');
  8. const elColorColored = document.querySelector<HTMLElement>('#flow-color-colored');
  9. const toggleColorMode = (mode: 'monochrome' | 'colored') => {
  10. toggleElemClass(graphContainer, 'monochrome', mode === 'monochrome');
  11. toggleElemClass(graphContainer, 'colored', mode === 'colored');
  12. toggleElemClass(elColorMonochrome, 'active', mode === 'monochrome');
  13. toggleElemClass(elColorColored, 'active', mode === 'colored');
  14. const params = new URLSearchParams(window.location.search);
  15. params.set('mode', mode);
  16. window.history.replaceState(null, '', `?${params.toString()}`);
  17. for (const link of document.querySelectorAll('#git-graph-body .pagination a')) {
  18. const href = link.getAttribute('href');
  19. if (!href) continue;
  20. const url = new URL(href, window.location.href);
  21. const params = url.searchParams;
  22. params.set('mode', mode);
  23. url.search = `?${params.toString()}`;
  24. link.setAttribute('href', url.href);
  25. }
  26. };
  27. elColorMonochrome.addEventListener('click', () => toggleColorMode('monochrome'));
  28. elColorColored.addEventListener('click', () => toggleColorMode('colored'));
  29. const elGraphBody = document.querySelector<HTMLElement>('#git-graph-body');
  30. const url = new URL(window.location.href);
  31. const params = url.searchParams;
  32. const loadGitGraph = async () => {
  33. const queryString = params.toString();
  34. const ajaxUrl = new URL(url);
  35. ajaxUrl.searchParams.set('div-only', 'true');
  36. window.history.replaceState(null, '', queryString ? `?${queryString}` : window.location.pathname);
  37. elGraphBody.classList.add('is-loading');
  38. try {
  39. const resp = await GET(ajaxUrl.toString());
  40. elGraphBody.innerHTML = await resp.text();
  41. } finally {
  42. elGraphBody.classList.remove('is-loading');
  43. }
  44. };
  45. const dropdownSelected = params.getAll('branch');
  46. if (params.has('hide-pr-refs') && params.get('hide-pr-refs') === 'true') {
  47. dropdownSelected.splice(0, 0, '...flow-hide-pr-refs');
  48. }
  49. const $dropdown = fomanticQuery('#flow-select-refs-dropdown');
  50. $dropdown.dropdown({clearable: true});
  51. $dropdown.dropdown('set selected', dropdownSelected);
  52. // must add the callback after setting the selected items, otherwise each "selected" item will trigger the callback
  53. $dropdown.dropdown('setting', {
  54. onRemove(toRemove: string) {
  55. if (toRemove === '...flow-hide-pr-refs') {
  56. params.delete('hide-pr-refs');
  57. } else {
  58. const branches = params.getAll('branch');
  59. params.delete('branch');
  60. for (const branch of branches) {
  61. if (branch !== toRemove) {
  62. params.append('branch', branch);
  63. }
  64. }
  65. }
  66. loadGitGraph();
  67. },
  68. onAdd(toAdd: string) {
  69. if (toAdd === '...flow-hide-pr-refs') {
  70. params.set('hide-pr-refs', 'true');
  71. } else {
  72. params.append('branch', toAdd);
  73. }
  74. loadGitGraph();
  75. },
  76. });
  77. }