mirror of
https://github.com/go-gitea/gitea
synced 2024-11-06 02:04:26 +00:00
0352b99221
Backport #30453 by @silverwind Enable `no-sizzle` lint rule, there was only one use in `initCompReactionSelector` which I have rewritten as follows: - Remove all jQuery except the necessary fomantic dropdown init - Remove the recursion, instead bind event listeners to common parent container nodes Did various tests, works with our without attachments, in diff view and in diff comments inside comment list. Additionally the style of reactions now matches between code comments and issue comments: <img width="275" alt="Screenshot 2024-04-13 at 14 58 10" src="https://github.com/go-gitea/gitea/assets/115237/9d08f188-8661-4dd9-bff4-cad6d6d09cab"> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
223 lines
7.9 KiB
JavaScript
223 lines
7.9 KiB
JavaScript
import $ from 'jquery';
|
|
import {initCompReactionSelector} from './comp/ReactionSelector.js';
|
|
import {initRepoIssueContentHistory} from './repo-issue-content.js';
|
|
import {initDiffFileTree} from './repo-diff-filetree.js';
|
|
import {initDiffCommitSelect} from './repo-diff-commitselect.js';
|
|
import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js';
|
|
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js';
|
|
import {initImageDiff} from './imagediff.js';
|
|
import {showErrorToast} from '../modules/toast.js';
|
|
import {submitEventSubmitter} from '../utils/dom.js';
|
|
import {POST, GET} from '../modules/fetch.js';
|
|
|
|
const {pageData, i18n} = window.config;
|
|
|
|
function initRepoDiffReviewButton() {
|
|
const reviewBox = document.getElementById('review-box');
|
|
if (!reviewBox) return;
|
|
|
|
const $reviewBox = $(reviewBox);
|
|
const counter = reviewBox.querySelector('.review-comments-counter');
|
|
if (!counter) return;
|
|
|
|
$(document).on('click', 'button[name="pending_review"]', (e) => {
|
|
const $form = $(e.target).closest('form');
|
|
// Watch for the form's submit event.
|
|
$form.on('submit', () => {
|
|
const num = parseInt(counter.getAttribute('data-pending-comment-number')) + 1 || 1;
|
|
counter.setAttribute('data-pending-comment-number', num);
|
|
counter.textContent = 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');
|
|
});
|
|
});
|
|
}
|
|
|
|
function initRepoDiffFileViewToggle() {
|
|
$('.file-view-toggle').on('click', function () {
|
|
const $this = $(this);
|
|
$this.parent().children().removeClass('active');
|
|
$this.addClass('active');
|
|
|
|
const $target = $($this.data('toggle-selector'));
|
|
$target.parent().children().addClass('tw-hidden');
|
|
$target.removeClass('tw-hidden');
|
|
});
|
|
}
|
|
|
|
function initRepoDiffConversationForm() {
|
|
$(document).on('submit', '.conversation-holder form', async (e) => {
|
|
e.preventDefault();
|
|
|
|
const $form = $(e.target);
|
|
const textArea = e.target.querySelector('textarea');
|
|
if (!validateTextareaNonEmpty(textArea)) {
|
|
return;
|
|
}
|
|
|
|
if ($form.hasClass('is-loading')) return;
|
|
try {
|
|
$form.addClass('is-loading');
|
|
const formData = new FormData($form[0]);
|
|
|
|
// if the form is submitted by a button, append the button's name and value to the form data
|
|
const submitter = submitEventSubmitter(e);
|
|
const isSubmittedByButton = (submitter?.nodeName === 'BUTTON') || (submitter?.nodeName === 'INPUT' && submitter.type === 'submit');
|
|
if (isSubmittedByButton && submitter.name) {
|
|
formData.append(submitter.name, submitter.value);
|
|
}
|
|
|
|
const response = await POST(e.target.getAttribute('action'), {data: formData});
|
|
const $newConversationHolder = $(await response.text());
|
|
const {path, side, idx} = $newConversationHolder.data();
|
|
|
|
$form.closest('.conversation-holder').replaceWith($newConversationHolder);
|
|
if ($form.closest('tr').data('line-type') === 'same') {
|
|
$(`[data-path="${path}"] .add-code-comment[data-idx="${idx}"]`).addClass('tw-invisible');
|
|
} else {
|
|
$(`[data-path="${path}"] .add-code-comment[data-side="${side}"][data-idx="${idx}"]`).addClass('tw-invisible');
|
|
}
|
|
$newConversationHolder.find('.dropdown').dropdown();
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
showErrorToast(i18n.network_error);
|
|
} finally {
|
|
$form.removeClass('is-loading');
|
|
}
|
|
});
|
|
|
|
$(document).on('click', '.resolve-conversation', async function (e) {
|
|
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');
|
|
|
|
try {
|
|
const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id})});
|
|
const data = await response.text();
|
|
|
|
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();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
}
|
|
});
|
|
}
|
|
|
|
export function initRepoDiffConversationNav() {
|
|
// Previous/Next code review conversation
|
|
$(document).on('click', '.previous-conversation', (e) => {
|
|
const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
|
|
const $conversations = $('.comment-code-cloud:not(.tw-hidden)');
|
|
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()[0].getAttribute('id');
|
|
window.location.href = `#${anchor}`;
|
|
});
|
|
$(document).on('click', '.next-conversation', (e) => {
|
|
const $conversation = $(e.currentTarget).closest('.comment-code-cloud');
|
|
const $conversations = $('.comment-code-cloud:not(.tw-hidden)');
|
|
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()[0].getAttribute('id');
|
|
window.location.href = `#${anchor}`;
|
|
});
|
|
}
|
|
|
|
// Will be called when the show more (files) button has been pressed
|
|
function onShowMoreFiles() {
|
|
initRepoIssueContentHistory();
|
|
initViewedCheckboxListenerFor();
|
|
countAndUpdateViewedFiles();
|
|
initImageDiff();
|
|
}
|
|
|
|
export async function loadMoreFiles(url) {
|
|
const $target = $('a#diff-show-more-files');
|
|
if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
|
|
return;
|
|
}
|
|
|
|
pageData.diffFileInfo.isLoadingNewData = true;
|
|
$target.addClass('disabled');
|
|
|
|
try {
|
|
const response = await GET(url);
|
|
const resp = await response.text();
|
|
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'));
|
|
|
|
onShowMoreFiles();
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
showErrorToast('An error occurred while loading more files.');
|
|
} finally {
|
|
$target.removeClass('disabled');
|
|
pageData.diffFileInfo.isLoadingNewData = false;
|
|
}
|
|
}
|
|
|
|
function initRepoDiffShowMore() {
|
|
$(document).on('click', 'a#diff-show-more-files', (e) => {
|
|
e.preventDefault();
|
|
|
|
const linkLoadMore = e.target.getAttribute('data-href');
|
|
loadMoreFiles(linkLoadMore);
|
|
});
|
|
|
|
$(document).on('click', 'a.diff-load-button', async (e) => {
|
|
e.preventDefault();
|
|
const $target = $(e.target);
|
|
|
|
if ($target.hasClass('disabled')) {
|
|
return;
|
|
}
|
|
|
|
$target.addClass('disabled');
|
|
|
|
const url = $target.data('href');
|
|
|
|
try {
|
|
const response = await GET(url);
|
|
const resp = await response.text();
|
|
|
|
if (!resp) {
|
|
return;
|
|
}
|
|
$target.parent().replaceWith($(resp).find('#diff-file-boxes .diff-file-body .file-body').children());
|
|
onShowMoreFiles();
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
} finally {
|
|
$target.removeClass('disabled');
|
|
}
|
|
});
|
|
}
|
|
|
|
export function initRepoDiffView() {
|
|
initRepoDiffConversationForm();
|
|
if (!$('#diff-file-list').length) return;
|
|
initDiffFileTree();
|
|
initDiffCommitSelect();
|
|
initRepoDiffShowMore();
|
|
initRepoDiffReviewButton();
|
|
initRepoDiffFileViewToggle();
|
|
initViewedCheckboxListenerFor();
|
|
initExpandAndCollapseFilesButton();
|
|
}
|