Skip to content

Commit 7773fe2

Browse files
committed
use the global-init framework to initial avater-uploader
1 parent dd0caf7 commit 7773fe2

File tree

8 files changed

+11
-17
lines changed

8 files changed

+11
-17
lines changed

templates/shared/avatar_upload_crop.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{- /* we do not need to set for/id here, global aria init code will add them automatically */ -}}
22
<label>{{.LabelText}}</label>
3-
<input class="avatar-file-with-cropper" name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp">
3+
<input class="avatar-file-with-cropper" name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp" data-global-init="initAvatarUploader">
44
{{- /* the cropper-panel must be next sibling of the input "avatar" */ -}}
55
<div class="cropper-panel tw-hidden">
66
<div class="tw-my-2">{{ctx.Locale.Tr "settings.cropper_prompt"}}</div>

web_src/js/features/admin/common.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {checkAppUrl} from '../common-page.ts';
22
import {hideElem, queryElems, showElem, toggleElem} from '../../utils/dom.ts';
33
import {POST} from '../../modules/fetch.ts';
4-
import {initAvatarUploaderWithCropper} from '../comp/Cropper.ts';
54
import {fomanticQuery} from '../../modules/fomantic/base.ts';
65

76
const {appSubUrl} = window.config;
@@ -23,8 +22,6 @@ export function initAdminCommon(): void {
2322
initAdminUser();
2423
initAdminAuthentication();
2524
initAdminNotice();
26-
27-
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
2825
}
2926

3027
function initAdminUser() {

web_src/js/features/common-organization.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {initCompLabelEdit} from './comp/LabelEdit.ts';
2-
import {queryElems, toggleElem} from '../utils/dom.ts';
3-
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
2+
import {toggleElem} from '../utils/dom.ts';
43

54
export function initCommonOrganization() {
65
if (!document.querySelectorAll('.organization').length) {
@@ -14,6 +13,4 @@ export function initCommonOrganization() {
1413

1514
// Labels
1615
initCompLabelEdit('.page-content.organization.settings.labels');
17-
18-
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
1916
}

web_src/js/features/common-page.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {showGlobalErrorMessage} from '../bootstrap.ts';
33
import {fomanticQuery} from '../modules/fomantic/base.ts';
44
import {queryElems} from '../utils/dom.ts';
55
import {registerGlobalInitFunc, registerGlobalSelectorFunc} from '../modules/observer.ts';
6+
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
67

78
const {appUrl} = window.config;
89

@@ -80,6 +81,10 @@ export function initGlobalTabularMenu() {
8081
fomanticQuery('.ui.menu.tabular:not(.custom) .item').tab();
8182
}
8283

84+
export function initGlobalAvatarUploader() {
85+
registerGlobalInitFunc('initAvatarUploader', initAvatarUploaderWithCropper);
86+
}
87+
8388
// for performance considerations, it only uses performant syntax
8489
function attachInputDirAuto(el: Partial<HTMLInputElement | HTMLTextAreaElement>) {
8590
if (el.type !== 'hidden' &&

web_src/js/features/repo-settings.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {minimatch} from 'minimatch';
22
import {createMonaco} from './codeeditor.ts';
33
import {onInputDebounce, queryElems, toggleClass, toggleElem} from '../utils/dom.ts';
44
import {POST} from '../modules/fetch.ts';
5-
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
65
import {initRepoSettingsBranchesDrag} from './repo-settings-branches.ts';
76
import {fomanticQuery} from '../modules/fomantic/base.ts';
87

@@ -149,6 +148,4 @@ export function initRepoSettings() {
149148
initRepoSettingsSearchTeamBox();
150149
initRepoSettingsGitHook();
151150
initRepoSettingsBranchesDrag();
152-
153-
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
154151
}

web_src/js/features/user-settings.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import {hideElem, queryElems, showElem} from '../utils/dom.ts';
2-
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
1+
import {hideElem, showElem} from '../utils/dom.ts';
32

43
export function initUserSettings() {
54
if (!document.querySelector('.user.settings.profile')) return;
65

7-
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
8-
96
const usernameInput = document.querySelector<HTMLInputElement>('#username');
107
if (!usernameInput) return;
118
usernameInput.addEventListener('input', function () {

web_src/js/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {initColorPickers} from './features/colorpicker.ts';
6060
import {initAdminSelfCheck} from './features/admin/selfcheck.ts';
6161
import {initOAuth2SettingsDisableCheckbox} from './features/oauth2-settings.ts';
6262
import {initGlobalFetchAction} from './features/common-fetch-action.ts';
63-
import {initFootLanguageMenu, initGlobalDropdown, initGlobalInput, initGlobalTabularMenu, initHeadNavbarContentToggle} from './features/common-page.ts';
63+
import {initFootLanguageMenu, initGlobalAvatarUploader, initGlobalDropdown, initGlobalInput, initGlobalTabularMenu, initHeadNavbarContentToggle} from './features/common-page.ts';
6464
import {initGlobalButtonClickOnEnter, initGlobalButtons, initGlobalDeleteButton} from './features/common-button.ts';
6565
import {initGlobalComboMarkdownEditor, initGlobalEnterQuickSubmit, initGlobalFormDirtyLeaveConfirm} from './features/common-form.ts';
6666
import {callInitFunctions} from './modules/init.ts';
@@ -72,6 +72,7 @@ initSubmitEventPolyfill();
7272
onDomReady(() => {
7373
const initStartTime = performance.now();
7474
const initPerformanceTracer = callInitFunctions([
75+
initGlobalAvatarUploader,
7576
initGlobalDropdown,
7677
initGlobalTabularMenu,
7778
initGlobalFetchAction,

web_src/js/utils/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function queryElemChildren<T extends Element>(parent: Element | ParentNod
8989
}
9090

9191
// it works like parent.querySelectorAll: all descendants are selected
92-
// in the future, all "queryElems(document, ...)" should be refactored to use a more specific parent
92+
// in the future, all "queryElems(document, ...)" should be refactored to use a more specific parent if the targets are not for page-level components.
9393
export function queryElems<T extends HTMLElement>(parent: Element | ParentNode, selector: string, fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
9494
return applyElemsCallback<T>(parent.querySelectorAll(selector), fn);
9595
}

0 commit comments

Comments
 (0)