Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove jQuery .attr from the common issue page functions #30083

Merged
merged 6 commits into from
Mar 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 62 additions & 51 deletions web_src/js/features/repo-issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ export function initRepoIssueTimeTracking() {
}

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

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

if (Number.isNaN(newDate)) {
$('#deadline-loader').removeClass('is-loading');
showElem($('#deadline-err-invalid-date'));
document.getElementById('deadline-loader')?.classList.remove('is-loading');
showElem('#deadline-err-invalid-date');
return false;
}
realDeadline = new Date(newDate);
}

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

Expand All @@ -69,8 +69,8 @@ async function updateDeadline(deadlineString) {
}
} catch (error) {
console.error(error);
$('#deadline-loader').removeClass('is-loading');
showElem($('#deadline-err-invalid-date'));
document.getElementById('deadline-loader').classList.remove('is-loading');
showElem('#deadline-err-invalid-date');
}
}

Expand Down Expand Up @@ -123,9 +123,12 @@ export function initRepoIssueSidebarList() {
fullTextSearch: true,
});

function excludeLabel(item) {
const href = $(item).attr('href');
const id = $(item).data('label-id');
/**
* @param {HTMLElement} item
*/
function excludeLabel(item) { // eslint-disable-line unicorn/consistent-function-scoping
silverwind marked this conversation as resolved.
Show resolved Hide resolved
const href = item.getAttribute('href');
const id = item.getAttribute('data-label-id');

const regStr = `labels=((?:-?[0-9]+%2c)*)(${id})((?:%2c-?[0-9]+)*)&`;
const newStr = 'labels=$1-$2$3&';
Expand All @@ -144,9 +147,9 @@ export function initRepoIssueSidebarList() {

$('.menu .ui.dropdown.label-filter').on('keydown', (e) => {
if (e.altKey && e.keyCode === 13) {
const $selectedItems = $('.menu .ui.dropdown.label-filter .menu .item.selected');
if ($selectedItems.length > 0) {
excludeLabel($($selectedItems[0]));
const selectedItems = document.querySelectorAll('.menu .ui.dropdown.label-filter .menu .item.selected');
if (selectedItems.length > 0) {
excludeLabel(selectedItems[0]);
yardenshoham marked this conversation as resolved.
Show resolved Hide resolved
}
}
});
Expand All @@ -166,11 +169,11 @@ export function initRepoIssueCommentDelete() {
const $parentTimelineGroup = $this.closest('.timeline-item-group');
// Check if this was a pending comment.
if ($conversationHolder.find('.pending-label').length) {
const $counter = $('#review-box .review-comments-counter');
let num = parseInt($counter.attr('data-pending-comment-number')) - 1 || 0;
const counter = document.querySelector('#review-box .review-comments-counter');
let num = parseInt(counter?.getAttribute('data-pending-comment-number')) - 1 || 0;
num = Math.max(num, 0);
$counter.attr('data-pending-comment-number', num);
$counter.text(num);
counter.setAttribute('data-pending-comment-number', num);
counter.textContent = num;
yardenshoham marked this conversation as resolved.
Show resolved Hide resolved
}

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

export function initRepoPullRequestAllowMaintainerEdit() {
const $checkbox = $('#allow-edits-from-maintainers');
if (!$checkbox.length) return;
const checkbox = document.getElementById('allow-edits-from-maintainers');
if (!checkbox) return;

const promptError = $checkbox.attr('data-prompt-error');
const $checkbox = $(checkbox);

const promptError = checkbox.getAttribute('data-prompt-error');
$checkbox.checkbox({
'onChange': async () => {
const checked = $checkbox.checkbox('is checked');
let url = $checkbox.attr('data-url');
let url = checkbox.getAttribute('data-url');
url += '/set_allow_maintainer_edit';
$checkbox.checkbox('set disabled');
try {
Expand All @@ -298,7 +303,7 @@ export function initRepoPullRequestAllowMaintainerEdit() {
}
} catch (error) {
console.error(error);
showTemporaryTooltip($checkbox[0], promptError);
showTemporaryTooltip(checkbox, promptError);
} finally {
$checkbox.checkbox('set enabled');
}
Expand All @@ -325,7 +330,9 @@ export function initRepoIssueReferenceRepositorySearch() {
},
onChange(_value, _text, $choice) {
const $form = $choice.closest('form');
$form.attr('action', `${appSubUrl}/${_text}/issues/new`);
if (!$form.length) return;

$form[0].setAttribute('action', `${appSubUrl}/${_text}/issues/new`);
},
fullTextSearch: true,
});
Expand Down Expand Up @@ -375,17 +382,17 @@ export function initRepoIssueComments() {
window.location.reload();
});

$(document).on('click', (event) => {
const $urlTarget = $(':target');
if (!$urlTarget.length) return;
document.addEventListener('click', (event) => {
yardenshoham marked this conversation as resolved.
Show resolved Hide resolved
const urlTarget = document.querySelector(':target');
if (!urlTarget) return;

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

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

if (!$target.closest(`#${urlTargetId}`).length) {
const target = event.target;
if (!target.closest(`#${urlTargetId}`)) {
yardenshoham marked this conversation as resolved.
Show resolved Hide resolved
const scrollPosition = $(window).scrollTop();
window.location.hash = '';
$(window).scrollTop(scrollPosition);
Expand Down Expand Up @@ -419,30 +426,33 @@ export function initRepoPullRequestReview() {
if (window.history.scrollRestoration !== 'manual') {
window.history.scrollRestoration = 'manual';
}
const $commentDiv = $(window.location.hash);
if ($commentDiv) {
const commentDiv = document.querySelector(window.location.hash);
if (commentDiv) {
// get the name of the parent id
const groupID = $commentDiv.closest('div[id^="code-comments-"]').attr('id');
const groupID = commentDiv.closest('div[id^="code-comments-"]')?.getAttribute('id');
if (groupID && groupID.startsWith('code-comments-')) {
const id = groupID.slice(14);
const $ancestorDiffBox = $commentDiv.closest('.diff-file-box');
const ancestorDiffBox = commentDiv.closest('.diff-file-box');
// on pages like conversation, there is no diff header
const $diffHeader = $ancestorDiffBox.find('.diff-file-header');
const diffHeader = ancestorDiffBox?.querySelector('.diff-file-header');

// offset is for scrolling
let offset = 30;
if ($diffHeader[0]) {
offset += $('.diff-detail-box').outerHeight() + $diffHeader.outerHeight();
if (diffHeader) {
offset += $('.diff-detail-box').outerHeight() + $(diffHeader).outerHeight();
}
$(`#show-outdated-${id}`).addClass('tw-hidden');
$(`#code-comments-${id}`).removeClass('tw-hidden');
$(`#code-preview-${id}`).removeClass('tw-hidden');
$(`#hide-outdated-${id}`).removeClass('tw-hidden');

document.getElementById(`show-outdated-${id}`).classList.add('tw-hidden');
document.getElementById(`code-comments-${id}`).classList.remove('tw-hidden');
document.getElementById(`code-preview-${id}`).classList.remove('tw-hidden');
document.getElementById(`hide-outdated-${id}`).classList.remove('tw-hidden');
// if the comment box is folded, expand it
if ($ancestorDiffBox.attr('data-folded') && $ancestorDiffBox.attr('data-folded') === 'true') {
setFileFolding($ancestorDiffBox[0], $ancestorDiffBox.find('.fold-file')[0], false);
if (ancestorDiffBox.getAttribute('data-folded') === 'true') {
setFileFolding(ancestorDiffBox, ancestorDiffBox.querySelector('.fold-file'), false);
}

window.scrollTo({
top: $commentDiv.offset().top - offset,
top: $(commentDiv).offset().top - offset,
behavior: 'instant',
});
}
Expand Down Expand Up @@ -529,7 +539,7 @@ export function initRepoPullRequestReview() {
const $commentCloud = $td.find('.comment-code-cloud');
if (!$commentCloud.length && !$ntr.find('button[name="pending_review"]').length) {
try {
const response = await GET($(this).closest('[data-new-comment-url]').attr('data-new-comment-url'));
const response = await GET(this.closest('[data-new-comment-url]')?.getAttribute('data-new-comment-url'));
const html = await response.text();
$td.html(html);
$td.find("input[name='line']").val(idx);
Expand Down Expand Up @@ -607,7 +617,7 @@ export function initRepoIssueTitleEdit() {
$('#edit-title').on('click', editTitleToggle);
$('#cancel-edit-title').on('click', editTitleToggle);
$('#save-edit-title').on('click', editTitleToggle).on('click', async function () {
const pullrequest_targetbranch_change = async function (update_url) {
const pullrequest_targetbranch_change = async function (update_url) {// eslint-disable-line unicorn/consistent-function-scoping
silverwind marked this conversation as resolved.
Show resolved Hide resolved
const targetBranch = $('#pull-target-branch').data('branch');
const $branchTarget = $('#branch_target');
if (targetBranch === $branchTarget.text()) {
Expand All @@ -623,15 +633,15 @@ export function initRepoIssueTitleEdit() {
}
};

const pullrequest_target_update_url = $(this).attr('data-target-update-url');
const pullrequest_target_update_url = this.getAttribute('data-target-update-url');
if (!$editInput.val().length || $editInput.val() === $issueTitle.text()) {
$editInput.val($issueTitle.text());
await pullrequest_targetbranch_change(pullrequest_target_update_url);
} else {
try {
const params = new URLSearchParams();
params.append('title', $editInput.val());
const response = await POST($(this).attr('data-update-url'), {data: params});
const response = await POST(this.getAttribute('data-update-url'), {data: params});
const data = await response.json();
$editInput.val(data.title);
$issueTitle.text(data.title);
Expand Down Expand Up @@ -671,10 +681,11 @@ export function initSingleCommentEditor($commentForm) {
// * normal new issue/pr page, no status-button
// * issue/pr view page, with comment form, has status-button
const opts = {};
const $statusButton = $('#status-button');
if ($statusButton.length) {
const statusButton = document.getElementById('status-button');
if (statusButton) {
opts.onContentChanged = (editor) => {
$statusButton.text($statusButton.attr(editor.value().trim() ? 'data-status-and-comment' : 'data-status'));
const statusText = editor.value().trim() ? statusButton.getAttribute('data-status-and-comment') : statusButton.getAttribute('data-status');
yardenshoham marked this conversation as resolved.
Show resolved Hide resolved
statusButton.textContent = statusText;
};
}
initComboMarkdownEditor($commentForm.find('.combo-markdown-editor'), opts);
Expand Down