2021-11-30 18:20:42 +08:00
"use strict" ;
2023-07-13 18:01:46 +08:00
function showPassword ( id ) {
let passwordField = document . getElementById ( id ) ;
if ( passwordField . type === "password" && passwordField . value !== "" ) {
passwordField . type = "text" ;
} else if ( passwordField . type === "text" && passwordField . value === "" ) {
passwordField . type = "password" ;
}
}
2021-11-30 18:20:42 +08:00
( function ( ) {
2023-06-06 17:27:52 +08:00
let lastError ;
2021-11-30 18:20:42 +08:00
function showError ( message ) {
const body = document . getElementsByTagName ( 'body' ) [ 0 ]
const toast = document . createElement ( "div" )
toast . className = "toast error"
toast . prepend ( message )
if ( lastError ) {
lastError . remove ( )
}
lastError = toast
body . prepend ( toast )
2022-05-20 20:02:04 +08:00
setTimeout ( toast . remove . bind ( toast ) , 10000 )
2021-11-30 18:20:42 +08:00
}
function handleEvent ( e ) {
const xhr = e . target ;
if ( xhr . status === 201 ) {
window . location . replace ( xhr . getResponseHeader ( 'Location' ) ) ;
2022-12-17 19:33:24 +08:00
} else if ( xhr . status === 422 ) {
2023-03-31 22:41:48 +08:00
disableSpinner ( )
2021-11-30 18:20:42 +08:00
showError ( xhr . response ) ;
2022-12-17 19:33:24 +08:00
} else if ( xhr . status === 500 ) {
2023-07-24 22:33:13 +08:00
showError ( "Server error. Please check the mastercontainer logs for details. This page will reload after 10s automatically. Then you can check the mastercontainer logs." ) ;
// Reload after 10s since it is expected that the updated view is shown (e.g. after starting containers)
setTimeout ( function ( ) {
window . location . reload ( 1 ) ;
} , 10000 ) ;
2022-12-17 19:33:24 +08:00
} else {
// If the responose is not one of the above, we should reload to show the latest content
window . location . reload ( 1 ) ;
2021-11-30 18:20:42 +08:00
}
}
2023-04-25 05:32:30 +08:00
function enableSpinner ( ) {
2021-11-30 18:20:42 +08:00
document . getElementById ( 'overlay' ) . classList . add ( 'loading' ) ;
}
2023-03-31 22:41:48 +08:00
function disableSpinner ( ) {
2021-11-30 18:20:42 +08:00
document . getElementById ( 'overlay' ) . classList . remove ( 'loading' ) ;
}
function initForm ( form ) {
function submit ( event )
{
if ( lastError ) {
lastError . remove ( )
}
2023-06-06 17:27:52 +08:00
let xhr = new XMLHttpRequest ( ) ;
2021-11-30 18:20:42 +08:00
xhr . addEventListener ( 'load' , handleEvent ) ;
xhr . addEventListener ( 'error' , ( ) => showError ( "Failed to talk to server." ) ) ;
2023-03-31 22:41:48 +08:00
xhr . addEventListener ( 'error' , ( ) => disableSpinner ( ) ) ;
2021-11-30 18:20:42 +08:00
xhr . open ( form . method , form . getAttribute ( "action" ) ) ;
xhr . setRequestHeader ( 'Content-type' , 'application/x-www-form-urlencoded' ) ;
2023-04-25 05:32:30 +08:00
enableSpinner ( ) ;
2021-11-30 18:20:42 +08:00
xhr . send ( new URLSearchParams ( new FormData ( form ) ) ) ;
event . preventDefault ( ) ;
}
form . onsubmit = submit ;
console . info ( form ) ;
}
function initForms ( ) {
const forms = document . querySelectorAll ( 'form.xhr' )
console . info ( "Making " + forms . length + " form(s) use XHR." ) ;
for ( const form of forms ) {
initForm ( form ) ;
}
}
if ( document . readyState === 'loading' ) {
// Loading hasn't finished yet
document . addEventListener ( 'DOMContentLoaded' , initForms ) ;
} else { // `DOMContentLoaded` has already fired
initForms ( ) ;
}
} ) ( )