Skip to content

Commit 26dbca7

Browse files
authored
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 <[email protected]>
1 parent d0d7b4b commit 26dbca7

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

web_src/js/features/repo-settings.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ const {appSubUrl, csrfToken} = window.config;
88

99
export function initRepoSettingsCollaboration() {
1010
// Change collaborator access mode
11-
$('.page-content.repository .ui.dropdown.access-mode').each((_, e) => {
12-
const $dropdown = $(e);
11+
$('.page-content.repository .ui.dropdown.access-mode').each((_, el) => {
12+
const $dropdown = $(el);
1313
const $text = $dropdown.find('> .text');
1414
$dropdown.dropdown({
1515
async action(_text, value) {
16-
const lastValue = $dropdown.attr('data-last-value');
16+
const lastValue = el.getAttribute('data-last-value');
1717
try {
18-
$dropdown.attr('data-last-value', value);
18+
el.setAttribute('data-last-value', value);
1919
$dropdown.dropdown('hide');
2020
const data = new FormData();
21-
data.append('uid', $dropdown.attr('data-uid'));
21+
data.append('uid', el.getAttribute('data-uid'));
2222
data.append('mode', value);
23-
await POST($dropdown.attr('data-url'), {data});
23+
await POST(el.getAttribute('data-url'), {data});
2424
} catch {
2525
$text.text('(error)'); // prevent from misleading users when error occurs
26-
$dropdown.attr('data-last-value', lastValue);
26+
el.setAttribute('data-last-value', lastValue);
2727
}
2828
},
2929
onChange(_value, text, _$choice) {
@@ -32,9 +32,9 @@ export function initRepoSettingsCollaboration() {
3232
onHide() {
3333
// 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
3434
setTimeout(() => {
35-
const $item = $dropdown.dropdown('get item', $dropdown.attr('data-last-value'));
35+
const $item = $dropdown.dropdown('get item', el.getAttribute('data-last-value'));
3636
if ($item) {
37-
$dropdown.dropdown('set selected', $dropdown.attr('data-last-value'));
37+
$dropdown.dropdown('set selected', el.getAttribute('data-last-value'));
3838
} else {
3939
$text.text('(none)'); // prevent from misleading users when the access mode is undefined
4040
}
@@ -45,11 +45,13 @@ export function initRepoSettingsCollaboration() {
4545
}
4646

4747
export function initRepoSettingSearchTeamBox() {
48-
const $searchTeamBox = $('#search-team-box');
49-
$searchTeamBox.search({
48+
const searchTeamBox = document.getElementById('search-team-box');
49+
if (!searchTeamBox) return;
50+
51+
$(searchTeamBox).search({
5052
minCharacters: 2,
5153
apiSettings: {
52-
url: `${appSubUrl}/org/${$searchTeamBox.attr('data-org-name')}/teams/-/search?q={query}`,
54+
url: `${appSubUrl}/org/${searchTeamBox.getAttribute('data-org-name')}/teams/-/search?q={query}`,
5355
headers: {'X-Csrf-Token': csrfToken},
5456
onResponse(response) {
5557
const items = [];
@@ -77,11 +79,11 @@ export function initRepoSettingGitHook() {
7779
export function initRepoSettingBranches() {
7880
if (!$('.repository.settings.branches').length) return;
7981
$('.toggle-target-enabled').on('change', function () {
80-
const $target = $($(this).attr('data-target'));
82+
const $target = $(this.getAttribute('data-target'));
8183
$target.toggleClass('disabled', !this.checked);
8284
});
8385
$('.toggle-target-disabled').on('change', function () {
84-
const $target = $($(this).attr('data-target'));
86+
const $target = $(this.getAttribute('data-target'));
8587
if (this.checked) $target.addClass('disabled'); // only disable, do not auto enable
8688
});
8789
$('#dismiss_stale_approvals').on('change', function () {

0 commit comments

Comments
 (0)