Skip to content

Commit c487a32

Browse files
Remove jQuery class from the diff view (#30176)
- Switched from jQuery class functions to plain JavaScript `classList` - Tested the diff view functionality and it works as before --------- Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: silverwind <[email protected]>
1 parent 56ac5f1 commit c487a32

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

web_src/js/features/repo-diff.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js';
77
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js';
88
import {initImageDiff} from './imagediff.js';
99
import {showErrorToast} from '../modules/toast.js';
10-
import {submitEventSubmitter} from '../utils/dom.js';
10+
import {submitEventSubmitter, queryElemSiblings, hideElem, showElem} from '../utils/dom.js';
1111
import {POST, GET} from '../modules/fetch.js';
1212

1313
const {pageData, i18n} = window.config;
@@ -16,7 +16,6 @@ function initRepoDiffReviewButton() {
1616
const reviewBox = document.getElementById('review-box');
1717
if (!reviewBox) return;
1818

19-
const $reviewBox = $(reviewBox);
2019
const counter = reviewBox.querySelector('.review-comments-counter');
2120
if (!counter) return;
2221

@@ -27,23 +26,27 @@ function initRepoDiffReviewButton() {
2726
const num = parseInt(counter.getAttribute('data-pending-comment-number')) + 1 || 1;
2827
counter.setAttribute('data-pending-comment-number', num);
2928
counter.textContent = num;
30-
// Force the browser to reflow the DOM. This is to ensure that the browser replay the animation
31-
$reviewBox.removeClass('pulse');
32-
$reviewBox.width();
33-
$reviewBox.addClass('pulse');
29+
30+
reviewBox.classList.remove('pulse');
31+
requestAnimationFrame(() => {
32+
reviewBox.classList.add('pulse');
33+
});
3434
});
3535
});
3636
}
3737

3838
function initRepoDiffFileViewToggle() {
3939
$('.file-view-toggle').on('click', function () {
40-
const $this = $(this);
41-
$this.parent().children().removeClass('active');
42-
$this.addClass('active');
40+
for (const el of queryElemSiblings(this)) {
41+
el.classList.remove('active');
42+
}
43+
this.classList.add('active');
4344

44-
const $target = $($this.data('toggle-selector'));
45-
$target.parent().children().addClass('tw-hidden');
46-
$target.removeClass('tw-hidden');
45+
const target = document.querySelector(this.getAttribute('data-toggle-selector'));
46+
if (!target) return;
47+
48+
hideElem(queryElemSiblings(target));
49+
showElem(target);
4750
});
4851
}
4952

@@ -57,9 +60,9 @@ function initRepoDiffConversationForm() {
5760
return;
5861
}
5962

60-
if ($form.hasClass('is-loading')) return;
63+
if (e.target.classList.contains('is-loading')) return;
6164
try {
62-
$form.addClass('is-loading');
65+
e.target.classList.add('is-loading');
6366
const formData = new FormData($form[0]);
6467

6568
// if the form is submitted by a button, append the button's name and value to the form data
@@ -74,18 +77,22 @@ function initRepoDiffConversationForm() {
7477
const {path, side, idx} = $newConversationHolder.data();
7578

7679
$form.closest('.conversation-holder').replaceWith($newConversationHolder);
80+
let selector;
7781
if ($form.closest('tr').data('line-type') === 'same') {
78-
$(`[data-path="${path}"] .add-code-comment[data-idx="${idx}"]`).addClass('tw-invisible');
82+
selector = `[data-path="${path}"] .add-code-comment[data-idx="${idx}"]`;
7983
} else {
80-
$(`[data-path="${path}"] .add-code-comment[data-side="${side}"][data-idx="${idx}"]`).addClass('tw-invisible');
84+
selector = `[data-path="${path}"] .add-code-comment[data-side="${side}"][data-idx="${idx}"]`;
85+
}
86+
for (const el of document.querySelectorAll(selector)) {
87+
el.classList.add('tw-invisible');
8188
}
8289
$newConversationHolder.find('.dropdown').dropdown();
8390
initCompReactionSelector($newConversationHolder);
8491
} catch (error) {
8592
console.error('Error:', error);
8693
showErrorToast(i18n.network_error);
8794
} finally {
88-
$form.removeClass('is-loading');
95+
e.target.classList.remove('is-loading');
8996
}
9097
});
9198

@@ -145,13 +152,13 @@ function onShowMoreFiles() {
145152
}
146153

147154
export async function loadMoreFiles(url) {
148-
const $target = $('a#diff-show-more-files');
149-
if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
155+
const target = document.querySelector('a#diff-show-more-files');
156+
if (target?.classList.contains('disabled') || pageData.diffFileInfo.isLoadingNewData) {
150157
return;
151158
}
152159

153160
pageData.diffFileInfo.isLoadingNewData = true;
154-
$target.addClass('disabled');
161+
target?.classList.add('disabled');
155162

156163
try {
157164
const response = await GET(url);
@@ -168,7 +175,7 @@ export async function loadMoreFiles(url) {
168175
console.error('Error:', error);
169176
showErrorToast('An error occurred while loading more files.');
170177
} finally {
171-
$target.removeClass('disabled');
178+
target?.classList.remove('disabled');
172179
pageData.diffFileInfo.isLoadingNewData = false;
173180
}
174181
}
@@ -185,11 +192,11 @@ function initRepoDiffShowMore() {
185192
e.preventDefault();
186193
const $target = $(e.target);
187194

188-
if ($target.hasClass('disabled')) {
195+
if (e.target.classList.contains('disabled')) {
189196
return;
190197
}
191198

192-
$target.addClass('disabled');
199+
e.target.classList.add('disabled');
193200

194201
const url = $target.data('href');
195202

@@ -205,7 +212,7 @@ function initRepoDiffShowMore() {
205212
} catch (error) {
206213
console.error('Error:', error);
207214
} finally {
208-
$target.removeClass('disabled');
215+
e.target.classList.remove('disabled');
209216
}
210217
});
211218
}

web_src/js/utils/dom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function isElemHidden(el) {
5151
return res[0];
5252
}
5353

54-
export function queryElemSiblings(el, selector) {
54+
export function queryElemSiblings(el, selector = '*') {
5555
return Array.from(el.parentNode.children).filter((child) => child !== el && child.matches(selector));
5656
}
5757

0 commit comments

Comments
 (0)