2022-01-29 02:30:11 +05:30
|
|
|
import $ from 'jquery';
|
2021-10-16 22:58:04 +05:30
|
|
|
import {initCompReactionSelector} from './comp/ReactionSelector.js';
|
2021-12-06 09:27:51 +05:30
|
|
|
import {initRepoIssueContentHistory} from './repo-issue-content.js';
|
2023-04-09 18:41:02 +05:30
|
|
|
import {initDiffFileTree} from './repo-diff-filetree.js';
|
2023-07-29 00:48:12 +05:30
|
|
|
import {initDiffCommitSelect} from './repo-diff-commitselect.js';
|
2023-04-03 15:36:57 +05:30
|
|
|
import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js';
|
2023-04-09 18:41:02 +05:30
|
|
|
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js';
|
2023-07-04 14:06:27 +05:30
|
|
|
import {initImageDiff} from './imagediff.js';
|
2023-11-21 07:42:31 +05:30
|
|
|
import {showErrorToast} from '../modules/toast.js';
|
2023-12-15 04:56:36 +05:30
|
|
|
import {submitEventSubmitter} from '../utils/dom.js';
|
2024-03-15 03:51:14 +05:30
|
|
|
import {POST, GET} from '../modules/fetch.js';
|
2022-01-29 02:30:11 +05:30
|
|
|
|
2024-03-15 03:51:14 +05:30
|
|
|
const {pageData, i18n} = window.config;
|
2021-10-16 22:58:04 +05:30
|
|
|
|
2023-04-09 18:41:02 +05:30
|
|
|
function initRepoDiffReviewButton() {
|
2024-03-23 18:07:18 +05:30
|
|
|
const reviewBox = document.getElementById('review-box');
|
|
|
|
if (!reviewBox) return;
|
|
|
|
|
|
|
|
const $reviewBox = $(reviewBox);
|
|
|
|
const counter = reviewBox.querySelector('.review-comments-counter');
|
|
|
|
if (!counter) return;
|
2022-05-07 11:05:12 +05:30
|
|
|
|
2023-03-04 12:43:37 +05:30
|
|
|
$(document).on('click', 'button[name="pending_review"]', (e) => {
|
2022-05-07 11:05:12 +05:30
|
|
|
const $form = $(e.target).closest('form');
|
|
|
|
// Watch for the form's submit event.
|
|
|
|
$form.on('submit', () => {
|
2024-03-23 18:07:18 +05:30
|
|
|
const num = parseInt(counter.getAttribute('data-pending-comment-number')) + 1 || 1;
|
|
|
|
counter.setAttribute('data-pending-comment-number', num);
|
|
|
|
counter.textContent = num;
|
2022-05-07 11:05:12 +05:30
|
|
|
// 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 22:58:04 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-09 18:41:02 +05:30
|
|
|
function initRepoDiffFileViewToggle() {
|
2021-10-16 22:58:04 +05:30
|
|
|
$('.file-view-toggle').on('click', function () {
|
|
|
|
const $this = $(this);
|
|
|
|
$this.parent().children().removeClass('active');
|
|
|
|
$this.addClass('active');
|
|
|
|
|
2022-01-16 16:49:26 +05:30
|
|
|
const $target = $($this.data('toggle-selector'));
|
2024-03-24 23:53:38 +05:30
|
|
|
$target.parent().children().addClass('tw-hidden');
|
|
|
|
$target.removeClass('tw-hidden');
|
2021-10-16 22:58:04 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-09 18:41:02 +05:30
|
|
|
function initRepoDiffConversationForm() {
|
2021-10-17 02:00:31 +05:30
|
|
|
$(document).on('submit', '.conversation-holder form', async (e) => {
|
2021-10-16 22:58:04 +05:30
|
|
|
e.preventDefault();
|
2022-01-03 04:01:03 +05:30
|
|
|
|
2022-08-17 02:35:40 +05:30
|
|
|
const $form = $(e.target);
|
2024-02-20 16:07:37 +05:30
|
|
|
const textArea = e.target.querySelector('textarea');
|
|
|
|
if (!validateTextareaNonEmpty(textArea)) {
|
2022-01-03 04:01:03 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-21 07:42:31 +05:30
|
|
|
if ($form.hasClass('is-loading')) return;
|
|
|
|
try {
|
|
|
|
$form.addClass('is-loading');
|
|
|
|
const formData = new FormData($form[0]);
|
2023-03-04 12:43:37 +05:30
|
|
|
|
2024-02-17 17:22:11 +05:30
|
|
|
// 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-21 07:42:31 +05:30
|
|
|
const isSubmittedByButton = (submitter?.nodeName === 'BUTTON') || (submitter?.nodeName === 'INPUT' && submitter.type === 'submit');
|
|
|
|
if (isSubmittedByButton && submitter.name) {
|
|
|
|
formData.append(submitter.name, submitter.value);
|
|
|
|
}
|
2024-03-15 03:51:14 +05:30
|
|
|
|
2024-03-23 18:07:18 +05:30
|
|
|
const response = await POST(e.target.getAttribute('action'), {data: formData});
|
2024-03-15 03:51:14 +05:30
|
|
|
const $newConversationHolder = $(await response.text());
|
2023-11-21 07:42:31 +05:30
|
|
|
const {path, side, idx} = $newConversationHolder.data();
|
2021-10-16 22:58:04 +05:30
|
|
|
|
2023-11-21 07:42:31 +05:30
|
|
|
$form.closest('.conversation-holder').replaceWith($newConversationHolder);
|
|
|
|
if ($form.closest('tr').data('line-type') === 'same') {
|
2024-03-05 10:59:32 +05:30
|
|
|
$(`[data-path="${path}"] .add-code-comment[data-idx="${idx}"]`).addClass('tw-invisible');
|
2023-11-21 07:42:31 +05:30
|
|
|
} else {
|
2024-03-05 10:59:32 +05:30
|
|
|
$(`[data-path="${path}"] .add-code-comment[data-side="${side}"][data-idx="${idx}"]`).addClass('tw-invisible');
|
2023-11-21 07:42:31 +05:30
|
|
|
}
|
|
|
|
$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 17:22:11 +05:30
|
|
|
console.error('error when submitting conversation', e);
|
2023-11-21 07:42:31 +05:30
|
|
|
showErrorToast(i18n.network_error);
|
|
|
|
} finally {
|
|
|
|
$form.removeClass('is-loading');
|
2021-10-16 22:58:04 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-01-18 22:58:38 +05:30
|
|
|
$(document).on('click', '.resolve-conversation', async function (e) {
|
2021-10-16 22:58:04 +05:30
|
|
|
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');
|
|
|
|
|
2024-03-15 03:51:14 +05:30
|
|
|
try {
|
|
|
|
const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id})});
|
|
|
|
const data = await response.text();
|
|
|
|
|
|
|
|
if ($(this).closest('.conversation-holder').length) {
|
2024-03-16 17:52:16 +05:30
|
|
|
const $conversation = $(data);
|
|
|
|
$(this).closest('.conversation-holder').replaceWith($conversation);
|
|
|
|
$conversation.find('.dropdown').dropdown();
|
|
|
|
initCompReactionSelector($conversation);
|
2024-03-15 03:51:14 +05:30
|
|
|
} else {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error:', error);
|
2021-10-16 22:58:04 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoDiffConversationNav() {
|
|
|
|
// Previous/Next code review conversation
|
|
|
|
$(document).on('click', '.previous-conversation', (e) => {
|
|
|
|
const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
|
2024-03-24 23:53:38 +05:30
|
|
|
const $conversations = $('.comment-code-cloud:not(.tw-hidden)');
|
2021-10-16 22:58:04 +05:30
|
|
|
const index = $conversations.index($conversation);
|
|
|
|
const previousIndex = index > 0 ? index - 1 : $conversations.length - 1;
|
|
|
|
const $previousConversation = $conversations.eq(previousIndex);
|
2024-03-23 18:07:18 +05:30
|
|
|
const anchor = $previousConversation.find('.comment').first()[0].getAttribute('id');
|
2021-10-16 22:58:04 +05:30
|
|
|
window.location.href = `#${anchor}`;
|
|
|
|
});
|
|
|
|
$(document).on('click', '.next-conversation', (e) => {
|
|
|
|
const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
|
2024-03-24 23:53:38 +05:30
|
|
|
const $conversations = $('.comment-code-cloud:not(.tw-hidden)');
|
2021-10-16 22:58:04 +05:30
|
|
|
const index = $conversations.index($conversation);
|
|
|
|
const nextIndex = index < $conversations.length - 1 ? index + 1 : 0;
|
|
|
|
const $nextConversation = $conversations.eq(nextIndex);
|
2024-03-23 18:07:18 +05:30
|
|
|
const anchor = $nextConversation.find('.comment').first()[0].getAttribute('id');
|
2021-10-16 22:58:04 +05:30
|
|
|
window.location.href = `#${anchor}`;
|
|
|
|
});
|
|
|
|
}
|
2021-11-09 14:57:25 +05:30
|
|
|
|
2022-05-07 23:58:10 +05:30
|
|
|
// Will be called when the show more (files) button has been pressed
|
|
|
|
function onShowMoreFiles() {
|
|
|
|
initRepoIssueContentHistory();
|
|
|
|
initViewedCheckboxListenerFor();
|
|
|
|
countAndUpdateViewedFiles();
|
2023-07-04 14:06:27 +05:30
|
|
|
initImageDiff();
|
2022-05-07 23:58:10 +05:30
|
|
|
}
|
|
|
|
|
2024-03-15 03:51:14 +05:30
|
|
|
export async function loadMoreFiles(url) {
|
2023-01-13 11:20:32 +05:30
|
|
|
const $target = $('a#diff-show-more-files');
|
2023-05-30 16:23:15 +05:30
|
|
|
if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
|
2023-01-13 11:20:32 +05:30
|
|
|
return;
|
|
|
|
}
|
2023-05-30 16:23:15 +05:30
|
|
|
|
|
|
|
pageData.diffFileInfo.isLoadingNewData = true;
|
2023-01-13 11:20:32 +05:30
|
|
|
$target.addClass('disabled');
|
2024-03-15 03:51:14 +05:30
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await GET(url);
|
|
|
|
const resp = await response.text();
|
2023-05-30 16:23:15 +05:30
|
|
|
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 11:20:32 +05:30
|
|
|
onShowMoreFiles();
|
2024-03-15 03:51:14 +05:30
|
|
|
} catch (error) {
|
|
|
|
console.error('Error:', error);
|
|
|
|
showErrorToast('An error occurred while loading more files.');
|
|
|
|
} finally {
|
2023-01-13 11:20:32 +05:30
|
|
|
$target.removeClass('disabled');
|
2023-05-30 16:23:15 +05:30
|
|
|
pageData.diffFileInfo.isLoadingNewData = false;
|
2024-03-15 03:51:14 +05:30
|
|
|
}
|
2022-09-27 10:52:19 +05:30
|
|
|
}
|
|
|
|
|
2023-04-09 18:41:02 +05:30
|
|
|
function initRepoDiffShowMore() {
|
2023-01-13 11:20:32 +05:30
|
|
|
$(document).on('click', 'a#diff-show-more-files', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2024-03-23 18:07:18 +05:30
|
|
|
const linkLoadMore = e.target.getAttribute('data-href');
|
2023-05-30 16:23:15 +05:30
|
|
|
loadMoreFiles(linkLoadMore);
|
2023-01-13 11:20:32 +05:30
|
|
|
});
|
|
|
|
|
2024-03-15 03:51:14 +05:30
|
|
|
$(document).on('click', 'a.diff-load-button', async (e) => {
|
2021-11-21 22:21:08 +05:30
|
|
|
e.preventDefault();
|
|
|
|
const $target = $(e.target);
|
|
|
|
|
|
|
|
if ($target.hasClass('disabled')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$target.addClass('disabled');
|
|
|
|
|
|
|
|
const url = $target.data('href');
|
2024-03-15 03:51:14 +05:30
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await GET(url);
|
|
|
|
const resp = await response.text();
|
|
|
|
|
2021-11-21 22:21:08 +05:30
|
|
|
if (!resp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$target.parent().replaceWith($(resp).find('#diff-file-boxes .diff-file-body .file-body').children());
|
2022-05-07 23:58:10 +05:30
|
|
|
onShowMoreFiles();
|
2024-03-15 03:51:14 +05:30
|
|
|
} catch (error) {
|
|
|
|
console.error('Error:', error);
|
|
|
|
} finally {
|
2021-11-21 22:21:08 +05:30
|
|
|
$target.removeClass('disabled');
|
2024-03-15 03:51:14 +05:30
|
|
|
}
|
2021-11-09 14:57:25 +05:30
|
|
|
});
|
|
|
|
}
|
2023-04-09 18:41:02 +05:30
|
|
|
|
|
|
|
export function initRepoDiffView() {
|
2023-04-19 22:35:25 +05:30
|
|
|
initRepoDiffConversationForm();
|
2024-03-16 17:52:16 +05:30
|
|
|
const $diffFileList = $('#diff-file-list');
|
|
|
|
if ($diffFileList.length === 0) return;
|
2023-04-09 18:41:02 +05:30
|
|
|
initDiffFileTree();
|
2023-07-29 00:48:12 +05:30
|
|
|
initDiffCommitSelect();
|
2023-04-09 18:41:02 +05:30
|
|
|
initRepoDiffShowMore();
|
|
|
|
initRepoDiffReviewButton();
|
|
|
|
initRepoDiffFileViewToggle();
|
|
|
|
initViewedCheckboxListenerFor();
|
|
|
|
initExpandAndCollapseFilesButton();
|
|
|
|
}
|