2018-04-25 17:11:10 -07:00
/ * *
* Initialize UI functions which depend on internal modules .
* Loaded after core UI functions are initialized in uicore . js .
* /
// Requirements
const path = require ( 'path' )
2018-05-09 19:23:37 -07:00
const AuthManager = require ( './assets/js/authmanager.js' )
2018-05-08 17:10:46 -07:00
const { AssetGuard } = require ( './assets/js/assetguard.js' )
2018-05-07 15:15:59 -07:00
const ConfigManager = require ( './assets/js/configmanager.js' )
2018-04-25 17:11:10 -07:00
2018-05-06 22:34:57 -07:00
let rscShouldLoad = false
2018-05-08 03:34:16 -07:00
let fatalStartupError = false
2018-05-09 19:23:37 -07:00
// Mapping of each view to their container IDs.
const VIEWS = {
landing : 'landingContainer' ,
login : 'loginContainer' ,
welcome : 'welcomeContainer'
}
// The currently shown view container.
let currentView = VIEWS . landing
/ * *
* Switch launcher views .
*
* @ param { string } current The ID of the current view container .
* @ param { * } next The ID of the next view container .
* @ param { * } currentFadeTime Optional . The fade out time for the current view .
* @ param { * } nextFadeTime Optional . The fade in time for the next view .
* @ param { * } onCurrentFade Optional . Callback function to execute when the current
* view fades out .
* @ param { * } onNextFade Optional . Callback function to execute when the next view
* fades in .
* /
function switchView ( current , next , currentFadeTime = 500 , nextFadeTime = 500 , onCurrentFade = ( ) => { } , onNextFade = ( ) => { } ) {
currentView = current
$ ( ` # ${ current } ` ) . fadeOut ( currentFadeTime , ( ) => {
onCurrentFade ( )
$ ( ` # ${ next } ` ) . fadeIn ( nextFadeTime , ( ) => {
onNextFade ( )
} )
} )
}
/ * *
* Get the currently shown view container .
*
* @ returns { string } The currently shown view container .
* /
function getCurrentView ( ) {
return currentView
}
2018-05-08 03:34:16 -07:00
function showMainUI ( ) {
2018-05-08 17:10:46 -07:00
updateSelectedServer ( AssetGuard . getServerById ( ConfigManager . getSelectedServer ( ) ) . name )
2018-05-08 03:34:16 -07:00
refreshServerStatus ( )
setTimeout ( ( ) => {
document . getElementById ( 'frameBar' ) . style . backgroundColor = 'rgba(1, 2, 1, 0.5)'
document . body . style . backgroundImage = ` url('assets/images/backgrounds/ ${ document . body . getAttribute ( 'bkid' ) } .jpg') `
$ ( '#main' ) . show ( )
2018-05-09 19:23:37 -07:00
//validateSelectedAccount()
2018-05-08 03:34:16 -07:00
if ( ConfigManager . isFirstLaunch ( ) ) {
$ ( '#welcomeContainer' ) . fadeIn ( 1000 )
} else {
$ ( '#landingContainer' ) . fadeIn ( 1000 )
}
setTimeout ( ( ) => {
2018-05-09 19:23:37 -07:00
$ ( '#loadingContainer' ) . fadeOut ( 500 , ( ) => {
2018-05-08 03:34:16 -07:00
$ ( '#loadSpinnerImage' ) . removeClass ( 'rotating' )
} )
2018-05-09 21:01:38 -07:00
} , 250 )
2018-05-08 03:34:16 -07:00
} , 750 )
initNews ( )
}
function showFatalStartupError ( ) {
setTimeout ( ( ) => {
$ ( '#loadingContainer' ) . fadeOut ( 250 , ( ) => {
document . getElementById ( 'overlayContainer' ) . style . background = 'none'
setOverlayContent (
'Fatal Error: Unable to Load Distribution Index' ,
'A connection could not be established to our servers to download the distribution index. No local copies were available to load. <br><br>The distribution index is an essential file which provides the latest server information. The launcher is unable to start without it. Ensure you are connected to the internet and relaunch the application.' ,
'Close'
)
setOverlayHandler ( ( ) => {
const window = remote . getCurrentWindow ( )
window . close ( )
} )
toggleOverlay ( true )
} )
} , 750 )
}
2018-05-06 22:34:57 -07:00
2018-05-08 17:10:46 -07:00
function onDistroRefresh ( data ) {
updateSelectedServer ( AssetGuard . getServerById ( ConfigManager . getSelectedServer ( ) ) . name )
refreshServerStatus ( )
initNews ( )
}
function refreshDistributionIndex ( remote , onSuccess , onError ) {
if ( remote ) {
AssetGuard . refreshDistributionDataRemote ( ConfigManager . getLauncherDirectory ( ) )
. then ( onSuccess )
. catch ( onError )
} else {
AssetGuard . refreshDistributionDataLocal ( ConfigManager . getLauncherDirectory ( ) )
. then ( onSuccess )
. catch ( onError )
}
}
2018-05-09 19:23:37 -07:00
async function validateSelectedAccount ( ) {
const selectedAcc = ConfigManager . getSelectedAccount ( )
if ( selectedAcc != null ) {
const val = await AuthManager . validateSelected ( )
if ( ! val ) {
const accLen = Object . keys ( ConfigManager . getAuthAccounts ( ) ) . length
setOverlayContent (
'Failed to Refresh Login' ,
` We were unable to refresh the login for <strong> ${ selectedAcc . displayName } </strong>. Please ${ accLen > 1 ? 'select another account or ' : '' } login again. ` ,
'Login' ,
'Select Another Account'
)
setOverlayHandler ( ( ) => {
document . getElementById ( 'loginUsername' ) . value = selectedAcc . username
validateEmail ( selectedAcc . username )
switchView ( getCurrentView ( ) , VIEWS . login )
toggleOverlay ( false )
} )
setDismissHandler ( ( ) => {
if ( accLen > 2 ) {
prepareAccountSelectionList ( )
$ ( '#overlayContent' ) . fadeOut ( 250 , ( ) => {
$ ( '#accountSelectContent' ) . fadeIn ( 250 )
} )
} else {
const accountsObj = ConfigManager . getAuthAccounts ( )
const accounts = Array . from ( Object . keys ( accountsObj ) , v => accountsObj [ v ] ) ;
const selectedUUID = ConfigManager . getSelectedAccount ( ) . uuid
for ( let i = 0 ; i < accounts . length ; i ++ ) {
if ( accounts [ i ] . uuid !== selectedUUID ) {
setSelectedAccount ( accounts [ i ] . uuid )
toggleOverlay ( false )
validateSelectedAccount ( )
}
}
}
} )
toggleOverlay ( true , accLen > 1 )
} else {
return true
}
} else {
return true
}
}
2018-05-09 21:01:38 -07:00
/ * *
* Temporary function to update the selected account along
* with the relevent UI elements .
*
* @ param { string } uuid The UUID of the account .
* /
2018-05-09 19:23:37 -07:00
function setSelectedAccount ( uuid ) {
const authAcc = ConfigManager . setSelectedAccount ( uuid )
ConfigManager . save ( )
updateSelectedAccount ( authAcc )
//validateSelectedAccount()
}
2018-04-25 17:11:10 -07:00
// Synchronous Listener
document . addEventListener ( 'readystatechange' , function ( ) {
if ( document . readyState === 'complete' ) {
2018-05-06 22:34:57 -07:00
if ( rscShouldLoad ) {
2018-05-08 03:34:16 -07:00
if ( ! fatalStartupError ) {
showMainUI ( )
2018-05-06 22:34:57 -07:00
} else {
2018-05-08 03:34:16 -07:00
showFatalStartupError ( )
2018-05-06 22:34:57 -07:00
}
2018-05-08 03:34:16 -07:00
}
} else if ( document . readyState === 'interactive' ) {
//toggleOverlay(true, 'loadingContent')
2018-04-25 17:11:10 -07:00
}
/ * i f ( d o c u m e n t . r e a d y S t a t e = = = ' i n t e r a c t i v e ' ) {
} * /
} , false )
2018-05-06 22:34:57 -07:00
// Actions that must be performed after the distribution index is downloaded.
2018-05-08 03:34:16 -07:00
ipcRenderer . on ( 'distributionIndexDone' , ( event , data ) => {
if ( data != null ) {
if ( document . readyState === 'complete' ) {
showMainUI ( )
2018-05-06 22:34:57 -07:00
} else {
2018-05-08 03:34:16 -07:00
rscShouldLoad = true
2018-05-06 22:34:57 -07:00
}
} else {
2018-05-08 03:34:16 -07:00
fatalStartupError = true
if ( document . readyState === 'complete' ) {
showFatalStartupError ( )
} else {
rscShouldLoad = true
}
2018-05-06 22:34:57 -07:00
}
} )