const settingsNavDone = document.getElementById('settingsNavDone') const settingsAddAccount = document.getElementById('settingsAddAccount') const settingsCurrentAccounts = document.getElementById('settingsCurrentAccounts') /** * General Settings Functions */ let selectedTab = 'settingsTabAccount' /** * Bind functionality for the settings navigation items. */ function setupSettingsTabs(){ Array.from(document.getElementsByClassName('settingsNavItem')).map((val) => { val.onclick = (e) => { if(val.hasAttribute('selected')){ return } const navItems = document.getElementsByClassName('settingsNavItem') for(let i=0; i { $(`#${selectedTab}`).fadeIn(250) }) } }) } /* Closes the settings view and saves all data. */ settingsNavDone.onclick = () => { switchView(getCurrentView(), VIEWS.landing) } /** * Account Management Tab */ // Bind the add account button. settingsAddAccount.onclick = (e) => { switchView(getCurrentView(), VIEWS.login, 500, 500, () => { loginViewOnCancel = VIEWS.settings loginViewOnSuccess = VIEWS.settings loginCancelEnabled(true) }) } /** * Bind functionality for the account selection buttons. If another account * is selected, the UI of the previously selected account will be updated. */ function bindAuthAccountSelect(){ Array.from(document.getElementsByClassName('settingsAuthAccountSelect')).map((val) => { val.onclick = (e) => { if(val.hasAttribute('selected')){ return } const selectBtns = document.getElementsByClassName('settingsAuthAccountSelect') for(let i=0; i { val.onclick = (e) => { let isLastAccount = false if(Object.keys(ConfigManager.getAuthAccounts()).length === 1){ isLastAccount = true setOverlayContent( 'Warning
This is Your Last Account', 'In order to use the launcher you must be logged into at least one account. You will need to login again after.

Are you sure you want to log out?', 'I\'m Sure', 'Cancel' ) setOverlayHandler(() => { processLogOut(val, isLastAccount) switchView(getCurrentView(), VIEWS.login) toggleOverlay(false) }) setDismissHandler(() => { toggleOverlay(false) }) toggleOverlay(true, true) } else { processLogOut(val, isLastAccount) } } }) } /** * Process a log out. * * @param {Element} val The log out button element. * @param {boolean} isLastAccount If this logout is on the last added account. */ function processLogOut(val, isLastAccount){ const parent = val.closest('.settingsAuthAccount') const uuid = parent.getAttribute('uuid') const prevSelAcc = ConfigManager.getSelectedAccount() AuthManager.removeAccount(uuid).then(() => { if(!isLastAccount && uuid === prevSelAcc.uuid){ const selAcc = ConfigManager.getSelectedAccount() refreshAuthAccountSelected(selAcc.uuid) updateSelectedAccount(selAcc) validateSelectedAccount() } }) $(parent).fadeOut(250, () => { parent.remove() }) } /** * Refreshes the status of the selected account on the auth account * elements. * * @param {string} uuid The UUID of the new selected account. */ function refreshAuthAccountSelected(uuid){ Array.from(document.getElementsByClassName('settingsAuthAccount')).map((val) => { const selBtn = val.getElementsByClassName('settingsAuthAccountSelect')[0] if(uuid === val.getAttribute('uuid')){ selBtn.setAttribute('selected', '') selBtn.innerHTML = 'Selected Account ✔' } else { if(selBtn.hasAttribute('selected')){ selBtn.removeAttribute('selected') } selBtn.innerHTML = 'Select Account' } }) } /** * Add auth account elements for each one stored in the authentication database. */ function populateAuthAccounts(){ const authAccounts = ConfigManager.getAuthAccounts() const authKeys = Object.keys(authAccounts) const selectedUUID = ConfigManager.getSelectedAccount().uuid let authAccountStr = `` authKeys.map((val) => { const acc = authAccounts[val] authAccountStr += `
${acc.displayName}
Username
${acc.displayName}
${acc.displayName === acc.username ? 'UUID' : 'Email'}
${acc.displayName === acc.username ? acc.uuid : acc.username}
` }) settingsCurrentAccounts.innerHTML = authAccountStr } function prepareAccountsTab() { populateAuthAccounts() bindAuthAccountSelect() bindAuthAccountLogOut() } /** * Settings preparation functions. */ /** * Prepare the entire settings UI. */ function prepareSettings() { setupSettingsTabs() prepareAccountsTab() } // Prepare the settings UI on startup. prepareSettings()