overlay.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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({
  32. duration: 250,
  33. start: () => {
  34. if(getCurrentView() === VIEWS.settings){
  35. document.getElementById('settingsContainer').style.backgroundColor = 'transparent'
  36. }
  37. }
  38. })
  39. } else {
  40. document.getElementById('main').removeAttribute('overlay')
  41. // Make things tabbable.
  42. $("#main *").removeAttr('tabindex')
  43. $('#overlayContainer').fadeOut({
  44. duration: 250,
  45. start: () => {
  46. if(getCurrentView() === VIEWS.settings){
  47. document.getElementById('settingsContainer').style.backgroundColor = 'rgba(0, 0, 0, 0.50)'
  48. }
  49. },
  50. complete: () => {
  51. $('#' + content).parent().children().hide()
  52. $('#' + content).show()
  53. if(dismissable){
  54. $('#overlayDismiss').show()
  55. } else {
  56. $('#overlayDismiss').hide()
  57. }
  58. }
  59. })
  60. }
  61. }
  62. function toggleServerSelection(toggleState){
  63. prepareServerSelectionList()
  64. toggleOverlay(toggleState, 'serverSelectContent')
  65. }
  66. /**
  67. * Set the content of the overlay.
  68. *
  69. * @param {string} title Overlay title text.
  70. * @param {string} description Overlay description text.
  71. * @param {string} acknowledge Acknowledge button text.
  72. * @param {string} dismiss Dismiss button text.
  73. */
  74. function setOverlayContent(title, description, acknowledge, dismiss = 'Dismiss'){
  75. document.getElementById('overlayTitle').innerHTML = title
  76. document.getElementById('overlayDesc').innerHTML = description
  77. document.getElementById('overlayAcknowledge').innerHTML = acknowledge
  78. document.getElementById('overlayDismiss').innerHTML = dismiss
  79. }
  80. /**
  81. * Set the onclick handler of the overlay acknowledge button.
  82. * If the handler is null, a default handler will be added.
  83. *
  84. * @param {function} handler
  85. */
  86. function setOverlayHandler(handler){
  87. if(handler == null){
  88. document.getElementById('overlayAcknowledge').onclick = () => {
  89. toggleOverlay(false)
  90. }
  91. } else {
  92. document.getElementById('overlayAcknowledge').onclick = handler
  93. }
  94. }
  95. /**
  96. * Set the onclick handler of the overlay dismiss button.
  97. * If the handler is null, a default handler will be added.
  98. *
  99. * @param {function} handler
  100. */
  101. function setDismissHandler(handler){
  102. if(handler == null){
  103. document.getElementById('overlayDismiss').onclick = () => {
  104. toggleOverlay(false)
  105. }
  106. } else {
  107. document.getElementById('overlayDismiss').onclick = handler
  108. }
  109. }
  110. /* Server Select View */
  111. document.addEventListener('keydown', (e) => {
  112. if(document.getElementById('serverSelectContent').style.display !== 'none'){
  113. console.debug('ServSelLi Keydown Called:', document.getElementById('serverSelectContent').style.display)
  114. if(e.key === 'Escape'){
  115. document.getElementById('serverSelectCancel').click()
  116. } else if(e.key === 'Enter'){
  117. document.getElementById('serverSelectConfirm').click()
  118. }
  119. } else if(document.getElementById('accountSelectContent').style.display !== 'none'){
  120. console.debug('ServSelLi Keydown Called:', document.getElementById('accountSelectContent').style.display)
  121. if(e.key === 'Escape'){
  122. document.getElementById('accountSelectCancel').click()
  123. } else if(e.key === 'Enter'){
  124. document.getElementById('accountSelectConfirm').click()
  125. }
  126. }
  127. })
  128. document.getElementById('serverSelectConfirm').addEventListener('click', () => {
  129. const listings = document.getElementsByClassName('serverListing')
  130. for(let i=0; i<listings.length; i++){
  131. if(listings[i].hasAttribute('selected')){
  132. const serv = DistroManager.getDistribution().getServer(listings[i].getAttribute('servid'))
  133. ConfigManager.setSelectedServer(serv != null ? serv.getID() : null)
  134. ConfigManager.save()
  135. updateSelectedServer(serv != null ? serv.getName() : null)
  136. setLaunchEnabled(serv != null)
  137. refreshServerStatus(true)
  138. toggleOverlay(false)
  139. return
  140. }
  141. }
  142. // None are selected? Not possible right? Meh, handle it.
  143. if(listings.length > 0){
  144. ConfigManager.setSelectedServer(listings[0].getAttribute('servid'))
  145. ConfigManager.save()
  146. updateSelectedServer()
  147. toggleOverlay(false)
  148. }
  149. })
  150. document.getElementById('accountSelectConfirm').addEventListener('click', () => {
  151. const listings = document.getElementsByClassName('accountListing')
  152. for(let i=0; i<listings.length; i++){
  153. if(listings[i].hasAttribute('selected')){
  154. const authAcc = ConfigManager.setSelectedAccount(listings[i].getAttribute('uuid'))
  155. ConfigManager.save()
  156. updateSelectedAccount(authAcc)
  157. toggleOverlay(false)
  158. validateSelectedAccount()
  159. return
  160. }
  161. }
  162. // None are selected? Not possible right? Meh, handle it.
  163. if(listings.length > 0){
  164. const authAcc = ConfigManager.setSelectedAccount(listings[0].getAttribute('uuid'))
  165. ConfigManager.save()
  166. updateSelectedAccount(authAcc)
  167. toggleOverlay(false)
  168. validateSelectedAccount()
  169. }
  170. })
  171. // Bind server select cancel button.
  172. document.getElementById('serverSelectCancel').addEventListener('click', () => {
  173. toggleOverlay(false)
  174. })
  175. document.getElementById('accountSelectCancel').addEventListener('click', () => {
  176. $('#accountSelectContent').fadeOut(250, () => {
  177. $('#overlayContent').fadeIn(250)
  178. })
  179. })
  180. function setServerListingHandlers(){
  181. const listings = Array.from(document.getElementsByClassName('serverListing'))
  182. listings.map((val) => {
  183. val.onclick = e => {
  184. if(val.hasAttribute('selected')){
  185. return
  186. }
  187. const cListings = document.getElementsByClassName('serverListing')
  188. for(let i=0; i<cListings.length; i++){
  189. if(cListings[i].hasAttribute('selected')){
  190. cListings[i].removeAttribute('selected')
  191. }
  192. }
  193. val.setAttribute('selected', '')
  194. document.activeElement.blur()
  195. }
  196. })
  197. }
  198. function setAccountListingHandlers(){
  199. const listings = Array.from(document.getElementsByClassName('accountListing'))
  200. listings.map((val) => {
  201. val.onclick = e => {
  202. if(val.hasAttribute('selected')){
  203. return
  204. }
  205. const cListings = document.getElementsByClassName('accountListing')
  206. for(let i=0; i<cListings.length; i++){
  207. if(cListings[i].hasAttribute('selected')){
  208. cListings[i].removeAttribute('selected')
  209. }
  210. }
  211. val.setAttribute('selected', '')
  212. document.activeElement.blur()
  213. }
  214. })
  215. }
  216. function populateServerListings(){
  217. const distro = DistroManager.getDistribution()
  218. const giaSel = ConfigManager.getSelectedServer()
  219. const servers = distro.getServers()
  220. let htmlString = ``
  221. for(const serv of servers){
  222. htmlString += `<button class="serverListing" servid="${serv.getID()}" ${serv.getID() === giaSel ? `selected` : ``}>
  223. <img class="serverListingImg" src="${serv.getIcon()}"/>
  224. <div class="serverListingDetails">
  225. <span class="serverListingName">${serv.getName()}</span>
  226. <span class="serverListingDescription">${serv.getDescription()}</span>
  227. <div class="serverListingInfo">
  228. <div class="serverListingVersion">${serv.getMinecraftVersion()}</div>
  229. <div class="serverListingRevision">${serv.getVersion()}</div>
  230. ${serv.isMainServer() ? `<div class="serverListingStarWrapper">
  231. <svg id="Layer_1" viewBox="0 0 107.45 104.74" width="20px" height="20px">
  232. <defs>
  233. <style>.cls-1{fill:#fff;}.cls-2{fill:none;stroke:#fff;stroke-miterlimit:10;}</style>
  234. </defs>
  235. <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"/>
  236. <circle class="cls-2" cx="53.73" cy="53.9" r="38"/>
  237. </svg>
  238. <span class="serverListingStarTooltip">Main Server</span>
  239. </div>` : ``}
  240. </div>
  241. </div>
  242. </button>`
  243. }
  244. document.getElementById('serverSelectListScrollable').innerHTML = htmlString
  245. }
  246. function populateAccountListings(){
  247. const accountsObj = ConfigManager.getAuthAccounts()
  248. const accounts = Array.from(Object.keys(accountsObj), v=>accountsObj[v]);
  249. let htmlString = ``
  250. for(let i=0; i<accounts.length; i++){
  251. htmlString += `<button class="accountListing" uuid="${accounts[i].uuid}" ${i===0 ? 'selected' : ''}>
  252. <img src="https://crafatar.com/renders/head/${accounts[i].uuid}?scale=2&default=MHF_Steve&overlay">
  253. <div class="accountListingName">${accounts[i].displayName}</div>
  254. </button>`
  255. }
  256. document.getElementById('accountSelectListScrollable').innerHTML = htmlString
  257. }
  258. function prepareServerSelectionList(){
  259. populateServerListings()
  260. setServerListingHandlers()
  261. }
  262. function prepareAccountSelectionList(){
  263. populateAccountListings()
  264. setAccountListingHandlers()
  265. }