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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import $ from 'jquery';
  2. import {hideElem, showElem} from '../utils/dom.js';
  3. import {POST} from '../modules/fetch.js';
  4. async function getArchive($target, url, first) {
  5. const dropdownBtn = $target[0].closest('.ui.dropdown.button') ?? $target[0].closest('.ui.dropdown.btn');
  6. try {
  7. dropdownBtn.classList.add('is-loading');
  8. const response = await POST(url);
  9. if (response.status === 200) {
  10. const data = await response.json();
  11. if (!data) {
  12. // XXX Shouldn't happen?
  13. dropdownBtn.classList.remove('is-loading');
  14. return;
  15. }
  16. if (!data.complete) {
  17. // Wait for only three quarters of a second initially, in case it's
  18. // quickly archived.
  19. setTimeout(() => {
  20. getArchive($target, url, false);
  21. }, first ? 750 : 2000);
  22. } else {
  23. // We don't need to continue checking.
  24. dropdownBtn.classList.remove('is-loading');
  25. window.location.href = url;
  26. }
  27. }
  28. } catch {
  29. dropdownBtn.classList.remove('is-loading');
  30. }
  31. }
  32. export function initRepoArchiveLinks() {
  33. $('.archive-link').on('click', function (event) {
  34. event.preventDefault();
  35. const url = this.getAttribute('href');
  36. if (!url) return;
  37. getArchive($(event.target), url, true);
  38. });
  39. }
  40. export function initRepoCloneLink() {
  41. const $repoCloneSsh = $('#repo-clone-ssh');
  42. const $repoCloneHttps = $('#repo-clone-https');
  43. const $inputLink = $('#repo-clone-url');
  44. if ((!$repoCloneSsh.length && !$repoCloneHttps.length) || !$inputLink.length) {
  45. return;
  46. }
  47. $repoCloneSsh.on('click', () => {
  48. localStorage.setItem('repo-clone-protocol', 'ssh');
  49. window.updateCloneStates();
  50. });
  51. $repoCloneHttps.on('click', () => {
  52. localStorage.setItem('repo-clone-protocol', 'https');
  53. window.updateCloneStates();
  54. });
  55. $inputLink.on('focus', () => {
  56. $inputLink.trigger('select');
  57. });
  58. }
  59. export function initRepoCommonBranchOrTagDropdown(selector) {
  60. $(selector).each(function () {
  61. const $dropdown = $(this);
  62. $dropdown.find('.reference.column').on('click', function () {
  63. hideElem($dropdown.find('.scrolling.reference-list-menu'));
  64. showElem($($(this).data('target')));
  65. return false;
  66. });
  67. });
  68. }
  69. export function initRepoCommonFilterSearchDropdown(selector) {
  70. const $dropdown = $(selector);
  71. if (!$dropdown.length) return;
  72. $dropdown.dropdown({
  73. fullTextSearch: 'exact',
  74. selectOnKeydown: false,
  75. onChange(_text, _value, $choice) {
  76. if ($choice[0].getAttribute('data-url')) {
  77. window.location.href = $choice[0].getAttribute('data-url');
  78. }
  79. },
  80. message: {noResults: $dropdown[0].getAttribute('data-no-results')},
  81. });
  82. }