2023-02-19 12:06:14 +08:00
import { hideElem , showElem , toggleElem } from '../utils/dom.js' ;
2022-01-28 13:00:11 -08:00
2024-02-20 00:34:35 +02:00
const service = document . getElementById ( 'service_type' ) ;
const user = document . getElementById ( 'auth_username' ) ;
const pass = document . getElementById ( 'auth_password' ) ;
const token = document . getElementById ( 'auth_token' ) ;
const mirror = document . getElementById ( 'mirror' ) ;
const lfs = document . getElementById ( 'lfs' ) ;
const lfsSettings = document . getElementById ( 'lfs_settings' ) ;
const lfsEndpoint = document . getElementById ( 'lfs_endpoint' ) ;
const items = document . querySelectorAll ( '#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
2024-02-20 00:34:35 +02:00
user ? . addEventListener ( 'input' , ( ) => { checkItems ( false ) } ) ;
pass ? . addEventListener ( 'input' , ( ) => { checkItems ( false ) } ) ;
token ? . addEventListener ( 'input' , ( ) => { checkItems ( true ) } ) ;
mirror ? . addEventListener ( 'change' , ( ) => { checkItems ( true ) } ) ;
document . getElementById ( 'lfs_settings_show' ) ? . addEventListener ( 'click' , ( e ) => {
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
showElem ( lfsEndpoint ) ;
} ) ;
lfs ? . addEventListener ( 'change' , setLFSSettingsVisibility ) ;
const cloneAddr = document . getElementById ( 'clone_addr' ) ;
cloneAddr ? . addEventListener ( 'change' , ( ) => {
const repoName = document . getElementById ( 'repo_name' ) ;
if ( cloneAddr . value && ! repoName ? . value ) { // Only modify if repo_name input is blank
repoName . value = cloneAddr . value . match ( /^(.*\/)?((.+?)(\.git)?)$/ ) [ 3 ] ;
2020-08-27 20:36:37 -05:00
}
} ) ;
}
function checkAuth ( ) {
2024-02-20 00:34:35 +02:00
if ( ! service ) return ;
const serviceType = Number ( service . value ) ;
2020-08-27 20:36:37 -05:00
2020-09-10 02:29:10 +08:00
checkItems ( serviceType !== 1 ) ;
2020-08-27 20:36:37 -05:00
}
function checkItems ( tokenAuth ) {
let enableItems ;
if ( tokenAuth ) {
2024-02-20 00:34:35 +02:00
enableItems = token ? . value !== '' ;
2020-08-27 20:36:37 -05:00
} else {
2024-02-20 00:34:35 +02:00
enableItems = user ? . value !== '' || pass ? . value !== '' ;
2020-08-27 20:36:37 -05:00
}
2024-02-20 00:34:35 +02:00
if ( enableItems && Number ( service ? . value ) > 1 ) {
if ( mirror ? . checked ) {
for ( const item of items ) {
item . disabled = item . name !== 'wiki' ;
}
2020-09-21 18:42:22 -04:00
return ;
}
2024-02-20 00:34:35 +02:00
for ( const item of items ) item . disabled = false ;
2020-08-27 20:36:37 -05:00
} else {
2024-02-20 00:34:35 +02:00
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 ( ) {
2024-02-20 00:34:35 +02:00
if ( ! lfs ) return ;
const visible = lfs . checked ;
toggleElem ( lfsSettings , visible ) ;
hideElem ( lfsEndpoint ) ;
2021-04-09 00:25:57 +02:00
}