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-diff.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {initCompReactionSelector} from './comp/ReactionSelector.js';
  2. const {csrfToken} = window.config;
  3. export function initRepoDiffReviewButton() {
  4. $(document).on('click', 'button[name="is_review"]', (e) => {
  5. $(e.target).closest('form').append('<input type="hidden" name="is_review" value="true">');
  6. });
  7. }
  8. export function initRepoDiffFileViewToggle() {
  9. $('.file-view-toggle').on('click', function () {
  10. const $this = $(this);
  11. $this.parent().children().removeClass('active');
  12. $this.addClass('active');
  13. const $target = $($this.data('toggle-selector'));
  14. $target.parent().children().addClass('hide');
  15. $target.removeClass('hide');
  16. });
  17. }
  18. export function initRepoDiffConversationForm() {
  19. $(document).on('submit', '.conversation-holder form', async (e) => {
  20. e.preventDefault();
  21. const form = $(e.target);
  22. const newConversationHolder = $(await $.post(form.attr('action'), form.serialize()));
  23. const {path, side, idx} = newConversationHolder.data();
  24. form.closest('.conversation-holder').replaceWith(newConversationHolder);
  25. if (form.closest('tr').data('line-type') === 'same') {
  26. $(`[data-path="${path}"] a.add-code-comment[data-idx="${idx}"]`).addClass('invisible');
  27. } else {
  28. $(`[data-path="${path}"] a.add-code-comment[data-side="${side}"][data-idx="${idx}"]`).addClass('invisible');
  29. }
  30. newConversationHolder.find('.dropdown').dropdown();
  31. initCompReactionSelector(newConversationHolder);
  32. });
  33. $('.resolve-conversation').on('click', async function (e) {
  34. e.preventDefault();
  35. const comment_id = $(this).data('comment-id');
  36. const origin = $(this).data('origin');
  37. const action = $(this).data('action');
  38. const url = $(this).data('update-url');
  39. const data = await $.post(url, {_csrf: csrfToken, origin, action, comment_id});
  40. if ($(this).closest('.conversation-holder').length) {
  41. const conversation = $(data);
  42. $(this).closest('.conversation-holder').replaceWith(conversation);
  43. conversation.find('.dropdown').dropdown();
  44. initCompReactionSelector(conversation);
  45. } else {
  46. window.location.reload();
  47. }
  48. });
  49. }
  50. export function initRepoDiffConversationNav() {
  51. // Previous/Next code review conversation
  52. $(document).on('click', '.previous-conversation', (e) => {
  53. const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
  54. const $conversations = $('.comment-code-cloud:not(.hide)');
  55. const index = $conversations.index($conversation);
  56. const previousIndex = index > 0 ? index - 1 : $conversations.length - 1;
  57. const $previousConversation = $conversations.eq(previousIndex);
  58. const anchor = $previousConversation.find('.comment').first().attr('id');
  59. window.location.href = `#${anchor}`;
  60. });
  61. $(document).on('click', '.next-conversation', (e) => {
  62. const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
  63. const $conversations = $('.comment-code-cloud:not(.hide)');
  64. const index = $conversations.index($conversation);
  65. const nextIndex = index < $conversations.length - 1 ? index + 1 : 0;
  66. const $nextConversation = $conversations.eq(nextIndex);
  67. const anchor = $nextConversation.find('.comment').first().attr('id');
  68. window.location.href = `#${anchor}`;
  69. });
  70. }
  71. export function initRepoDiffShowMore() {
  72. $('#diff-files, #diff-file-boxes').on('click', '#diff-show-more-files, #diff-show-more-files-stats', (e) => {
  73. e.preventDefault();
  74. if ($(e.target).hasClass('disabled')) {
  75. return;
  76. }
  77. $('#diff-show-more-files, #diff-show-more-files-stats').addClass('disabled');
  78. const url = $('#diff-show-more-files, #diff-show-more-files-stats').data('href');
  79. $.ajax({
  80. type: 'GET',
  81. url,
  82. }).done((resp) => {
  83. if (!resp) {
  84. $('#diff-show-more-files, #diff-show-more-files-stats').removeClass('disabled');
  85. return;
  86. }
  87. $('#diff-too-many-files-stats').remove();
  88. $('#diff-files').append($(resp).find('#diff-files li'));
  89. $('#diff-incomplete').replaceWith($(resp).find('#diff-file-boxes').children());
  90. }).fail(() => {
  91. $('#diff-show-more-files, #diff-show-more-files-stats').removeClass('disabled');
  92. });
  93. });
  94. $(document).on('click', 'a.diff-show-more-button', (e) => {
  95. e.preventDefault();
  96. const $target = $(e.target);
  97. if ($target.hasClass('disabled')) {
  98. return;
  99. }
  100. $target.addClass('disabled');
  101. const url = $target.data('href');
  102. $.ajax({
  103. type: 'GET',
  104. url,
  105. }).done((resp) => {
  106. if (!resp) {
  107. $target.removeClass('disabled');
  108. return;
  109. }
  110. $target.parent().replaceWith($(resp).find('#diff-file-boxes .diff-file-body .file-body').children());
  111. }).fail(() => {
  112. $target.removeClass('disabled');
  113. });
  114. });
  115. }