From 08a9a61faf87a4f50c9d132f0b6e851045ddec33 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 23 Mar 2024 14:28:53 +0200 Subject: [PATCH] Remove jQuery `.attr` from the repository settings (#30018) - Switched from jQuery `.attr` to plain javascript `getAttribute` and `setAttribute` - Tested the collaborator access mode change, team search box, and branch protection form. They all work as before --------- Signed-off-by: Yarden Shoham (cherry picked from commit 26dbca741114587f4191050a76ee1a36282a2018) --- web_src/js/features/repo-settings.js | 30 +++++++++++++++------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/web_src/js/features/repo-settings.js b/web_src/js/features/repo-settings.js index 58b714fbb7..dc1db8ab29 100644 --- a/web_src/js/features/repo-settings.js +++ b/web_src/js/features/repo-settings.js @@ -8,22 +8,22 @@ const {appSubUrl, csrfToken} = window.config; export function initRepoSettingsCollaboration() { // Change collaborator access mode - $('.page-content.repository .ui.dropdown.access-mode').each((_, e) => { - const $dropdown = $(e); + $('.page-content.repository .ui.dropdown.access-mode').each((_, el) => { + const $dropdown = $(el); const $text = $dropdown.find('> .text'); $dropdown.dropdown({ async action(_text, value) { - const lastValue = $dropdown.attr('data-last-value'); + const lastValue = el.getAttribute('data-last-value'); try { - $dropdown.attr('data-last-value', value); + el.setAttribute('data-last-value', value); $dropdown.dropdown('hide'); const data = new FormData(); - data.append('uid', $dropdown.attr('data-uid')); + data.append('uid', el.getAttribute('data-uid')); data.append('mode', value); - await POST($dropdown.attr('data-url'), {data}); + await POST(el.getAttribute('data-url'), {data}); } catch { $text.text('(error)'); // prevent from misleading users when error occurs - $dropdown.attr('data-last-value', lastValue); + el.setAttribute('data-last-value', lastValue); } }, onChange(_value, text, _$choice) { @@ -32,9 +32,9 @@ export function initRepoSettingsCollaboration() { onHide() { // set to the really selected value, defer to next tick to make sure `action` has finished its work because the calling order might be onHide -> action setTimeout(() => { - const $item = $dropdown.dropdown('get item', $dropdown.attr('data-last-value')); + const $item = $dropdown.dropdown('get item', el.getAttribute('data-last-value')); if ($item) { - $dropdown.dropdown('set selected', $dropdown.attr('data-last-value')); + $dropdown.dropdown('set selected', el.getAttribute('data-last-value')); } else { $text.text('(none)'); // prevent from misleading users when the access mode is undefined } @@ -45,11 +45,13 @@ export function initRepoSettingsCollaboration() { } export function initRepoSettingSearchTeamBox() { - const $searchTeamBox = $('#search-team-box'); - $searchTeamBox.search({ + const searchTeamBox = document.getElementById('search-team-box'); + if (!searchTeamBox) return; + + $(searchTeamBox).search({ minCharacters: 2, apiSettings: { - url: `${appSubUrl}/org/${$searchTeamBox.attr('data-org-name')}/teams/-/search?q={query}`, + url: `${appSubUrl}/org/${searchTeamBox.getAttribute('data-org-name')}/teams/-/search?q={query}`, headers: {'X-Csrf-Token': csrfToken}, onResponse(response) { const items = []; @@ -77,11 +79,11 @@ export function initRepoSettingGitHook() { export function initRepoSettingBranches() { if (!$('.repository.settings.branches').length) return; $('.toggle-target-enabled').on('change', function () { - const $target = $($(this).attr('data-target')); + const $target = $(this.getAttribute('data-target')); $target.toggleClass('disabled', !this.checked); }); $('.toggle-target-disabled').on('change', function () { - const $target = $($(this).attr('data-target')); + const $target = $(this.getAttribute('data-target')); if (this.checked) $target.addClass('disabled'); // only disable, do not auto enable }); $('#dismiss_stale_approvals').on('change', function () {