2022-01-28 16:00:11 -05:00
|
|
|
import $ from 'jquery';
|
2021-10-16 13:28:04 -04:00
|
|
|
import {initCompReactionSelector} from './comp/ReactionSelector.js';
|
2021-12-05 22:57:51 -05:00
|
|
|
import {initRepoIssueContentHistory} from './repo-issue-content.js';
|
2023-04-09 09:11:02 -04:00
|
|
|
import {initDiffFileTree} from './repo-diff-filetree.js';
|
2023-07-28 15:18:12 -04:00
|
|
|
import {initDiffCommitSelect} from './repo-diff-commitselect.js';
|
2023-04-03 06:06:57 -04:00
|
|
|
import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js';
|
2023-04-09 09:11:02 -04:00
|
|
|
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js';
|
2023-07-04 04:36:27 -04:00
|
|
|
import {initImageDiff} from './imagediff.js';
|
2023-11-20 21:12:31 -05:00
|
|
|
import {showErrorToast} from '../modules/toast.js';
|
2023-12-14 18:26:36 -05:00
|
|
|
import {submitEventSubmitter} from '../utils/dom.js';
|
2022-01-28 16:00:11 -05:00
|
|
|
|
2023-11-20 21:12:31 -05:00
|
|
|
const {csrfToken, pageData, i18n} = window.config;
|
2021-10-16 13:28:04 -04:00
|
|
|
|
2023-04-09 09:11:02 -04:00
|
|
|
function initRepoDiffReviewButton() {
|
2022-05-07 01:35:12 -04:00
|
|
|
const $reviewBox = $('#review-box');
|
|
|
|
const $counter = $reviewBox.find('.review-comments-counter');
|
|
|
|
|
2023-03-04 02:13:37 -05:00
|
|
|
$(document).on('click', 'button[name="pending_review"]', (e) => {
|
2022-05-07 01:35:12 -04:00
|
|
|
const $form = $(e.target).closest('form');
|
|
|
|
// Watch for the form's submit event.
|
|
|
|
$form.on('submit', () => {
|
|
|
|
const num = parseInt($counter.attr('data-pending-comment-number')) + 1 || 1;
|
|
|
|
$counter.attr('data-pending-comment-number', num);
|
|
|
|
$counter.text(num);
|
|
|
|
// Force the browser to reflow the DOM. This is to ensure that the browser replay the animation
|
|
|
|
$reviewBox.removeClass('pulse');
|
|
|
|
$reviewBox.width();
|
|
|
|
$reviewBox.addClass('pulse');
|
|
|
|
});
|
2021-10-16 13:28:04 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-09 09:11:02 -04:00
|
|
|
function initRepoDiffFileViewToggle() {
|
2021-10-16 13:28:04 -04:00
|
|
|
$('.file-view-toggle').on('click', function () {
|
|
|
|
const $this = $(this);
|
|
|
|
$this.parent().children().removeClass('active');
|
|
|
|
$this.addClass('active');
|
|
|
|
|
2022-01-16 06:19:26 -05:00
|
|
|
const $target = $($this.data('toggle-selector'));
|
2023-02-18 23:06:14 -05:00
|
|
|
$target.parent().children().addClass('gt-hidden');
|
|
|
|
$target.removeClass('gt-hidden');
|
2021-10-16 13:28:04 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-09 09:11:02 -04:00
|
|
|
function initRepoDiffConversationForm() {
|
2021-10-16 16:30:31 -04:00
|
|
|
$(document).on('submit', '.conversation-holder form', async (e) => {
|
2021-10-16 13:28:04 -04:00
|
|
|
e.preventDefault();
|
2022-01-02 17:31:03 -05:00
|
|
|
|
2022-08-16 17:05:40 -04:00
|
|
|
const $form = $(e.target);
|
2024-02-20 05:37:37 -05:00
|
|
|
const textArea = e.target.querySelector('textarea');
|
|
|
|
if (!validateTextareaNonEmpty(textArea)) {
|
2022-01-02 17:31:03 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-20 21:12:31 -05:00
|
|
|
if ($form.hasClass('is-loading')) return;
|
|
|
|
try {
|
|
|
|
$form.addClass('is-loading');
|
|
|
|
const formData = new FormData($form[0]);
|
2023-03-04 02:13:37 -05:00
|
|
|
|
2024-02-17 06:52:11 -05:00
|
|
|
// If the form is submitted by a button, append the button's name and value to the form data.
|
|
|
|
// originalEvent can be undefined, such as an event that's caused by Ctrl+Enter, in that case
|
|
|
|
// sent the event itself.
|
|
|
|
const submitter = submitEventSubmitter(e.originalEvent ?? e);
|
2023-11-20 21:12:31 -05:00
|
|
|
const isSubmittedByButton = (submitter?.nodeName === 'BUTTON') || (submitter?.nodeName === 'INPUT' && submitter.type === 'submit');
|
|
|
|
if (isSubmittedByButton && submitter.name) {
|
|
|
|
formData.append(submitter.name, submitter.value);
|
|
|
|
}
|
|
|
|
const formDataString = String(new URLSearchParams(formData));
|
|
|
|
const $newConversationHolder = $(await $.post($form.attr('action'), formDataString));
|
|
|
|
const {path, side, idx} = $newConversationHolder.data();
|
2021-10-16 13:28:04 -04:00
|
|
|
|
2023-11-20 21:12:31 -05:00
|
|
|
$form.closest('.conversation-holder').replaceWith($newConversationHolder);
|
|
|
|
if ($form.closest('tr').data('line-type') === 'same') {
|
|
|
|
$(`[data-path="${path}"] .add-code-comment[data-idx="${idx}"]`).addClass('gt-invisible');
|
|
|
|
} else {
|
|
|
|
$(`[data-path="${path}"] .add-code-comment[data-side="${side}"][data-idx="${idx}"]`).addClass('gt-invisible');
|
|
|
|
}
|
|
|
|
$newConversationHolder.find('.dropdown').dropdown();
|
|
|
|
initCompReactionSelector($newConversationHolder);
|
|
|
|
} catch { // here the caught error might be a jQuery AJAX error (thrown by await $.post), which is not good to use for error message handling
|
2024-02-17 06:52:11 -05:00
|
|
|
console.error('error when submitting conversation', e);
|
2023-11-20 21:12:31 -05:00
|
|
|
showErrorToast(i18n.network_error);
|
|
|
|
} finally {
|
|
|
|
$form.removeClass('is-loading');
|
2021-10-16 13:28:04 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-01-18 12:28:38 -05:00
|
|
|
$(document).on('click', '.resolve-conversation', async function (e) {
|
2021-10-16 13:28:04 -04:00
|
|
|
e.preventDefault();
|
|
|
|
const comment_id = $(this).data('comment-id');
|
|
|
|
const origin = $(this).data('origin');
|
|
|
|
const action = $(this).data('action');
|
|
|
|
const url = $(this).data('update-url');
|
|
|
|
|
2021-10-21 03:37:43 -04:00
|
|
|
const data = await $.post(url, {_csrf: csrfToken, origin, action, comment_id});
|
2021-10-16 13:28:04 -04:00
|
|
|
|
|
|
|
if ($(this).closest('.conversation-holder').length) {
|
|
|
|
const conversation = $(data);
|
|
|
|
$(this).closest('.conversation-holder').replaceWith(conversation);
|
|
|
|
conversation.find('.dropdown').dropdown();
|
|
|
|
initCompReactionSelector(conversation);
|
|
|
|
} else {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoDiffConversationNav() {
|
|
|
|
// Previous/Next code review conversation
|
|
|
|
$(document).on('click', '.previous-conversation', (e) => {
|
|
|
|
const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
|
2023-02-18 23:06:14 -05:00
|
|
|
const $conversations = $('.comment-code-cloud:not(.gt-hidden)');
|
2021-10-16 13:28:04 -04:00
|
|
|
const index = $conversations.index($conversation);
|
|
|
|
const previousIndex = index > 0 ? index - 1 : $conversations.length - 1;
|
|
|
|
const $previousConversation = $conversations.eq(previousIndex);
|
|
|
|
const anchor = $previousConversation.find('.comment').first().attr('id');
|
|
|
|
window.location.href = `#${anchor}`;
|
|
|
|
});
|
|
|
|
$(document).on('click', '.next-conversation', (e) => {
|
|
|
|
const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
|
2023-02-18 23:06:14 -05:00
|
|
|
const $conversations = $('.comment-code-cloud:not(.gt-hidden)');
|
2021-10-16 13:28:04 -04:00
|
|
|
const index = $conversations.index($conversation);
|
|
|
|
const nextIndex = index < $conversations.length - 1 ? index + 1 : 0;
|
|
|
|
const $nextConversation = $conversations.eq(nextIndex);
|
|
|
|
const anchor = $nextConversation.find('.comment').first().attr('id');
|
|
|
|
window.location.href = `#${anchor}`;
|
|
|
|
});
|
|
|
|
}
|
2021-11-09 04:27:25 -05:00
|
|
|
|
2022-05-07 14:28:10 -04:00
|
|
|
// Will be called when the show more (files) button has been pressed
|
|
|
|
function onShowMoreFiles() {
|
|
|
|
initRepoIssueContentHistory();
|
|
|
|
initViewedCheckboxListenerFor();
|
|
|
|
countAndUpdateViewedFiles();
|
2023-07-04 04:36:27 -04:00
|
|
|
initImageDiff();
|
2022-05-07 14:28:10 -04:00
|
|
|
}
|
|
|
|
|
2023-05-30 06:53:15 -04:00
|
|
|
export function loadMoreFiles(url) {
|
2023-01-13 00:50:32 -05:00
|
|
|
const $target = $('a#diff-show-more-files');
|
2023-05-30 06:53:15 -04:00
|
|
|
if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
|
2023-01-13 00:50:32 -05:00
|
|
|
return;
|
|
|
|
}
|
2023-05-30 06:53:15 -04:00
|
|
|
|
|
|
|
pageData.diffFileInfo.isLoadingNewData = true;
|
2023-01-13 00:50:32 -05:00
|
|
|
$target.addClass('disabled');
|
2022-09-27 01:22:19 -04:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url,
|
|
|
|
}).done((resp) => {
|
2023-05-30 06:53:15 -04:00
|
|
|
const $resp = $(resp);
|
|
|
|
// the response is a full HTML page, we need to extract the relevant contents:
|
|
|
|
// 1. append the newly loaded file list items to the existing list
|
|
|
|
$('#diff-incomplete').replaceWith($resp.find('#diff-file-boxes').children());
|
|
|
|
// 2. re-execute the script to append the newly loaded items to the JS variables to refresh the DiffFileTree
|
|
|
|
$('body').append($resp.find('script#diff-data-script'));
|
|
|
|
|
2023-01-13 00:50:32 -05:00
|
|
|
onShowMoreFiles();
|
2023-05-30 06:53:15 -04:00
|
|
|
}).always(() => {
|
2023-01-13 00:50:32 -05:00
|
|
|
$target.removeClass('disabled');
|
2023-05-30 06:53:15 -04:00
|
|
|
pageData.diffFileInfo.isLoadingNewData = false;
|
2021-11-21 11:51:08 -05:00
|
|
|
});
|
2022-09-27 01:22:19 -04:00
|
|
|
}
|
|
|
|
|
2023-04-09 09:11:02 -04:00
|
|
|
function initRepoDiffShowMore() {
|
2023-01-13 00:50:32 -05:00
|
|
|
$(document).on('click', 'a#diff-show-more-files', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const $target = $(e.target);
|
2023-05-30 06:53:15 -04:00
|
|
|
const linkLoadMore = $target.attr('data-href');
|
|
|
|
loadMoreFiles(linkLoadMore);
|
2023-01-13 00:50:32 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', 'a.diff-load-button', (e) => {
|
2021-11-21 11:51:08 -05:00
|
|
|
e.preventDefault();
|
|
|
|
const $target = $(e.target);
|
|
|
|
|
|
|
|
if ($target.hasClass('disabled')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$target.addClass('disabled');
|
|
|
|
|
|
|
|
const url = $target.data('href');
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url,
|
|
|
|
}).done((resp) => {
|
|
|
|
if (!resp) {
|
|
|
|
$target.removeClass('disabled');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$target.parent().replaceWith($(resp).find('#diff-file-boxes .diff-file-body .file-body').children());
|
2022-05-07 14:28:10 -04:00
|
|
|
onShowMoreFiles();
|
2021-11-21 11:51:08 -05:00
|
|
|
}).fail(() => {
|
|
|
|
$target.removeClass('disabled');
|
2021-11-09 04:27:25 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-04-09 09:11:02 -04:00
|
|
|
|
|
|
|
export function initRepoDiffView() {
|
2023-04-19 13:05:25 -04:00
|
|
|
initRepoDiffConversationForm();
|
2023-04-09 09:11:02 -04:00
|
|
|
const diffFileList = $('#diff-file-list');
|
|
|
|
if (diffFileList.length === 0) return;
|
|
|
|
initDiffFileTree();
|
2023-07-28 15:18:12 -04:00
|
|
|
initDiffCommitSelect();
|
2023-04-09 09:11:02 -04:00
|
|
|
initRepoDiffShowMore();
|
|
|
|
initRepoDiffReviewButton();
|
|
|
|
initRepoDiffFileViewToggle();
|
|
|
|
initViewedCheckboxListenerFor();
|
|
|
|
initExpandAndCollapseFilesButton();
|
|
|
|
}
|