overlay.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Script for overlay.ejs
  3. */
  4. /* Overlay Wrapper Functions */
  5. /**
  6. * Toggle the visibility of the overlay.
  7. *
  8. * @param {boolean} toggleState True to display, false to hide.
  9. * @param {boolean} dismissable Optional. True to show the dismiss option, otherwise false.
  10. * @param {string} content Optional. The content div to be shown.
  11. */
  12. function toggleOverlay(toggleState, dismissable = false, content = 'overlayContent'){
  13. if(toggleState == null){
  14. toggleState = !document.getElementById('main').hasAttribute('overlay')
  15. }
  16. if(typeof dismissable === 'string'){
  17. content = dismissable
  18. dismissable = false
  19. }
  20. if(toggleState){
  21. document.getElementById('main').setAttribute('overlay', true)
  22. // Make things untabbable.
  23. $("#main *").attr('tabindex', '-1')
  24. $('#' + content).parent().children().hide()
  25. $('#' + content).show()
  26. if(dismissable){
  27. $('#overlayDismiss').show()
  28. } else {
  29. $('#overlayDismiss').hide()
  30. }
  31. $('#overlayContainer').fadeIn(250)
  32. } else {
  33. document.getElementById('main').removeAttribute('overlay')
  34. // Make things tabbable.
  35. $("#main *").removeAttr('tabindex')
  36. $('#overlayContainer').fadeOut(250, () => {
  37. $('#' + content).parent().children().hide()
  38. $('#' + content).show()
  39. if(dismissable){
  40. $('#overlayDismiss').show()
  41. } else {
  42. $('#overlayDismiss').hide()
  43. }
  44. })
  45. }
  46. }
  47. function toggleServerSelection(toggleState){
  48. prepareServerSelectionList()
  49. toggleOverlay(toggleState, 'serverSelectContent')
  50. }
  51. /**
  52. * Set the content of the overlay.
  53. *
  54. * @param {string} title Overlay title text.
  55. * @param {string} description Overlay description text.
  56. * @param {string} acknowledge Acknowledge button text.
  57. * @param {string} dismiss Dismiss button text.
  58. */
  59. function setOverlayContent(title, description, acknowledge, dismiss = 'Dismiss'){
  60. document.getElementById('overlayTitle').innerHTML = title
  61. document.getElementById('overlayDesc').innerHTML = description
  62. document.getElementById('overlayAcknowledge').innerHTML = acknowledge
  63. document.getElementById('overlayDismiss').innerHTML = dismiss
  64. }
  65. /**
  66. * Set the onclick handler of the overlay acknowledge button.
  67. * If the handler is null, a default handler will be added.
  68. *
  69. * @param {function} handler
  70. */
  71. function setOverlayHandler(handler){
  72. if(handler == null){
  73. document.getElementById('overlayAcknowledge').onclick = () => {
  74. toggleOverlay(false)
  75. }
  76. } else {
  77. document.getElementById('overlayAcknowledge').onclick = handler
  78. }
  79. }
  80. /**
  81. * Set the onclick handler of the overlay dismiss button.
  82. * If the handler is null, a default handler will be added.
  83. *
  84. * @param {function} handler
  85. */
  86. function setDismissHandler(handler){
  87. if(handler == null){
  88. document.getElementById('overlayDismiss').onclick = () => {
  89. toggleOverlay(false)
  90. }
  91. } else {
  92. document.getElementById('overlayDismiss').onclick = handler
  93. }
  94. }
  95. /* Server Select View */
  96. document.addEventListener('keydown', (e) => {
  97. if(document.getElementById('serverSelectContent').style.display !== 'none'){
  98. console.debug('ServSelLi Keydown Called:', document.getElementById('serverSelectContent').style.display)
  99. if(e.key === 'Escape'){
  100. document.getElementById('serverSelectCancel').click()
  101. } else if(e.key === 'Enter'){
  102. document.getElementById('serverSelectConfirm').click()
  103. }
  104. } else if(document.getElementById('accountSelectContent').style.display !== 'none'){
  105. console.debug('ServSelLi Keydown Called:', document.getElementById('accountSelectContent').style.display)
  106. if(e.key === 'Escape'){
  107. document.getElementById('accountSelectCancel').click()
  108. } else if(e.key === 'Enter'){
  109. document.getElementById('accountSelectConfirm').click()
  110. }
  111. }
  112. })
  113. document.getElementById('serverSelectConfirm').addEventListener('click', () => {
  114. const listings = document.getElementsByClassName('serverListing')
  115. for(let i=0; i<listings.length; i++){
  116. if(listings[i].hasAttribute('selected')){
  117. const serv = AssetGuard.getServerById(listings[i].getAttribute('servid'))
  118. ConfigManager.setSelectedServer(serv != null ? serv.id : null)
  119. ConfigManager.save()
  120. updateSelectedServer(serv != null ? serv.name : null)
  121. setLaunchEnabled(serv != null)
  122. refreshServerStatus(true)
  123. toggleOverlay(false)
  124. return
  125. }
  126. }
  127. // None are selected? Not possible right? Meh, handle it.
  128. if(listings.length > 0){
  129. ConfigManager.setSelectedServer(listings[0].getAttribute('servid'))
  130. ConfigManager.save()
  131. updateSelectedServer()
  132. toggleOverlay(false)
  133. }
  134. })
  135. document.getElementById('accountSelectConfirm').addEventListener('click', () => {
  136. const listings = document.getElementsByClassName('accountListing')
  137. for(let i=0; i<listings.length; i++){
  138. if(listings[i].hasAttribute('selected')){
  139. const authAcc = ConfigManager.setSelectedAccount(listings[i].getAttribute('uuid'))
  140. ConfigManager.save()
  141. updateSelectedAccount(authAcc)
  142. toggleOverlay(false)
  143. validateSelectedAccount()
  144. return
  145. }
  146. }
  147. // None are selected? Not possible right? Meh, handle it.
  148. if(listings.length > 0){
  149. const authAcc = ConfigManager.setSelectedAccount(listings[0].getAttribute('uuid'))
  150. ConfigManager.save()
  151. updateSelectedAccount(authAcc)
  152. toggleOverlay(false)
  153. validateSelectedAccount()
  154. }
  155. })
  156. // Bind server select cancel button.
  157. document.getElementById('serverSelectCancel').addEventListener('click', () => {
  158. toggleOverlay(false)
  159. })
  160. document.getElementById('accountSelectCancel').addEventListener('click', () => {
  161. $('#accountSelectContent').fadeOut(250, () => {
  162. $('#overlayContent').fadeIn(250)
  163. })
  164. })
  165. function setServerListingHandlers(){
  166. const listings = Array.from(document.getElementsByClassName('serverListing'))
  167. listings.map((val) => {
  168. val.onclick = e => {
  169. if(val.hasAttribute('selected')){
  170. return
  171. }
  172. const cListings = document.getElementsByClassName('serverListing')
  173. for(let i=0; i<cListings.length; i++){
  174. if(cListings[i].hasAttribute('selected')){
  175. cListings[i].removeAttribute('selected')
  176. }
  177. }
  178. val.setAttribute('selected', '')
  179. document.activeElement.blur()
  180. }
  181. })
  182. }
  183. function setAccountListingHandlers(){
  184. const listings = Array.from(document.getElementsByClassName('accountListing'))
  185. listings.map((val) => {
  186. val.onclick = e => {
  187. if(val.hasAttribute('selected')){
  188. return
  189. }
  190. const cListings = document.getElementsByClassName('accountListing')
  191. for(let i=0; i<cListings.length; i++){
  192. if(cListings[i].hasAttribute('selected')){
  193. cListings[i].removeAttribute('selected')
  194. }
  195. }
  196. val.setAttribute('selected', '')
  197. document.activeElement.blur()
  198. }
  199. })
  200. }
  201. function populateServerListings(){
  202. const distro = AssetGuard.getDistributionData()
  203. const giaSel = ConfigManager.getSelectedServer()
  204. const servers = distro.servers
  205. let htmlString = ``
  206. for(let i=0; i<servers.length; i++){
  207. htmlString += `<button class="serverListing" servid="${servers[i].id}" ${servers[i].id === giaSel ? `selected` : ``}>
  208. <img class="serverListingImg" src="${servers[i].icon_url}"/>
  209. <div class="serverListingDetails">
  210. <span class="serverListingName">${servers[i].name}</span>
  211. <span class="serverListingDescription">${servers[i].description}</span>
  212. <div class="serverListingInfo">
  213. <div class="serverListingVersion">${servers[i].mc_version}</div>
  214. <div class="serverListingRevision">${servers[i].revision}</div>
  215. ${servers[i].default_selected ? `<div class="serverListingStarWrapper">
  216. <svg id="Layer_1" viewBox="0 0 107.45 104.74" width="20px" height="20px">
  217. <defs>
  218. <style>.cls-1{fill:#fff;}.cls-2{fill:none;stroke:#fff;stroke-miterlimit:10;}</style>
  219. </defs>
  220. <path class="cls-1" d="M100.93,65.54C89,62,68.18,55.65,63.54,52.13c2.7-5.23,18.8-19.2,28-27.55C81.36,31.74,63.74,43.87,58.09,45.3c-2.41-5.37-3.61-26.52-4.37-39-.77,12.46-2,33.64-4.36,39-5.7-1.46-23.3-13.57-33.49-20.72,9.26,8.37,25.39,22.36,28,27.55C39.21,55.68,18.47,62,6.52,65.55c12.32-2,33.63-6.06,39.34-4.9-.16,5.87-8.41,26.16-13.11,37.69,6.1-10.89,16.52-30.16,21-33.9,4.5,3.79,14.93,23.09,21,34C70,86.84,61.73,66.48,61.59,60.65,67.36,59.49,88.64,63.52,100.93,65.54Z"/>
  221. <circle class="cls-2" cx="53.73" cy="53.9" r="38"/>
  222. </svg>
  223. <span class="serverListingStarTooltip">Main Server</span>
  224. </div>` : ``}
  225. </div>
  226. </div>
  227. </button>`
  228. }
  229. document.getElementById('serverSelectListScrollable').innerHTML = htmlString
  230. }
  231. function populateAccountListings(){
  232. const accountsObj = ConfigManager.getAuthAccounts()
  233. const accounts = Array.from(Object.keys(accountsObj), v=>accountsObj[v]);
  234. let htmlString = ``
  235. for(let i=0; i<accounts.length; i++){
  236. htmlString += `<button class="accountListing" uuid="${accounts[i].uuid}" ${i===0 ? 'selected' : ''}>
  237. <img src="https://crafatar.com/renders/head/${accounts[i].uuid}?scale=2&default=MHF_Steve&overlay">
  238. <div class="accountListingName">${accounts[i].displayName}</div>
  239. </button>`
  240. }
  241. document.getElementById('accountSelectListScrollable').innerHTML = htmlString
  242. }
  243. function prepareServerSelectionList(){
  244. populateServerListings()
  245. setServerListingHandlers()
  246. }
  247. function prepareAccountSelectionList(){
  248. populateAccountListings()
  249. setAccountListingHandlers()
  250. }