Skip to content

Commit 676a0c0

Browse files
committed
Optimize the calling code of queryElems.
1 parent dd0caf7 commit 676a0c0

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

web_src/js/features/admin/common.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function onSecurityProtocolChange(): void {
1515
}
1616

1717
export function initAdminCommon(): void {
18-
if (!document.querySelector('.page-content.admin')) return;
18+
const el = document.querySelector('.page-content.admin');
19+
if (!el) return;
1920

2021
// check whether appUrl(ROOT_URL) is correct, if not, show an error message
2122
checkAppUrl();
@@ -24,7 +25,7 @@ export function initAdminCommon(): void {
2425
initAdminAuthentication();
2526
initAdminNotice();
2627

27-
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
28+
queryElems(el, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
2829
}
2930

3031
function initAdminUser() {

web_src/js/features/common-organization.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import {queryElems, toggleElem} from '../utils/dom.ts';
33
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
44

55
export function initCommonOrganization() {
6-
if (!document.querySelectorAll('.organization').length) {
7-
return;
8-
}
6+
const el = document.querySelector('.page-content.organization.settings');
7+
if (!el) return;
98

10-
document.querySelector<HTMLInputElement>('.organization.settings.options #org_name')?.addEventListener('input', function () {
11-
const nameChanged = this.value.toLowerCase() !== this.getAttribute('data-org-name').toLowerCase();
12-
toggleElem('#org-name-change-prompt', nameChanged);
9+
queryElems(el, '#org_name', (e: HTMLInputElement) => {
10+
e.addEventListener('input', function () {
11+
const nameChanged = this.value.toLowerCase() !== this.getAttribute('data-org-name').toLowerCase();
12+
toggleElem('#org-name-change-prompt', nameChanged);
13+
});
1314
});
1415

1516
// Labels
1617
initCompLabelEdit('.page-content.organization.settings.labels');
1718

18-
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
19+
queryElems(el, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
1920
}

web_src/js/features/repo-settings.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {appSubUrl, csrfToken} = window.config;
1010

1111
function initRepoSettingsCollaboration() {
1212
// Change collaborator access mode
13-
for (const dropdownEl of queryElems(document, '.page-content.repository .ui.dropdown.access-mode')) {
13+
for (const dropdownEl of queryElems(document.querySelector<HTMLElement>('.page-content.repository'), '.ui.dropdown.access-mode')) {
1414
const textEl = dropdownEl.querySelector(':scope > .text');
1515
const $dropdown = fomanticQuery(dropdownEl);
1616
$dropdown.dropdown({
@@ -142,13 +142,15 @@ function initRepoSettingsOptions() {
142142
}
143143

144144
export function initRepoSettings() {
145-
if (!document.querySelector('.page-content.repository.settings')) return;
145+
const el = document.querySelector('.page-content.repository.settings');
146+
if (!el) return;
147+
146148
initRepoSettingsOptions();
147149
initRepoSettingsBranches();
148150
initRepoSettingsCollaboration();
149151
initRepoSettingsSearchTeamBox();
150152
initRepoSettingsGitHook();
151153
initRepoSettingsBranchesDrag();
152154

153-
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
155+
queryElems(el, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
154156
}

web_src/js/features/user-settings.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ import {hideElem, queryElems, showElem} from '../utils/dom.ts';
22
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
33

44
export function initUserSettings() {
5-
if (!document.querySelector('.user.settings.profile')) return;
5+
const el = document.querySelector('.user.settings.profile');
6+
if (!el) return;
67

7-
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
8+
queryElems(el, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
89

9-
const usernameInput = document.querySelector<HTMLInputElement>('#username');
10-
if (!usernameInput) return;
11-
usernameInput.addEventListener('input', function () {
12-
const prompt = document.querySelector('#name-change-prompt');
13-
const promptRedirect = document.querySelector('#name-change-redirect-prompt');
14-
if (this.value.toLowerCase() !== this.getAttribute('data-name').toLowerCase()) {
15-
showElem(prompt);
16-
showElem(promptRedirect);
17-
} else {
18-
hideElem(prompt);
19-
hideElem(promptRedirect);
20-
}
10+
queryElems(el, '#username', (e: HTMLInputElement) => {
11+
e.addEventListener('input', function () {
12+
const prompt = document.querySelector('#name-change-prompt');
13+
const promptRedirect = document.querySelector('#name-change-redirect-prompt');
14+
if (this.value.toLowerCase() !== this.getAttribute('data-name').toLowerCase()) {
15+
showElem(prompt);
16+
showElem(promptRedirect);
17+
} else {
18+
hideElem(prompt);
19+
hideElem(promptRedirect);
20+
}
21+
});
2122
});
2223
}

web_src/js/utils/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function queryElemChildren<T extends Element>(parent: Element | ParentNod
9191
// it works like parent.querySelectorAll: all descendants are selected
9292
// in the future, all "queryElems(document, ...)" should be refactored to use a more specific parent
9393
export function queryElems<T extends HTMLElement>(parent: Element | ParentNode, selector: string, fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
94-
return applyElemsCallback<T>(parent.querySelectorAll(selector), fn);
94+
return parent ? applyElemsCallback<T>(parent.querySelectorAll(selector), fn) : [];
9595
}
9696

9797
export function onDomReady(cb: () => Promisable<void>) {

0 commit comments

Comments
 (0)