/** * Script for overlay.ejs */ /* Overlay Wrapper Functions */ /** * Check to see if the overlay is visible. * * @returns {boolean} Whether or not the overlay is visible. */ function isOverlayVisible(){ return document.getElementById('main').hasAttribute('overlay') } let overlayHandlerContent let overlayContainer = document.getElementById('overlayContainer') let accountSelectContent = document.getElementById('accountSelectContent') /** * Overlay keydown handler for a non-dismissable overlay. * * @param {KeyboardEvent} e The keydown event. */ function overlayKeyHandler (e){ if(e.key === 'Enter' || e.key === 'Escape'){ if (overlayContainer.hasAttribute('popup')) { toggleOverlay(false) } else { document.getElementById(overlayHandlerContent).getElementsByClassName('overlayKeybindEnter')[0].click() } } } /** * Overlay keydown handler for a dismissable overlay. * * @param {KeyboardEvent} e The keydown event. */ function overlayKeyDismissableHandler (e){ if(e.key === 'Enter'){ document.getElementById(overlayHandlerContent).getElementsByClassName('overlayKeybindEnter')[0].click() } else if(e.key === 'Escape'){ document.getElementById(overlayHandlerContent).getElementsByClassName('overlayKeybindEsc')[0].click() } } /** * Bind overlay keydown listeners for escape and exit. * * @param {boolean} state Whether or not to add new event listeners. * @param {string} content The overlay content which will be shown. * @param {boolean} dismissable Whether or not the overlay is dismissable */ function bindOverlayKeys(state, content, dismissable){ overlayHandlerContent = content document.removeEventListener('keydown', overlayKeyHandler) document.removeEventListener('keydown', overlayKeyDismissableHandler) if(state){ if(dismissable){ document.addEventListener('keydown', overlayKeyDismissableHandler) } else { document.addEventListener('keydown', overlayKeyHandler) } } } /** * Toggle the visibility of the overlay. * * @param {boolean} toggleState True to display, false to hide. * @param {boolean} dismissable Optional. True to show the dismiss option, otherwise false. * @param {string} content Optional. The content div to be shown. * @param {boolean} popup Optional. True to show the overlay as a popup that is easily dismissable and interactible. */ function toggleOverlay(toggleState, dismissable = false, content = 'overlayContent', popup = false){ if(toggleState == null){ toggleState = !document.getElementById('main').hasAttribute('overlay') } if(typeof dismissable === 'string'){ content = dismissable dismissable = false } bindOverlayKeys(toggleState, content, dismissable) if(toggleState){ document.getElementById('main').setAttribute('overlay', true) overlayContainer.setAttribute('content', content) overlayContainer.setAttribute('popup', popup) // Make things untabbable. $('#main *').attr('tabindex', '-1') $('#' + content).parent().children().hide() $('#' + content).show() if(dismissable){ $('#overlayDismiss').show() } else { $('#overlayDismiss').hide() } $('#overlayContainer').fadeIn({ duration: 250, start: () => { if(getCurrentView() === VIEWS.settings){ document.getElementById('settingsContainer').style.backgroundColor = 'transparent' } } }) } else { document.getElementById('main').removeAttribute('overlay') overlayContainer.removeAttribute('content') // Make things tabbable. $('#main *').removeAttr('tabindex') $('#overlayContainer').fadeOut({ duration: 250, start: () => { if(getCurrentView() === VIEWS.settings){ document.getElementById('settingsContainer').style.backgroundColor = 'rgba(0, 0, 0, 0.50)' } }, complete: () => { $('#' + content).parent().children().hide() $('#' + content).show() if(dismissable){ $('#overlayDismiss').show() } else { $('#overlayDismiss').hide() } } }) } } async function toggleServerSelection(toggleState){ await prepareServerSelectionList() toggleOverlay(toggleState, false, 'serverSelectContent', true) } async function toggleAccountSelection(toggleState, popup = false){ if (popup) { // set the accountSelectActions div to display: none to avoid colliding with the validateSelectedAccount function document.getElementById('accountSelectActions').style.display = 'none' } else { // set the overlayContainer div to display: block, this is not done while closing the overlay because of the fadeOut effect document.getElementById('accountSelectActions').style.display = 'block' } // show the overlay await prepareAccountSelectionList() toggleOverlay(toggleState, false, 'accountSelectContent', popup) } /** * Set the content of the overlay. * * @param {string} title Overlay title text. * @param {string} description Overlay description text. * @param {string} acknowledge Acknowledge button text. * @param {string} dismiss Dismiss button text. */ function setOverlayContent(title, description, acknowledge, dismiss = Lang.queryJS('overlay.dismiss')){ document.getElementById('overlayTitle').innerHTML = title document.getElementById('overlayDesc').innerHTML = description document.getElementById('overlayAcknowledge').innerHTML = acknowledge document.getElementById('overlayDismiss').innerHTML = dismiss } /** * Set the onclick handler of the overlay acknowledge button. * If the handler is null, a default handler will be added. * * @param {function} handler */ function setOverlayHandler(handler){ if(handler == null){ document.getElementById('overlayAcknowledge').onclick = () => { toggleOverlay(false) } } else { document.getElementById('overlayAcknowledge').onclick = handler } } /** * Set the onclick handler of the overlay dismiss button. * If the handler is null, a default handler will be added. * * @param {function} handler */ function setDismissHandler(handler){ if(handler == null){ document.getElementById('overlayDismiss').onclick = () => { toggleOverlay(false) } } else { document.getElementById('overlayDismiss').onclick = handler } } /* Account Select button */ // Bind account select confirm button. document.getElementById('accountSelectConfirm').addEventListener('click', async () => { const listings = document.getElementsByClassName('accountListing') for(let i=0; i 0){ const authAcc = ConfigManager.setSelectedAccount(listings[0].getAttribute('uuid')) ConfigManager.save() updateSelectedAccount(authAcc) if(getCurrentView() === VIEWS.settings) { await prepareSettings() } toggleOverlay(false) validateSelectedAccount() } }) // Bind account select cancel button. document.getElementById('accountSelectCancel').addEventListener('click', () => { $('#accountSelectContent').fadeOut(250, () => { $('#overlayContent').fadeIn(250) }) }) // Bind account select manage button. document.getElementById('accountSelectManage').addEventListener('click', async () => { await prepareSettings() switchView(getCurrentView(), VIEWS.settings, 500, 500, () => { settingsNavItemListener(document.getElementById('settingsNavAccount'), false) }) toggleOverlay(false) }) // Make the Server Selection background clickable to close the overlay. overlayContainer.addEventListener('click', e => { if (e.target === overlayContainer) { // This function only works if the overlay is a popup if(overlayContainer.hasAttribute('popup')) { toggleOverlay(false) } } }) async function setServerListingHandlers(){ const listings = Array.from(document.getElementsByClassName('serverListing')) listings.map(async (val) => { val.onclick = async e => { const serv = (await DistroAPI.getDistribution()).getServerById(val.getAttribute('servid')) updateSelectedServer(serv) refreshServerStatus(true) toggleOverlay(false) } }) } async function setAccountListingHandlers(){ const listings = Array.from(document.getElementsByClassName('accountListing')) listings.map(async (val) => { val.onclick = async e => { // popup mode if(overlayContainer.hasAttribute('popup')){ const authAcc = ConfigManager.setSelectedAccount(val.getAttribute('uuid')) ConfigManager.save() updateSelectedAccount(authAcc) if(getCurrentView() === VIEWS.settings) { await prepareSettings() } toggleOverlay(false) validateSelectedAccount() return } else { if(val.hasAttribute('selected')){ return } const cListings = document.getElementsByClassName('accountListing') for(let i=0; i
${serv.rawServer.name} ${serv.rawServer.description}
${serv.rawServer.minecraftVersion}
${serv.rawServer.version}
${serv.rawServer.mainServer ? `
${Lang.queryJS('settings.serverListing.mainServer')}
` : ''}
` } document.getElementById('serverSelectListScrollable').innerHTML = htmlString } function populateAccountListings(){ const accountsObj = ConfigManager.getAuthAccounts() const accounts = Array.from(Object.keys(accountsObj), v=>accountsObj[v]) let htmlString = '' for(let i=0; i
${accounts[i].displayName}
` } document.getElementById('accountSelectListScrollable').innerHTML = htmlString } async function prepareServerSelectionList(){ await populateServerListings() await setServerListingHandlers() } async function prepareAccountSelectionList(){ populateAccountListings() await setAccountListingHandlers() }