overlay.js 11 KB

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