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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const {csrfToken} = window.config;
  2. function getArchive($target, url, first) {
  3. $.ajax({
  4. url,
  5. type: 'POST',
  6. data: {
  7. _csrf: csrfToken,
  8. },
  9. complete(xhr) {
  10. if (xhr.status === 200) {
  11. if (!xhr.responseJSON) {
  12. // XXX Shouldn't happen?
  13. $target.closest('.dropdown').children('i').removeClass('loading');
  14. return;
  15. }
  16. if (!xhr.responseJSON.complete) {
  17. $target.closest('.dropdown').children('i').addClass('loading');
  18. // Wait for only three quarters of a second initially, in case it's
  19. // quickly archived.
  20. setTimeout(() => {
  21. getArchive($target, url, false);
  22. }, first ? 750 : 2000);
  23. } else {
  24. // We don't need to continue checking.
  25. $target.closest('.dropdown').children('i').removeClass('loading');
  26. window.location.href = url;
  27. }
  28. }
  29. },
  30. });
  31. }
  32. export function initRepoArchiveLinks() {
  33. $('.archive-link').on('click', function (event) {
  34. event.preventDefault();
  35. const url = $(this).attr('href');
  36. if (!url) return;
  37. getArchive($(event.target), url, true);
  38. });
  39. }
  40. export function initRepoClone() {
  41. // Quick start and repository home
  42. $('#repo-clone-ssh').on('click', function () {
  43. $('.clone-url').text($(this).data('link'));
  44. $('#repo-clone-url').val($(this).data('link'));
  45. $(this).addClass('primary');
  46. $('#repo-clone-https').removeClass('primary');
  47. localStorage.setItem('repo-clone-protocol', 'ssh');
  48. });
  49. $('#repo-clone-https').on('click', function () {
  50. $('.clone-url').text($(this).data('link'));
  51. $('#repo-clone-url').val($(this).data('link'));
  52. $(this).addClass('primary');
  53. if ($('#repo-clone-ssh').length > 0) {
  54. $('#repo-clone-ssh').removeClass('primary');
  55. localStorage.setItem('repo-clone-protocol', 'https');
  56. }
  57. });
  58. $('#repo-clone-url').on('click', function () {
  59. $(this).select();
  60. });
  61. }
  62. export function initRepoCommonBranchOrTagDropdown(selector) {
  63. $(selector).each(function () {
  64. const $dropdown = $(this);
  65. $dropdown.find('.reference.column').on('click', function () {
  66. $dropdown.find('.scrolling.reference-list-menu').hide();
  67. $($(this).data('target')).show();
  68. return false;
  69. });
  70. });
  71. }
  72. export function initRepoCommonFilterSearchDropdown(selector) {
  73. const $dropdown = $(selector);
  74. $dropdown.dropdown({
  75. fullTextSearch: true,
  76. selectOnKeydown: false,
  77. onChange(_text, _value, $choice) {
  78. if ($choice.data('url')) {
  79. window.location.href = $choice.data('url');
  80. }
  81. },
  82. message: {noResults: $dropdown.data('no-results')},
  83. });
  84. }
  85. export function initRepoCommonLanguageStats() {
  86. // Language stats
  87. if ($('.language-stats').length > 0) {
  88. $('.language-stats').on('click', (e) => {
  89. e.preventDefault();
  90. $('.language-stats-details, .repository-menu').slideToggle();
  91. });
  92. }
  93. }