Skip to content

Commit ca0ce00

Browse files
authored
Refactor repo-settings.ts (#33785)
and remove jQuery
1 parent 163bbca commit ca0ce00

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

web_src/js/features/repo-settings.ts

+39-39
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import $ from 'jquery';
21
import {minimatch} from 'minimatch';
32
import {createMonaco} from './codeeditor.ts';
43
import {onInputDebounce, queryElems, toggleElem} from '../utils/dom.ts';
54
import {POST} from '../modules/fetch.ts';
65
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
76
import {initRepoSettingsBranchesDrag} from './repo-settings-branches.ts';
7+
import {fomanticQuery} from '../modules/fomantic/base.ts';
88

99
const {appSubUrl, csrfToken} = window.config;
1010

1111
function initRepoSettingsCollaboration() {
1212
// Change collaborator access mode
1313
for (const dropdownEl of queryElems(document, '.page-content.repository .ui.dropdown.access-mode')) {
1414
const textEl = dropdownEl.querySelector(':scope > .text');
15-
$(dropdownEl).dropdown({
15+
const $dropdown = fomanticQuery(dropdownEl);
16+
$dropdown.dropdown({
1617
async action(text: string, value: string) {
1718
dropdownEl.classList.add('is-loading', 'loading-icon-2px');
1819
const lastValue = dropdownEl.getAttribute('data-last-value');
19-
$(dropdownEl).dropdown('hide');
20+
$dropdown.dropdown('hide');
2021
try {
2122
const uid = dropdownEl.getAttribute('data-uid');
2223
await POST(dropdownEl.getAttribute('data-url'), {data: new URLSearchParams({uid, 'mode': value})});
@@ -33,9 +34,9 @@ function initRepoSettingsCollaboration() {
3334
// set to the really selected value, defer to next tick to make sure `action` has finished
3435
// its work because the calling order might be onHide -> action
3536
setTimeout(() => {
36-
const $item = $(dropdownEl).dropdown('get item', dropdownEl.getAttribute('data-last-value'));
37+
const $item = $dropdown.dropdown('get item', dropdownEl.getAttribute('data-last-value'));
3738
if ($item) {
38-
$(dropdownEl).dropdown('set selected', dropdownEl.getAttribute('data-last-value'));
39+
$dropdown.dropdown('set selected', dropdownEl.getAttribute('data-last-value'));
3940
} else {
4041
textEl.textContent = '(none)'; // prevent from misleading users when the access mode is undefined
4142
}
@@ -49,32 +50,32 @@ function initRepoSettingsSearchTeamBox() {
4950
const searchTeamBox = document.querySelector('#search-team-box');
5051
if (!searchTeamBox) return;
5152

52-
$(searchTeamBox).search({
53+
fomanticQuery(searchTeamBox).search({
5354
minCharacters: 2,
55+
searchFields: ['name', 'description'],
56+
showNoResults: false,
57+
rawResponse: true,
5458
apiSettings: {
5559
url: `${appSubUrl}/org/${searchTeamBox.getAttribute('data-org-name')}/teams/-/search?q={query}`,
5660
headers: {'X-Csrf-Token': csrfToken},
5761
onResponse(response: any) {
5862
const items: Array<Record<string, any>> = [];
59-
$.each(response.data, (_i, item) => {
63+
for (const item of response.data) {
6064
items.push({
6165
title: item.name,
6266
description: `${item.permission} access`, // TODO: translate this string
6367
});
64-
});
65-
68+
}
6669
return {results: items};
6770
},
6871
},
69-
searchFields: ['name', 'description'],
70-
showNoResults: false,
7172
});
7273
}
7374

7475
function initRepoSettingsGitHook() {
75-
if (!$('.edit.githook').length) return;
76+
if (!document.querySelector('.page-content.repository.settings.edit.githook')) return;
7677
const filename = document.querySelector('.hook-filename').textContent;
77-
createMonaco($('#content')[0] as HTMLTextAreaElement, filename, {language: 'shell'});
78+
createMonaco(document.querySelector<HTMLTextAreaElement>('#content'), filename, {language: 'shell'});
7879
}
7980

8081
function initRepoSettingsBranches() {
@@ -121,32 +122,31 @@ function initRepoSettingsBranches() {
121122
}
122123

123124
function initRepoSettingsOptions() {
124-
if ($('.repository.settings.options').length > 0) {
125-
// Enable or select internal/external wiki system and issue tracker.
126-
$('.enable-system').on('change', function (this: HTMLInputElement) { // eslint-disable-line @typescript-eslint/no-deprecated
127-
if (this.checked) {
128-
$($(this).data('target')).removeClass('disabled');
129-
if (!$(this).data('context')) $($(this).data('context')).addClass('disabled');
130-
} else {
131-
$($(this).data('target')).addClass('disabled');
132-
if (!$(this).data('context')) $($(this).data('context')).removeClass('disabled');
133-
}
134-
});
135-
$('.enable-system-radio').on('change', function (this: HTMLInputElement) { // eslint-disable-line @typescript-eslint/no-deprecated
136-
if (this.value === 'false') {
137-
$($(this).data('target')).addClass('disabled');
138-
if ($(this).data('context') !== undefined) $($(this).data('context')).removeClass('disabled');
139-
} else if (this.value === 'true') {
140-
$($(this).data('target')).removeClass('disabled');
141-
if ($(this).data('context') !== undefined) $($(this).data('context')).addClass('disabled');
142-
}
143-
});
144-
const $trackerIssueStyleRadios = $('.js-tracker-issue-style');
145-
$trackerIssueStyleRadios.on('change input', () => {
146-
const checkedVal = $trackerIssueStyleRadios.filter(':checked').val();
147-
$('#tracker-issue-style-regex-box').toggleClass('disabled', checkedVal !== 'regexp');
148-
});
149-
}
125+
const pageContent = document.querySelector('.page-content.repository.settings.options');
126+
if (!pageContent) return;
127+
128+
const toggleClass = (elems: NodeListOf<Element>, className: string, value: boolean) => {
129+
for (const el of elems) el.classList.toggle(className, value);
130+
};
131+
132+
// Enable or select internal/external wiki system and issue tracker.
133+
queryElems<HTMLInputElement>(pageContent, '.enable-system', (el) => el.addEventListener('change', () => {
134+
const elTargets = document.querySelectorAll(el.getAttribute('data-target'));
135+
const elContexts = document.querySelectorAll(el.getAttribute('data-context'));
136+
toggleClass(elTargets, 'disabled', !el.checked);
137+
toggleClass(elContexts, 'disabled', el.checked);
138+
}));
139+
queryElems<HTMLInputElement>(pageContent, '.enable-system-radio', (el) => el.addEventListener('change', () => {
140+
const elTargets = document.querySelectorAll(el.getAttribute('data-target'));
141+
const elContexts = document.querySelectorAll(el.getAttribute('data-context'));
142+
toggleClass(elTargets, 'disabled', el.value === 'false');
143+
toggleClass(elContexts, 'disabled', el.value === 'true');
144+
}));
145+
146+
queryElems<HTMLInputElement>(pageContent, '.js-tracker-issue-style', (el) => el.addEventListener('change', () => {
147+
const checkedVal = el.value;
148+
pageContent.querySelector('#tracker-issue-style-regex-box').classList.toggle('disabled', checkedVal !== 'regexp');
149+
}));
150150
}
151151

152152
export function initRepoSettings() {

0 commit comments

Comments
 (0)