Skip to content

Commit e0b0187

Browse files
yardenshohamsilverwinddelvh
authored
Remove jQuery .attr from the common issue page functions (#30083)
- Switched from jQuery `attr` to plain javascript `getAttribute` and `setAttribute` - Tested most of the functions and they work as before --------- Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: silverwind <[email protected]> Co-authored-by: delvh <[email protected]>
1 parent 30a561c commit e0b0187

File tree

1 file changed

+83
-73
lines changed

1 file changed

+83
-73
lines changed

web_src/js/features/repo-issue.js

Lines changed: 83 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ export function initRepoIssueTimeTracking() {
4242
}
4343

4444
async function updateDeadline(deadlineString) {
45-
hideElem($('#deadline-err-invalid-date'));
46-
$('#deadline-loader').addClass('is-loading');
45+
hideElem('#deadline-err-invalid-date');
46+
document.getElementById('deadline-loader')?.classList.add('is-loading');
4747

4848
let realDeadline = null;
4949
if (deadlineString !== '') {
5050
const newDate = Date.parse(deadlineString);
5151

5252
if (Number.isNaN(newDate)) {
53-
$('#deadline-loader').removeClass('is-loading');
54-
showElem($('#deadline-err-invalid-date'));
53+
document.getElementById('deadline-loader')?.classList.remove('is-loading');
54+
showElem('#deadline-err-invalid-date');
5555
return false;
5656
}
5757
realDeadline = new Date(newDate);
5858
}
5959

6060
try {
61-
const response = await POST($('#update-issue-deadline-form').attr('action'), {
61+
const response = await POST(document.getElementById('update-issue-deadline-form').getAttribute('action'), {
6262
data: {due_date: realDeadline},
6363
});
6464

@@ -69,8 +69,8 @@ async function updateDeadline(deadlineString) {
6969
}
7070
} catch (error) {
7171
console.error(error);
72-
$('#deadline-loader').removeClass('is-loading');
73-
showElem($('#deadline-err-invalid-date'));
72+
document.getElementById('deadline-loader').classList.remove('is-loading');
73+
showElem('#deadline-err-invalid-date');
7474
}
7575
}
7676

@@ -87,6 +87,19 @@ export function initRepoIssueDue() {
8787
});
8888
}
8989

90+
/**
91+
* @param {HTMLElement} item
92+
*/
93+
function excludeLabel(item) {
94+
const href = item.getAttribute('href');
95+
const id = item.getAttribute('data-label-id');
96+
97+
const regStr = `labels=((?:-?[0-9]+%2c)*)(${id})((?:%2c-?[0-9]+)*)&`;
98+
const newStr = 'labels=$1-$2$3&';
99+
100+
window.location = href.replace(new RegExp(regStr), newStr);
101+
}
102+
90103
export function initRepoIssueSidebarList() {
91104
const repolink = $('#repolink').val();
92105
const repoId = $('#repoId').val();
@@ -123,16 +136,6 @@ export function initRepoIssueSidebarList() {
123136
fullTextSearch: true,
124137
});
125138

126-
function excludeLabel(item) {
127-
const href = $(item).attr('href');
128-
const id = $(item).data('label-id');
129-
130-
const regStr = `labels=((?:-?[0-9]+%2c)*)(${id})((?:%2c-?[0-9]+)*)&`;
131-
const newStr = 'labels=$1-$2$3&';
132-
133-
window.location = href.replace(new RegExp(regStr), newStr);
134-
}
135-
136139
$('.menu a.label-filter-item').each(function () {
137140
$(this).on('click', function (e) {
138141
if (e.altKey) {
@@ -144,9 +147,9 @@ export function initRepoIssueSidebarList() {
144147

145148
$('.menu .ui.dropdown.label-filter').on('keydown', (e) => {
146149
if (e.altKey && e.keyCode === 13) {
147-
const $selectedItems = $('.menu .ui.dropdown.label-filter .menu .item.selected');
148-
if ($selectedItems.length > 0) {
149-
excludeLabel($($selectedItems[0]));
150+
const selectedItem = document.querySelector('.menu .ui.dropdown.label-filter .menu .item.selected');
151+
if (selectedItem) {
152+
excludeLabel(selectedItem);
150153
}
151154
}
152155
});
@@ -166,11 +169,11 @@ export function initRepoIssueCommentDelete() {
166169
const $parentTimelineGroup = $this.closest('.timeline-item-group');
167170
// Check if this was a pending comment.
168171
if ($conversationHolder.find('.pending-label').length) {
169-
const $counter = $('#review-box .review-comments-counter');
170-
let num = parseInt($counter.attr('data-pending-comment-number')) - 1 || 0;
172+
const counter = document.querySelector('#review-box .review-comments-counter');
173+
let num = parseInt(counter?.getAttribute('data-pending-comment-number')) - 1 || 0;
171174
num = Math.max(num, 0);
172-
$counter.attr('data-pending-comment-number', num);
173-
$counter.text(num);
175+
counter.setAttribute('data-pending-comment-number', num);
176+
counter.textContent = String(num);
174177
}
175178

176179
$(`#${$this.data('comment-id')}`).remove();
@@ -279,14 +282,16 @@ export function initRepoPullRequestMergeInstruction() {
279282
}
280283

281284
export function initRepoPullRequestAllowMaintainerEdit() {
282-
const $checkbox = $('#allow-edits-from-maintainers');
283-
if (!$checkbox.length) return;
285+
const checkbox = document.getElementById('allow-edits-from-maintainers');
286+
if (!checkbox) return;
287+
288+
const $checkbox = $(checkbox);
284289

285-
const promptError = $checkbox.attr('data-prompt-error');
290+
const promptError = checkbox.getAttribute('data-prompt-error');
286291
$checkbox.checkbox({
287292
'onChange': async () => {
288293
const checked = $checkbox.checkbox('is checked');
289-
let url = $checkbox.attr('data-url');
294+
let url = checkbox.getAttribute('data-url');
290295
url += '/set_allow_maintainer_edit';
291296
$checkbox.checkbox('set disabled');
292297
try {
@@ -298,7 +303,7 @@ export function initRepoPullRequestAllowMaintainerEdit() {
298303
}
299304
} catch (error) {
300305
console.error(error);
301-
showTemporaryTooltip($checkbox[0], promptError);
306+
showTemporaryTooltip(checkbox, promptError);
302307
} finally {
303308
$checkbox.checkbox('set enabled');
304309
}
@@ -325,7 +330,9 @@ export function initRepoIssueReferenceRepositorySearch() {
325330
},
326331
onChange(_value, _text, $choice) {
327332
const $form = $choice.closest('form');
328-
$form.attr('action', `${appSubUrl}/${_text}/issues/new`);
333+
if (!$form.length) return;
334+
335+
$form[0].setAttribute('action', `${appSubUrl}/${_text}/issues/new`);
329336
},
330337
fullTextSearch: true,
331338
});
@@ -375,17 +382,16 @@ export function initRepoIssueComments() {
375382
window.location.reload();
376383
});
377384

378-
$(document).on('click', (event) => {
379-
const $urlTarget = $(':target');
380-
if (!$urlTarget.length) return;
385+
document.addEventListener('click', (e) => {
386+
const urlTarget = document.querySelector(':target');
387+
if (!urlTarget) return;
381388

382-
const urlTargetId = $urlTarget.attr('id');
389+
const urlTargetId = urlTarget.id;
383390
if (!urlTargetId) return;
384-
if (!/^(issue|pull)(comment)?-\d+$/.test(urlTargetId)) return;
385391

386-
const $target = $(event.target);
392+
if (!/^(issue|pull)(comment)?-\d+$/.test(urlTargetId)) return;
387393

388-
if (!$target.closest(`#${urlTargetId}`).length) {
394+
if (!e.target.closest(`#${urlTargetId}`)) {
389395
const scrollPosition = $(window).scrollTop();
390396
window.location.hash = '';
391397
$(window).scrollTop(scrollPosition);
@@ -419,30 +425,33 @@ export function initRepoPullRequestReview() {
419425
if (window.history.scrollRestoration !== 'manual') {
420426
window.history.scrollRestoration = 'manual';
421427
}
422-
const $commentDiv = $(window.location.hash);
423-
if ($commentDiv) {
428+
const commentDiv = document.querySelector(window.location.hash);
429+
if (commentDiv) {
424430
// get the name of the parent id
425-
const groupID = $commentDiv.closest('div[id^="code-comments-"]').attr('id');
431+
const groupID = commentDiv.closest('div[id^="code-comments-"]')?.getAttribute('id');
426432
if (groupID && groupID.startsWith('code-comments-')) {
427433
const id = groupID.slice(14);
428-
const $ancestorDiffBox = $commentDiv.closest('.diff-file-box');
434+
const ancestorDiffBox = commentDiv.closest('.diff-file-box');
429435
// on pages like conversation, there is no diff header
430-
const $diffHeader = $ancestorDiffBox.find('.diff-file-header');
436+
const diffHeader = ancestorDiffBox?.querySelector('.diff-file-header');
437+
431438
// offset is for scrolling
432439
let offset = 30;
433-
if ($diffHeader[0]) {
434-
offset += $('.diff-detail-box').outerHeight() + $diffHeader.outerHeight();
440+
if (diffHeader) {
441+
offset += $('.diff-detail-box').outerHeight() + $(diffHeader).outerHeight();
435442
}
436-
$(`#show-outdated-${id}`).addClass('tw-hidden');
437-
$(`#code-comments-${id}`).removeClass('tw-hidden');
438-
$(`#code-preview-${id}`).removeClass('tw-hidden');
439-
$(`#hide-outdated-${id}`).removeClass('tw-hidden');
443+
444+
document.getElementById(`show-outdated-${id}`).classList.add('tw-hidden');
445+
document.getElementById(`code-comments-${id}`).classList.remove('tw-hidden');
446+
document.getElementById(`code-preview-${id}`).classList.remove('tw-hidden');
447+
document.getElementById(`hide-outdated-${id}`).classList.remove('tw-hidden');
440448
// if the comment box is folded, expand it
441-
if ($ancestorDiffBox.attr('data-folded') && $ancestorDiffBox.attr('data-folded') === 'true') {
442-
setFileFolding($ancestorDiffBox[0], $ancestorDiffBox.find('.fold-file')[0], false);
449+
if (ancestorDiffBox.getAttribute('data-folded') === 'true') {
450+
setFileFolding(ancestorDiffBox, ancestorDiffBox.querySelector('.fold-file'), false);
443451
}
452+
444453
window.scrollTo({
445-
top: $commentDiv.offset().top - offset,
454+
top: $(commentDiv).offset().top - offset,
446455
behavior: 'instant',
447456
});
448457
}
@@ -529,7 +538,7 @@ export function initRepoPullRequestReview() {
529538
const $commentCloud = $td.find('.comment-code-cloud');
530539
if (!$commentCloud.length && !$ntr.find('button[name="pending_review"]').length) {
531540
try {
532-
const response = await GET($(this).closest('[data-new-comment-url]').attr('data-new-comment-url'));
541+
const response = await GET(this.closest('[data-new-comment-url]')?.getAttribute('data-new-comment-url'));
533542
const html = await response.text();
534543
$td.html(html);
535544
$td.find("input[name='line']").val(idx);
@@ -585,6 +594,22 @@ export function initRepoIssueWipToggle() {
585594
});
586595
}
587596

597+
async function pullrequest_targetbranch_change(update_url) {
598+
const targetBranch = $('#pull-target-branch').data('branch');
599+
const $branchTarget = $('#branch_target');
600+
if (targetBranch === $branchTarget.text()) {
601+
window.location.reload();
602+
return false;
603+
}
604+
try {
605+
await POST(update_url, {data: new URLSearchParams({target_branch: targetBranch})});
606+
} catch (error) {
607+
console.error(error);
608+
} finally {
609+
window.location.reload();
610+
}
611+
}
612+
588613
export function initRepoIssueTitleEdit() {
589614
// Edit issue title
590615
const $issueTitle = $('#issue-title');
@@ -607,31 +632,15 @@ export function initRepoIssueTitleEdit() {
607632
$('#edit-title').on('click', editTitleToggle);
608633
$('#cancel-edit-title').on('click', editTitleToggle);
609634
$('#save-edit-title').on('click', editTitleToggle).on('click', async function () {
610-
const pullrequest_targetbranch_change = async function (update_url) {
611-
const targetBranch = $('#pull-target-branch').data('branch');
612-
const $branchTarget = $('#branch_target');
613-
if (targetBranch === $branchTarget.text()) {
614-
window.location.reload();
615-
return false;
616-
}
617-
try {
618-
await POST(update_url, {data: new URLSearchParams({target_branch: targetBranch})});
619-
} catch (error) {
620-
console.error(error);
621-
} finally {
622-
window.location.reload();
623-
}
624-
};
625-
626-
const pullrequest_target_update_url = $(this).attr('data-target-update-url');
635+
const pullrequest_target_update_url = this.getAttribute('data-target-update-url');
627636
if (!$editInput.val().length || $editInput.val() === $issueTitle.text()) {
628637
$editInput.val($issueTitle.text());
629638
await pullrequest_targetbranch_change(pullrequest_target_update_url);
630639
} else {
631640
try {
632641
const params = new URLSearchParams();
633642
params.append('title', $editInput.val());
634-
const response = await POST($(this).attr('data-update-url'), {data: params});
643+
const response = await POST(this.getAttribute('data-update-url'), {data: params});
635644
const data = await response.json();
636645
$editInput.val(data.title);
637646
$issueTitle.text(data.title);
@@ -671,10 +680,11 @@ export function initSingleCommentEditor($commentForm) {
671680
// * normal new issue/pr page, no status-button
672681
// * issue/pr view page, with comment form, has status-button
673682
const opts = {};
674-
const $statusButton = $('#status-button');
675-
if ($statusButton.length) {
683+
const statusButton = document.getElementById('status-button');
684+
if (statusButton) {
676685
opts.onContentChanged = (editor) => {
677-
$statusButton.text($statusButton.attr(editor.value().trim() ? 'data-status-and-comment' : 'data-status'));
686+
const statusText = statusButton.getAttribute(editor.value().trim() ? 'data-status-and-comment' : 'data-status');
687+
statusButton.textContent = statusText;
678688
};
679689
}
680690
initComboMarkdownEditor($commentForm.find('.combo-markdown-editor'), opts);

0 commit comments

Comments
 (0)