Files

81 lines
2.8 KiB
TypeScript
Raw Permalink Normal View History

import {hideElem, showElem, toggleElem} from '../utils/dom.ts';
import {sanitizeRepoName} from './repo-common.ts';
2024-12-11 09:29:04 +01:00
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');
2025-12-03 03:13:16 +01:00
const lfsSettings = document.querySelector<HTMLElement>('#lfs_settings')!;
const lfsEndpoint = document.querySelector<HTMLElement>('#lfs_endpoint')!;
2024-12-11 09:29:04 +01:00
const items = document.querySelectorAll<HTMLInputElement>('#migrate_items input[type=checkbox]');
2020-08-27 20:36:37 -05:00
2022-12-23 17:03:11 +01:00
export function initRepoMigration() {
2020-08-27 20:36:37 -05:00
checkAuth();
2021-04-09 00:25:57 +02:00
setLFSSettingsVisibility();
2020-08-27 20:36:37 -05:00
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) => {
e.preventDefault();
e.stopPropagation();
showElem(lfsEndpoint);
});
lfs?.addEventListener('change', setLFSSettingsVisibility);
const elCloneAddr = document.querySelector<HTMLInputElement>('#clone_addr');
const elRepoName = document.querySelector<HTMLInputElement>('#repo_name');
if (elCloneAddr && elRepoName) {
let repoNameChanged = false;
elRepoName.addEventListener('input', () => {repoNameChanged = true});
elCloneAddr.addEventListener('input', () => {
if (repoNameChanged) return;
let repoNameFromUrl = elCloneAddr.value.split(/[?#]/)[0];
2025-07-27 16:16:52 +08:00
const parts = /^(.*\/)?((.+?)\/?)$/.exec(repoNameFromUrl);
if (!parts || parts.length < 4) {
elRepoName.value = '';
return;
}
repoNameFromUrl = parts[3].split(/[?#]/)[0];
elRepoName.value = sanitizeRepoName(repoNameFromUrl);
});
}
2020-08-27 20:36:37 -05:00
}
function checkAuth() {
if (!service) return;
const serviceType = Number(service.value);
2020-08-27 20:36:37 -05:00
checkItems(serviceType !== 1);
2020-08-27 20:36:37 -05:00
}
2024-12-11 09:29:04 +01:00
function checkItems(tokenAuth: boolean) {
2025-10-12 23:07:15 +02:00
let enableItems: boolean;
2020-08-27 20:36:37 -05:00
if (tokenAuth) {
enableItems = token?.value !== '';
2020-08-27 20:36:37 -05:00
} else {
enableItems = user?.value !== '' || pass?.value !== '';
2020-08-27 20:36:37 -05:00
}
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;
2020-08-27 20:36:37 -05:00
} else {
for (const item of items) item.disabled = true;
2020-08-27 20:36:37 -05:00
}
}
2021-04-09 00:25:57 +02:00
function setLFSSettingsVisibility() {
if (!lfs) return;
const visible = lfs.checked;
toggleElem(lfsSettings, visible);
hideElem(lfsEndpoint);
2021-04-09 00:25:57 +02:00
}