uibinder.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Initialize UI functions which depend on internal modules.
  3. * Loaded after core UI functions are initialized in uicore.js.
  4. */
  5. // Requirements
  6. const path = require('path')
  7. const AuthManager = require('./assets/js/authmanager.js')
  8. const {AssetGuard} = require('./assets/js/assetguard.js')
  9. const ConfigManager = require('./assets/js/configmanager.js')
  10. let rscShouldLoad = false
  11. let fatalStartupError = false
  12. // Mapping of each view to their container IDs.
  13. const VIEWS = {
  14. landing: 'landingContainer',
  15. login: 'loginContainer',
  16. welcome: 'welcomeContainer'
  17. }
  18. // The currently shown view container.
  19. let currentView = VIEWS.landing
  20. /**
  21. * Switch launcher views.
  22. *
  23. * @param {string} current The ID of the current view container.
  24. * @param {*} next The ID of the next view container.
  25. * @param {*} currentFadeTime Optional. The fade out time for the current view.
  26. * @param {*} nextFadeTime Optional. The fade in time for the next view.
  27. * @param {*} onCurrentFade Optional. Callback function to execute when the current
  28. * view fades out.
  29. * @param {*} onNextFade Optional. Callback function to execute when the next view
  30. * fades in.
  31. */
  32. function switchView(current, next, currentFadeTime = 500, nextFadeTime = 500, onCurrentFade = () => {}, onNextFade = () => {}){
  33. currentView = current
  34. $(`#${current}`).fadeOut(currentFadeTime, () => {
  35. onCurrentFade()
  36. $(`#${next}`).fadeIn(nextFadeTime, () => {
  37. onNextFade()
  38. })
  39. })
  40. }
  41. /**
  42. * Get the currently shown view container.
  43. *
  44. * @returns {string} The currently shown view container.
  45. */
  46. function getCurrentView(){
  47. return currentView
  48. }
  49. function showMainUI(){
  50. updateSelectedServer(AssetGuard.getServerById(ConfigManager.getSelectedServer()).name)
  51. refreshServerStatus()
  52. setTimeout(() => {
  53. document.getElementById('frameBar').style.backgroundColor = 'rgba(1, 2, 1, 0.5)'
  54. document.body.style.backgroundImage = `url('assets/images/backgrounds/${document.body.getAttribute('bkid')}.jpg')`
  55. $('#main').show()
  56. //validateSelectedAccount()
  57. if(ConfigManager.isFirstLaunch()){
  58. $('#welcomeContainer').fadeIn(1000)
  59. } else {
  60. $('#landingContainer').fadeIn(1000)
  61. }
  62. setTimeout(() => {
  63. $('#loadingContainer').fadeOut(500, () => {
  64. $('#loadSpinnerImage').removeClass('rotating')
  65. })
  66. }, 500)
  67. }, 750)
  68. initNews()
  69. }
  70. function showFatalStartupError(){
  71. setTimeout(() => {
  72. $('#loadingContainer').fadeOut(250, () => {
  73. document.getElementById('overlayContainer').style.background = 'none'
  74. setOverlayContent(
  75. 'Fatal Error: Unable to Load Distribution Index',
  76. 'A connection could not be established to our servers to download the distribution index. No local copies were available to load. <br><br>The distribution index is an essential file which provides the latest server information. The launcher is unable to start without it. Ensure you are connected to the internet and relaunch the application.',
  77. 'Close'
  78. )
  79. setOverlayHandler(() => {
  80. const window = remote.getCurrentWindow()
  81. window.close()
  82. })
  83. toggleOverlay(true)
  84. })
  85. }, 750)
  86. }
  87. function onDistroRefresh(data){
  88. updateSelectedServer(AssetGuard.getServerById(ConfigManager.getSelectedServer()).name)
  89. refreshServerStatus()
  90. initNews()
  91. }
  92. function refreshDistributionIndex(remote, onSuccess, onError){
  93. if(remote){
  94. AssetGuard.refreshDistributionDataRemote(ConfigManager.getLauncherDirectory())
  95. .then(onSuccess)
  96. .catch(onError)
  97. } else {
  98. AssetGuard.refreshDistributionDataLocal(ConfigManager.getLauncherDirectory())
  99. .then(onSuccess)
  100. .catch(onError)
  101. }
  102. }
  103. async function validateSelectedAccount(){
  104. const selectedAcc = ConfigManager.getSelectedAccount()
  105. if(selectedAcc != null){
  106. const val = await AuthManager.validateSelected()
  107. if(!val){
  108. const accLen = Object.keys(ConfigManager.getAuthAccounts()).length
  109. setOverlayContent(
  110. 'Failed to Refresh Login',
  111. `We were unable to refresh the login for <strong>${selectedAcc.displayName}</strong>. Please ${accLen > 1 ? 'select another account or ' : ''} login again.`,
  112. 'Login',
  113. 'Select Another Account'
  114. )
  115. setOverlayHandler(() => {
  116. document.getElementById('loginUsername').value = selectedAcc.username
  117. validateEmail(selectedAcc.username)
  118. switchView(getCurrentView(), VIEWS.login)
  119. toggleOverlay(false)
  120. })
  121. setDismissHandler(() => {
  122. if(accLen > 2){
  123. prepareAccountSelectionList()
  124. $('#overlayContent').fadeOut(250, () => {
  125. $('#accountSelectContent').fadeIn(250)
  126. })
  127. } else {
  128. const accountsObj = ConfigManager.getAuthAccounts()
  129. const accounts = Array.from(Object.keys(accountsObj), v=>accountsObj[v]);
  130. const selectedUUID = ConfigManager.getSelectedAccount().uuid
  131. for(let i=0; i<accounts.length; i++){
  132. if(accounts[i].uuid !== selectedUUID){
  133. setSelectedAccount(accounts[i].uuid)
  134. toggleOverlay(false)
  135. validateSelectedAccount()
  136. }
  137. }
  138. }
  139. })
  140. toggleOverlay(true, accLen > 1)
  141. } else {
  142. return true
  143. }
  144. } else {
  145. return true
  146. }
  147. }
  148. function setSelectedAccount(uuid){
  149. const authAcc = ConfigManager.setSelectedAccount(uuid)
  150. ConfigManager.save()
  151. updateSelectedAccount(authAcc)
  152. //validateSelectedAccount()
  153. }
  154. // Synchronous Listener
  155. document.addEventListener('readystatechange', function(){
  156. if (document.readyState === 'complete'){
  157. if(rscShouldLoad){
  158. if(!fatalStartupError){
  159. showMainUI()
  160. } else {
  161. showFatalStartupError()
  162. }
  163. }
  164. } else if(document.readyState === 'interactive'){
  165. //toggleOverlay(true, 'loadingContent')
  166. }
  167. /*if (document.readyState === 'interactive'){
  168. }*/
  169. }, false)
  170. // Actions that must be performed after the distribution index is downloaded.
  171. ipcRenderer.on('distributionIndexDone', (event, data) => {
  172. if(data != null) {
  173. if(document.readyState === 'complete'){
  174. showMainUI()
  175. } else {
  176. rscShouldLoad = true
  177. }
  178. } else {
  179. fatalStartupError = true
  180. if(document.readyState === 'complete'){
  181. showFatalStartupError()
  182. } else {
  183. rscShouldLoad = true
  184. }
  185. }
  186. })