2022-01-29 06:00:11 +09:00
|
|
|
import $ from 'jquery';
|
2021-10-17 02:28:04 +09:00
|
|
|
import {initCompReactionSelector} from './comp/ReactionSelector.js';
|
2021-12-06 12:57:51 +09:00
|
|
|
import {initRepoIssueContentHistory} from './repo-issue-content.js';
|
2022-01-05 21:17:25 +09:00
|
|
|
import {validateTextareaNonEmpty} from './comp/EasyMDE.js';
|
2022-05-08 03:28:10 +09:00
|
|
|
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles} from './pull-view-file.js';
|
2022-08-09 21:37:34 +09:00
|
|
|
import {initTooltip} from '../modules/tippy.js';
|
2022-01-29 06:00:11 +09:00
|
|
|
|
2021-10-21 16:37:43 +09:00
|
|
|
const {csrfToken} = window.config;
|
2021-10-17 02:28:04 +09:00
|
|
|
|
|
|
|
export function initRepoDiffReviewButton() {
|
2022-05-07 14:35:12 +09:00
|
|
|
const $reviewBox = $('#review-box');
|
|
|
|
const $counter = $reviewBox.find('.review-comments-counter');
|
|
|
|
|
2023-03-04 16:13:37 +09:00
|
|
|
$(document).on('click', 'button[name="pending_review"]', (e) => {
|
2022-05-07 14:35:12 +09: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-17 02:28:04 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoDiffFileViewToggle() {
|
|
|
|
$('.file-view-toggle').on('click', function () {
|
|
|
|
const $this = $(this);
|
|
|
|
$this.parent().children().removeClass('active');
|
|
|
|
$this.addClass('active');
|
|
|
|
|
2022-01-16 20:19:26 +09:00
|
|
|
const $target = $($this.data('toggle-selector'));
|
2023-02-19 13:06:14 +09:00
|
|
|
$target.parent().children().addClass('gt-hidden');
|
|
|
|
$target.removeClass('gt-hidden');
|
2021-10-17 02:28:04 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoDiffConversationForm() {
|
2021-10-17 05:30:31 +09:00
|
|
|
$(document).on('submit', '.conversation-holder form', async (e) => {
|
2021-10-17 02:28:04 +09:00
|
|
|
e.preventDefault();
|
2022-01-03 07:31:03 +09:00
|
|
|
|
2022-08-17 06:05:40 +09:00
|
|
|
const $form = $(e.target);
|
|
|
|
const $textArea = $form.find('textarea');
|
2022-01-04 01:53:53 +09:00
|
|
|
if (!validateTextareaNonEmpty($textArea)) {
|
2022-01-03 07:31:03 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-04 16:13:37 +09:00
|
|
|
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 = e.originalEvent?.submitter;
|
|
|
|
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));
|
2022-08-17 06:05:40 +09:00
|
|
|
const $newConversationHolder = $(await $.post($form.attr('action'), formDataString));
|
|
|
|
const {path, side, idx} = $newConversationHolder.data();
|
2021-10-17 02:28:04 +09:00
|
|
|
|
2022-08-17 06:05:40 +09:00
|
|
|
$newConversationHolder.find('.tooltip').each(function () {
|
|
|
|
initTooltip(this);
|
|
|
|
});
|
|
|
|
|
|
|
|
$form.closest('.conversation-holder').replaceWith($newConversationHolder);
|
|
|
|
if ($form.closest('tr').data('line-type') === 'same') {
|
2021-11-19 11:28:27 +09:00
|
|
|
$(`[data-path="${path}"] a.add-code-comment[data-idx="${idx}"]`).addClass('invisible');
|
2021-10-17 02:28:04 +09:00
|
|
|
} else {
|
2021-11-19 11:28:27 +09:00
|
|
|
$(`[data-path="${path}"] a.add-code-comment[data-side="${side}"][data-idx="${idx}"]`).addClass('invisible');
|
2021-10-17 02:28:04 +09:00
|
|
|
}
|
2022-08-17 06:05:40 +09:00
|
|
|
$newConversationHolder.find('.dropdown').dropdown();
|
|
|
|
initCompReactionSelector($newConversationHolder);
|
2021-10-17 02:28:04 +09:00
|
|
|
});
|
|
|
|
|
2022-01-19 02:28:38 +09:00
|
|
|
$(document).on('click', '.resolve-conversation', async function (e) {
|
2021-10-17 02:28:04 +09: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 16:37:43 +09:00
|
|
|
const data = await $.post(url, {_csrf: csrfToken, origin, action, comment_id});
|
2021-10-17 02:28:04 +09: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-19 13:06:14 +09:00
|
|
|
const $conversations = $('.comment-code-cloud:not(.gt-hidden)');
|
2021-10-17 02:28:04 +09: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-19 13:06:14 +09:00
|
|
|
const $conversations = $('.comment-code-cloud:not(.gt-hidden)');
|
2021-10-17 02:28:04 +09: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 18:27:25 +09:00
|
|
|
|
2022-05-08 03:28:10 +09:00
|
|
|
// Will be called when the show more (files) button has been pressed
|
|
|
|
function onShowMoreFiles() {
|
|
|
|
initRepoIssueContentHistory();
|
|
|
|
initViewedCheckboxListenerFor();
|
|
|
|
countAndUpdateViewedFiles();
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:22:19 +09:00
|
|
|
export function doLoadMoreFiles(link, diffEnd, callback) {
|
|
|
|
const url = `${link}?skip-to=${diffEnd}&file-only=true`;
|
2023-01-13 14:50:32 +09:00
|
|
|
loadMoreFiles(url, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadMoreFiles(url, callback) {
|
|
|
|
const $target = $('a#diff-show-more-files');
|
|
|
|
if ($target.hasClass('disabled')) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$target.addClass('disabled');
|
2022-09-27 14:22:19 +09:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url,
|
|
|
|
}).done((resp) => {
|
|
|
|
if (!resp) {
|
2023-01-13 14:50:32 +09:00
|
|
|
$target.removeClass('disabled');
|
2022-09-27 14:22:19 +09:00
|
|
|
callback(resp);
|
2021-11-09 18:27:25 +09:00
|
|
|
return;
|
|
|
|
}
|
2023-01-13 14:50:32 +09:00
|
|
|
$('#diff-incomplete').replaceWith($(resp).find('#diff-file-boxes').children());
|
2022-09-27 14:22:19 +09:00
|
|
|
// By simply rerunning the script we add the new data to our existing
|
|
|
|
// pagedata object. this triggers vue and the filetree and filelist will
|
|
|
|
// render the new elements.
|
|
|
|
$('body').append($(resp).find('script#diff-data-script'));
|
2023-01-13 14:50:32 +09:00
|
|
|
onShowMoreFiles();
|
2022-09-27 14:22:19 +09:00
|
|
|
callback(resp);
|
|
|
|
}).fail(() => {
|
2023-01-13 14:50:32 +09:00
|
|
|
$target.removeClass('disabled');
|
2022-09-27 14:22:19 +09:00
|
|
|
callback();
|
2021-11-22 01:51:08 +09:00
|
|
|
});
|
2022-09-27 14:22:19 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoDiffShowMore() {
|
2023-01-13 14:50:32 +09:00
|
|
|
$(document).on('click', 'a#diff-show-more-files', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const $target = $(e.target);
|
|
|
|
loadMoreFiles($target.data('href'), () => {});
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', 'a.diff-load-button', (e) => {
|
2021-11-22 01:51:08 +09: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-08 03:28:10 +09:00
|
|
|
onShowMoreFiles();
|
2021-11-22 01:51:08 +09:00
|
|
|
}).fail(() => {
|
|
|
|
$target.removeClass('disabled');
|
2021-11-09 18:27:25 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|