@@ -15,14 +15,21 @@ function isOverlayVisible(){
1515
1616let overlayHandlerContent
1717
18+ let overlayContainer = document . getElementById ( 'overlayContainer' )
19+ let accountSelectContent = document . getElementById ( 'accountSelectContent' )
20+
1821/**
1922 * Overlay keydown handler for a non-dismissable overlay.
2023 *
2124 * @param {KeyboardEvent } e The keydown event.
2225 */
2326function overlayKeyHandler ( e ) {
2427 if ( e . key === 'Enter' || e . key === 'Escape' ) {
25- document . getElementById ( overlayHandlerContent ) . getElementsByClassName ( 'overlayKeybindEnter' ) [ 0 ] . click ( )
28+ if ( overlayContainer . hasAttribute ( 'popup' ) ) {
29+ toggleOverlay ( false )
30+ } else {
31+ document . getElementById ( overlayHandlerContent ) . getElementsByClassName ( 'overlayKeybindEnter' ) [ 0 ] . click ( )
32+ }
2633 }
2734}
2835/**
@@ -64,8 +71,9 @@ function bindOverlayKeys(state, content, dismissable){
6471 * @param {boolean } toggleState True to display, false to hide.
6572 * @param {boolean } dismissable Optional. True to show the dismiss option, otherwise false.
6673 * @param {string } content Optional. The content div to be shown.
74+ * @param {boolean } popup Optional. True to show the overlay as a popup that is easily dismissable and interactible.
6775 */
68- function toggleOverlay ( toggleState , dismissable = false , content = 'overlayContent' ) {
76+ function toggleOverlay ( toggleState , dismissable = false , content = 'overlayContent' , popup = false ) {
6977 if ( toggleState == null ) {
7078 toggleState = ! document . getElementById ( 'main' ) . hasAttribute ( 'overlay' )
7179 }
@@ -75,6 +83,8 @@ function toggleOverlay(toggleState, dismissable = false, content = 'overlayConte
7583 }
7684 bindOverlayKeys ( toggleState , content , dismissable )
7785 if ( toggleState ) {
86+ overlayContainer . setAttribute ( 'content' , content )
87+ overlayContainer . setAttribute ( 'popup' , popup )
7888 document . getElementById ( 'main' ) . setAttribute ( 'overlay' , true )
7989 // Make things untabbable.
8090 $ ( '#main *' ) . attr ( 'tabindex' , '-1' )
@@ -94,6 +104,7 @@ function toggleOverlay(toggleState, dismissable = false, content = 'overlayConte
94104 }
95105 } )
96106 } else {
107+ overlayContainer . removeAttribute ( 'content' )
97108 document . getElementById ( 'main' ) . removeAttribute ( 'overlay' )
98109 // Make things tabbable.
99110 $ ( '#main *' ) . removeAttr ( 'tabindex' )
@@ -119,7 +130,21 @@ function toggleOverlay(toggleState, dismissable = false, content = 'overlayConte
119130
120131async function toggleServerSelection ( toggleState ) {
121132 await prepareServerSelectionList ( )
122- toggleOverlay ( toggleState , true , 'serverSelectContent' )
133+ toggleOverlay ( toggleState , false , 'serverSelectContent' , true )
134+ }
135+
136+ async function toggleAccountSelection ( toggleState , popup = false ) {
137+ if ( popup ) {
138+ // set the accountSelectActions div to display: none to avoid colliding with the validateSelectedAccount function
139+ document . getElementById ( 'accountSelectActions' ) . style . display = 'none'
140+ } else {
141+ // set the overlayContainer div to display: block, this is not done while closing the overlay because of the fadeOut effect
142+ document . getElementById ( 'accountSelectActions' ) . style . display = 'block'
143+ }
144+
145+ // show the overlay
146+ await prepareAccountSelectionList ( )
147+ toggleOverlay ( toggleState , false , 'accountSelectContent' , popup )
123148}
124149
125150/**
@@ -169,27 +194,9 @@ function setDismissHandler(handler){
169194 }
170195}
171196
172- /* Server Select View */
173-
174- document . getElementById ( 'serverSelectConfirm' ) . addEventListener ( 'click' , async ( ) => {
175- const listings = document . getElementsByClassName ( 'serverListing' )
176- for ( let i = 0 ; i < listings . length ; i ++ ) {
177- if ( listings [ i ] . hasAttribute ( 'selected' ) ) {
178- const serv = ( await DistroAPI . getDistribution ( ) ) . getServerById ( listings [ i ] . getAttribute ( 'servid' ) )
179- updateSelectedServer ( serv )
180- refreshServerStatus ( true )
181- toggleOverlay ( false )
182- return
183- }
184- }
185- // None are selected? Not possible right? Meh, handle it.
186- if ( listings . length > 0 ) {
187- const serv = ( await DistroAPI . getDistribution ( ) ) . getServerById ( listings [ i ] . getAttribute ( 'servid' ) )
188- updateSelectedServer ( serv )
189- toggleOverlay ( false )
190- }
191- } )
197+ /* Account Select button */
192198
199+ // Bind account select confirm button.
193200document . getElementById ( 'accountSelectConfirm' ) . addEventListener ( 'click' , async ( ) => {
194201 const listings = document . getElementsByClassName ( 'accountListing' )
195202 for ( let i = 0 ; i < listings . length ; i ++ ) {
@@ -218,51 +225,73 @@ document.getElementById('accountSelectConfirm').addEventListener('click', async
218225 }
219226} )
220227
221- // Bind server select cancel button.
222- document . getElementById ( 'serverSelectCancel' ) . addEventListener ( 'click' , ( ) => {
223- toggleOverlay ( false )
224- } )
225-
228+ // Bind account select cancel button.
226229document . getElementById ( 'accountSelectCancel' ) . addEventListener ( 'click' , ( ) => {
227230 $ ( '#accountSelectContent' ) . fadeOut ( 250 , ( ) => {
228231 $ ( '#overlayContent' ) . fadeIn ( 250 )
229232 } )
230233} )
231234
232- function setServerListingHandlers ( ) {
235+
236+ // Bind account select manage button.
237+ document . getElementById ( 'accountSelectManage' ) . addEventListener ( 'click' , async ( ) => {
238+ await prepareSettings ( )
239+ switchView ( getCurrentView ( ) , VIEWS . settings , 500 , 500 , ( ) => {
240+ settingsNavItemListener ( document . getElementById ( 'settingsNavAccount' ) , false )
241+ } )
242+ toggleOverlay ( false )
243+ } )
244+
245+ // Make the Server Selection background clickable to close the overlay.
246+ overlayContainer . addEventListener ( 'click' , e => {
247+ if ( e . target === overlayContainer ) {
248+ // This function only works if the overlay is a popup
249+ if ( overlayContainer . hasAttribute ( 'popup' ) ) {
250+ toggleOverlay ( false )
251+ }
252+ }
253+ } )
254+
255+ async function setServerListingHandlers ( ) {
233256 const listings = Array . from ( document . getElementsByClassName ( 'serverListing' ) )
234- listings . map ( ( val ) => {
235- val . onclick = e => {
236- if ( val . hasAttribute ( 'selected' ) ) {
237- return
238- }
239- const cListings = document . getElementsByClassName ( 'serverListing' )
240- for ( let i = 0 ; i < cListings . length ; i ++ ) {
241- if ( cListings [ i ] . hasAttribute ( 'selected' ) ) {
242- cListings [ i ] . removeAttribute ( 'selected' )
243- }
244- }
245- val . setAttribute ( 'selected' , '' )
246- document . activeElement . blur ( )
257+ listings . map ( async ( val ) => {
258+ val . onclick = async e => {
259+ const serv = ( await DistroAPI . getDistribution ( ) ) . getServerById ( val . getAttribute ( 'servid' ) )
260+ updateSelectedServer ( serv )
261+ refreshServerStatus ( true )
262+ toggleOverlay ( false )
247263 }
248264 } )
249265}
250266
251- function setAccountListingHandlers ( ) {
267+ async function setAccountListingHandlers ( ) {
252268 const listings = Array . from ( document . getElementsByClassName ( 'accountListing' ) )
253- listings . map ( ( val ) => {
254- val . onclick = e => {
255- if ( val . hasAttribute ( 'selected' ) ) {
269+ listings . map ( async ( val ) => {
270+ val . onclick = async e => {
271+ // popup mode
272+ if ( overlayContainer . hasAttribute ( 'popup' ) ) {
273+ const authAcc = ConfigManager . setSelectedAccount ( val . getAttribute ( 'uuid' ) )
274+ ConfigManager . save ( )
275+ updateSelectedAccount ( authAcc )
276+ if ( getCurrentView ( ) === VIEWS . settings ) {
277+ await prepareSettings ( )
278+ }
279+ toggleOverlay ( false )
280+ validateSelectedAccount ( )
256281 return
257- }
258- const cListings = document . getElementsByClassName ( 'accountListing' )
259- for ( let i = 0 ; i < cListings . length ; i ++ ) {
260- if ( cListings [ i ] . hasAttribute ( 'selected' ) ) {
261- cListings [ i ] . removeAttribute ( 'selected' )
282+ } else {
283+ if ( val . hasAttribute ( 'selected' ) ) {
284+ return
285+ }
286+ const cListings = document . getElementsByClassName ( 'accountListing' )
287+ for ( let i = 0 ; i < cListings . length ; i ++ ) {
288+ if ( cListings [ i ] . hasAttribute ( 'selected' ) ) {
289+ cListings [ i ] . removeAttribute ( 'selected' )
290+ }
262291 }
292+ val . setAttribute ( 'selected' , '' )
293+ document . activeElement . blur ( )
263294 }
264- val . setAttribute ( 'selected' , '' )
265- document . activeElement . blur ( )
266295 }
267296 } )
268297}
@@ -304,7 +333,7 @@ function populateAccountListings(){
304333 const accounts = Array . from ( Object . keys ( accountsObj ) , v => accountsObj [ v ] )
305334 let htmlString = ''
306335 for ( let i = 0 ; i < accounts . length ; i ++ ) {
307- htmlString += `<button class="accountListing" uuid="${ accounts [ i ] . uuid } " ${ i === 0 ? 'selected' : '' } >
336+ htmlString += `<button class="accountListing" uuid="${ accounts [ i ] . uuid } " ${ ! i && ! overlayContainer . hasAttribute ( "popup" ) ? 'selected' : '' } >
308337 <img src="https://mc-heads.net/head/${ accounts [ i ] . uuid } /40">
309338 <div class="accountListingName">${ accounts [ i ] . displayName } </div>
310339 </button>`
@@ -315,10 +344,10 @@ function populateAccountListings(){
315344
316345async function prepareServerSelectionList ( ) {
317346 await populateServerListings ( )
318- setServerListingHandlers ( )
347+ await setServerListingHandlers ( )
319348}
320349
321- function prepareAccountSelectionList ( ) {
350+ async function prepareAccountSelectionList ( ) {
322351 populateAccountListings ( )
323- setAccountListingHandlers ( )
352+ await setAccountListingHandlers ( )
324353}
0 commit comments