Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

repo-diff.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import $ from 'jquery';
  2. import {initCompReactionSelector} from './comp/ReactionSelector.js';
  3. import {initRepoIssueContentHistory} from './repo-issue-content.js';
  4. import {initDiffFileTree} from './repo-diff-filetree.js';
  5. import {initDiffCommitSelect} from './repo-diff-commitselect.js';
  6. import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js';
  7. import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js';
  8. import {initImageDiff} from './imagediff.js';
  9. import {showErrorToast} from '../modules/toast.js';
  10. import {submitEventSubmitter} from '../utils/dom.js';
  11. import {POST, GET} from '../modules/fetch.js';
  12. const {pageData, i18n} = window.config;
  13. function initRepoDiffReviewButton() {
  14. const reviewBox = document.getElementById('review-box');
  15. if (!reviewBox) return;
  16. const $reviewBox = $(reviewBox);
  17. const counter = reviewBox.querySelector('.review-comments-counter');
  18. if (!counter) return;
  19. $(document).on('click', 'button[name="pending_review"]', (e) => {
  20. const $form = $(e.target).closest('form');
  21. // Watch for the form's submit event.
  22. $form.on('submit', () => {
  23. const num = parseInt(counter.getAttribute('data-pending-comment-number')) + 1 || 1;
  24. counter.setAttribute('data-pending-comment-number', num);
  25. counter.textContent = num;
  26. // Force the browser to reflow the DOM. This is to ensure that the browser replay the animation
  27. $reviewBox.removeClass('pulse');
  28. $reviewBox.width();
  29. $reviewBox.addClass('pulse');
  30. });
  31. });
  32. }
  33. function initRepoDiffFileViewToggle() {
  34. $('.file-view-toggle').on('click', function () {
  35. const $this = $(this);
  36. $this.parent().children().removeClass('active');
  37. $this.addClass('active');
  38. const $target = $($this.data('toggle-selector'));
  39. $target.parent().children().addClass('tw-hidden');
  40. $target.removeClass('tw-hidden');
  41. });
  42. }
  43. function initRepoDiffConversationForm() {
  44. $(document).on('submit', '.conversation-holder form', async (e) => {
  45. e.preventDefault();
  46. const $form = $(e.target);
  47. const textArea = e.target.querySelector('textarea');
  48. if (!validateTextareaNonEmpty(textArea)) {
  49. return;
  50. }
  51. if ($form.hasClass('is-loading')) return;
  52. try {
  53. $form.addClass('is-loading');
  54. const formData = new FormData($form[0]);
  55. // if the form is submitted by a button, append the button's name and value to the form data
  56. const submitter = submitEventSubmitter(e);
  57. const isSubmittedByButton = (submitter?.nodeName === 'BUTTON') || (submitter?.nodeName === 'INPUT' && submitter.type === 'submit');
  58. if (isSubmittedByButton && submitter.name) {
  59. formData.append(submitter.name, submitter.value);
  60. }
  61. const response = await POST(e.target.getAttribute('action'), {data: formData});
  62. const $newConversationHolder = $(await response.text());
  63. const {path, side, idx} = $newConversationHolder.data();
  64. $form.closest('.conversation-holder').replaceWith($newConversationHolder);
  65. if ($form.closest('tr').data('line-type') === 'same') {
  66. $(`[data-path="${path}"] .add-code-comment[data-idx="${idx}"]`).addClass('tw-invisible');
  67. } else {
  68. $(`[data-path="${path}"] .add-code-comment[data-side="${side}"][data-idx="${idx}"]`).addClass('tw-invisible');
  69. }
  70. $newConversationHolder.find('.dropdown').dropdown();
  71. initCompReactionSelector($newConversationHolder);
  72. } catch (error) {
  73. console.error('Error:', error);
  74. showErrorToast(i18n.network_error);
  75. } finally {
  76. $form.removeClass('is-loading');
  77. }
  78. });
  79. $(document).on('click', '.resolve-conversation', async function (e) {
  80. e.preventDefault();
  81. const comment_id = $(this).data('comment-id');
  82. const origin = $(this).data('origin');
  83. const action = $(this).data('action');
  84. const url = $(this).data('update-url');
  85. try {
  86. const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id})});
  87. const data = await response.text();
  88. if ($(this).closest('.conversation-holder').length) {
  89. const $conversation = $(data);
  90. $(this).closest('.conversation-holder').replaceWith($conversation);
  91. $conversation.find('.dropdown').dropdown();
  92. initCompReactionSelector($conversation);
  93. } else {
  94. window.location.reload();
  95. }
  96. } catch (error) {
  97. console.error('Error:', error);
  98. }
  99. });
  100. }
  101. export function initRepoDiffConversationNav() {
  102. // Previous/Next code review conversation
  103. $(document).on('click', '.previous-conversation', (e) => {
  104. const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
  105. const $conversations = $('.comment-code-cloud:not(.tw-hidden)');
  106. const index = $conversations.index($conversation);
  107. const previousIndex = index > 0 ? index - 1 : $conversations.length - 1;
  108. const $previousConversation = $conversations.eq(previousIndex);
  109. const anchor = $previousConversation.find('.comment').first()[0].getAttribute('id');
  110. window.location.href = `#${anchor}`;
  111. });
  112. $(document).on('click', '.next-conversation', (e) => {
  113. const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
  114. const $conversations = $('.comment-code-cloud:not(.tw-hidden)');
  115. const index = $conversations.index($conversation);
  116. const nextIndex = index < $conversations.length - 1 ? index + 1 : 0;
  117. const $nextConversation = $conversations.eq(nextIndex);
  118. const anchor = $nextConversation.find('.comment').first()[0].getAttribute('id');
  119. window.location.href = `#${anchor}`;
  120. });
  121. }
  122. // Will be called when the show more (files) button has been pressed
  123. function onShowMoreFiles() {
  124. initRepoIssueContentHistory();
  125. initViewedCheckboxListenerFor();
  126. countAndUpdateViewedFiles();
  127. initImageDiff();
  128. }
  129. export async function loadMoreFiles(url) {
  130. const $target = $('a#diff-show-more-files');
  131. if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
  132. return;
  133. }
  134. pageData.diffFileInfo.isLoadingNewData = true;
  135. $target.addClass('disabled');
  136. try {
  137. const response = await GET(url);
  138. const resp = await response.text();
  139. const $resp = $(resp);
  140. // the response is a full HTML page, we need to extract the relevant contents:
  141. // 1. append the newly loaded file list items to the existing list
  142. $('#diff-incomplete').replaceWith($resp.find('#diff-file-boxes').children());
  143. // 2. re-execute the script to append the newly loaded items to the JS variables to refresh the DiffFileTree
  144. $('body').append($resp.find('script#diff-data-script'));
  145. onShowMoreFiles();
  146. } catch (error) {
  147. console.error('Error:', error);
  148. showErrorToast('An error occurred while loading more files.');
  149. } finally {
  150. $target.removeClass('disabled');
  151. pageData.diffFileInfo.isLoadingNewData = false;
  152. }
  153. }
  154. function initRepoDiffShowMore() {
  155. $(document).on('click', 'a#diff-show-more-files', (e) => {
  156. e.preventDefault();
  157. const linkLoadMore = e.target.getAttribute('data-href');
  158. loadMoreFiles(linkLoadMore);
  159. });
  160. $(document).on('click', 'a.diff-load-button', async (e) => {
  161. e.preventDefault();
  162. const $target = $(e.target);
  163. if ($target.hasClass('disabled')) {
  164. return;
  165. }
  166. $target.addClass('disabled');
  167. const url = $target.data('href');
  168. try {
  169. const response = await GET(url);
  170. const resp = await response.text();
  171. if (!resp) {
  172. return;
  173. }
  174. $target.parent().replaceWith($(resp).find('#diff-file-boxes .diff-file-body .file-body').children());
  175. onShowMoreFiles();
  176. } catch (error) {
  177. console.error('Error:', error);
  178. } finally {
  179. $target.removeClass('disabled');
  180. }
  181. });
  182. }
  183. export function initRepoDiffView() {
  184. initRepoDiffConversationForm();
  185. if (!$('#diff-file-list').length) return;
  186. initDiffFileTree();
  187. initDiffCommitSelect();
  188. initRepoDiffShowMore();
  189. initRepoDiffReviewButton();
  190. initRepoDiffFileViewToggle();
  191. initViewedCheckboxListenerFor();
  192. initExpandAndCollapseFilesButton();
  193. }