refactor: clean up git migration forms (#39672)

Only extracted shared code, removed unnecessary code, and use
form-fetch-action to replace RenderWithErrDeprecated, slightly
refactored JS to use data-global-init

Nothing else is changed, although there are still many problems.

Written by AI

Diff with hiding spaces:
https://github.com/go-gitea/gitea/pull/39672/changes?w=1
This commit is contained in:
wxiaoguang authored and GitHub committed 2026-10-08 10:36:08 +02:00
1 parent 89ec271d9c
commit cc623c5f2f
18 files changed
+274 -724

No files matched your search

+45 -50
View File
@@ -1,33 +1,35 @@
import {hideElem, showElem, toggleElem} from '../utils/dom.ts';
import {sanitizeRepoName} from './repo-common.ts';
import {registerGlobalInitFunc} from '../modules/observer.ts';
const service = document.querySelector<HTMLInputElement>('#service_type');
const user = document.querySelector<HTMLInputElement>('#auth_username');
const pass = document.querySelector<HTMLInputElement>('#auth_password');
const token = document.querySelector<HTMLInputElement>('#auth_token');
const mirror = document.querySelector<HTMLInputElement>('#mirror');
const lfs = document.querySelector<HTMLInputElement>('#lfs');
const lfsSettings = document.querySelector<HTMLElement>('#lfs_settings')!;
const lfsEndpoint = document.querySelector<HTMLElement>('#lfs_endpoint')!;
const items = document.querySelectorAll<HTMLInputElement>('#migrate_items input[type=checkbox]');
export const initRepoMigration = () => registerGlobalInitFunc('initGitMigrateForm', (el: HTMLElement) => {
const elForm = el.closest('form')!;
const service = elForm.querySelector<HTMLInputElement>('input[name="service"]')!;
const user = elForm.querySelector<HTMLInputElement>('input[name="auth_username"]');
const pass = elForm.querySelector<HTMLInputElement>('input[name="auth_password"]');
const token = elForm.querySelector<HTMLInputElement>('input[name="auth_token"]');
const mirror = elForm.querySelector<HTMLInputElement>('input[name="mirror"]');
const lfs = elForm.querySelector<HTMLInputElement>('input[name="lfs"]');
const lfsSettings = elForm.querySelector<HTMLElement>('#lfs_settings');
const lfsEndpoint = elForm.querySelector<HTMLElement>('#lfs_endpoint');
const items = elForm.querySelectorAll<HTMLInputElement>('#migrate_items input[type=checkbox]');
export function initRepoMigration() {
checkAuth();
checkItems(Number(service.value) !== 1); // 1 = plain git service
setLFSSettingsVisibility();
user?.addEventListener('input', () => {checkItems(false)});
pass?.addEventListener('input', () => {checkItems(false)});
token?.addEventListener('input', () => {checkItems(true)});
mirror?.addEventListener('change', () => {checkItems(true)});
document.querySelector('#lfs_settings_show')?.addEventListener('click', (e) => {
user?.addEventListener('input', () => checkItems(false));
pass?.addEventListener('input', () => checkItems(false));
token?.addEventListener('input', () => checkItems(true));
mirror?.addEventListener('change', () => checkItems(true));
elForm.querySelector('#lfs_settings_show')?.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
showElem(lfsEndpoint);
showElem(lfsEndpoint!);
});
lfs?.addEventListener('change', setLFSSettingsVisibility);
const elCloneAddr = document.querySelector<HTMLInputElement>('#clone_addr');
const elRepoName = document.querySelector<HTMLInputElement>('#repo_name');
const elCloneAddr = elForm.querySelector<HTMLInputElement>('input[name="clone_addr"]');
const elRepoName = elForm.querySelector<HTMLInputElement>('input[name="repo_name"]');
if (elCloneAddr && elRepoName) {
let repoNameChanged = false;
elRepoName.addEventListener('input', () => {repoNameChanged = true});
@@ -43,38 +45,31 @@ export function initRepoMigration() {
elRepoName.value = sanitizeRepoName(repoNameFromUrl);
});
}
}
function checkAuth() {
if (!service) return;
const serviceType = Number(service.value);
checkItems(serviceType !== 1);
}
function checkItems(tokenAuth: boolean) {
let enableItems: boolean;
if (tokenAuth) {
enableItems = token?.value !== '';
} else {
enableItems = user?.value !== '' || pass?.value !== '';
}
if (enableItems && Number(service?.value) > 1) {
if (mirror?.checked) {
for (const item of items) {
item.disabled = item.name !== 'wiki';
}
return;
function checkItems(tokenAuth: boolean) {
let enableItems: boolean;
if (tokenAuth) {
enableItems = token?.value !== '';
} else {
enableItems = user?.value !== '' || pass?.value !== '';
}
if (enableItems && Number(service?.value) > 1) {
if (mirror?.checked) {
for (const item of items) {
item.disabled = item.name !== 'wiki';
}
return;
}
for (const item of items) item.disabled = false;
} else {
for (const item of items) item.disabled = true;
}
for (const item of items) item.disabled = false;
} else {
for (const item of items) item.disabled = true;
}
}
function setLFSSettingsVisibility() {
if (!lfs) return;
const visible = lfs.checked;
toggleElem(lfsSettings, visible);
hideElem(lfsEndpoint);
}
function setLFSSettingsVisibility() {
if (!lfs) return;
const visible = lfs.checked;
toggleElem(lfsSettings!, visible);
hideElem(lfsEndpoint!);
}
});