You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo-common.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import $ from 'jquery';
  2. import {hideElem, queryElems, showElem} from '../utils/dom.js';
  3. import {POST} from '../modules/fetch.js';
  4. import {showErrorToast} from '../modules/toast.js';
  5. import {sleep} from '../utils.js';
  6. async function onDownloadArchive(e) {
  7. e.preventDefault();
  8. // there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list
  9. const el = e.target.closest('a.archive-link[href]');
  10. const targetLoading = el.closest('.ui.dropdown') ?? el;
  11. targetLoading.classList.add('is-loading', 'loading-icon-2px');
  12. try {
  13. for (let tryCount = 0; ;tryCount++) {
  14. const response = await POST(el.href);
  15. if (!response.ok) throw new Error(`Invalid server response: ${response.status}`);
  16. const data = await response.json();
  17. if (data.complete) break;
  18. await sleep(Math.min((tryCount + 1) * 750, 2000));
  19. }
  20. window.location.href = el.href; // the archive is ready, start real downloading
  21. } catch (e) {
  22. console.error(e);
  23. showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500});
  24. } finally {
  25. targetLoading.classList.remove('is-loading', 'loading-icon-2px');
  26. }
  27. }
  28. export function initRepoArchiveLinks() {
  29. queryElems('a.archive-link[href]', (el) => el.addEventListener('click', onDownloadArchive));
  30. }
  31. export function initRepoCloneLink() {
  32. const $repoCloneSsh = $('#repo-clone-ssh');
  33. const $repoCloneHttps = $('#repo-clone-https');
  34. const $inputLink = $('#repo-clone-url');
  35. if ((!$repoCloneSsh.length && !$repoCloneHttps.length) || !$inputLink.length) {
  36. return;
  37. }
  38. $repoCloneSsh.on('click', () => {
  39. localStorage.setItem('repo-clone-protocol', 'ssh');
  40. window.updateCloneStates();
  41. });
  42. $repoCloneHttps.on('click', () => {
  43. localStorage.setItem('repo-clone-protocol', 'https');
  44. window.updateCloneStates();
  45. });
  46. $inputLink.on('focus', () => {
  47. $inputLink.trigger('select');
  48. });
  49. }
  50. export function initRepoCommonBranchOrTagDropdown(selector) {
  51. $(selector).each(function () {
  52. const $dropdown = $(this);
  53. $dropdown.find('.reference.column').on('click', function () {
  54. hideElem($dropdown.find('.scrolling.reference-list-menu'));
  55. showElem($($(this).data('target')));
  56. return false;
  57. });
  58. });
  59. }
  60. export function initRepoCommonFilterSearchDropdown(selector) {
  61. const $dropdown = $(selector);
  62. if (!$dropdown.length) return;
  63. $dropdown.dropdown({
  64. fullTextSearch: 'exact',
  65. selectOnKeydown: false,
  66. onChange(_text, _value, $choice) {
  67. if ($choice[0].getAttribute('data-url')) {
  68. window.location.href = $choice[0].getAttribute('data-url');
  69. }
  70. },
  71. message: {noResults: $dropdown[0].getAttribute('data-no-results')},
  72. });
  73. }