uibinder.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. }, 250)
  67. }, 750)
  68. // Disable tabbing to the news container.
  69. initNews().then(() => {
  70. $("#newsContainer *").attr('tabindex', '-1')
  71. })
  72. }
  73. function showFatalStartupError(){
  74. setTimeout(() => {
  75. $('#loadingContainer').fadeOut(250, () => {
  76. document.getElementById('overlayContainer').style.background = 'none'
  77. setOverlayContent(
  78. 'Fatal Error: Unable to Load Distribution Index',
  79. '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.',
  80. 'Close'
  81. )
  82. setOverlayHandler(() => {
  83. const window = remote.getCurrentWindow()
  84. window.close()
  85. })
  86. toggleOverlay(true)
  87. })
  88. }, 750)
  89. }
  90. function onDistroRefresh(data){
  91. updateSelectedServer(AssetGuard.getServerById(ConfigManager.getSelectedServer()).name)
  92. refreshServerStatus()
  93. initNews()
  94. }
  95. function refreshDistributionIndex(remote, onSuccess, onError){
  96. if(remote){
  97. AssetGuard.refreshDistributionDataRemote(ConfigManager.getLauncherDirectory())
  98. .then(onSuccess)
  99. .catch(onError)
  100. } else {
  101. AssetGuard.refreshDistributionDataLocal(ConfigManager.getLauncherDirectory())
  102. .then(onSuccess)
  103. .catch(onError)
  104. }
  105. }
  106. async function validateSelectedAccount(){
  107. const selectedAcc = ConfigManager.getSelectedAccount()
  108. if(selectedAcc != null){
  109. const val = await AuthManager.validateSelected()
  110. if(!val){
  111. const accLen = Object.keys(ConfigManager.getAuthAccounts()).length
  112. setOverlayContent(
  113. 'Failed to Refresh Login',
  114. `We were unable to refresh the login for <strong>${selectedAcc.displayName}</strong>. Please ${accLen > 1 ? 'select another account or ' : ''} login again.`,
  115. 'Login',
  116. 'Select Another Account'
  117. )
  118. setOverlayHandler(() => {
  119. document.getElementById('loginUsername').value = selectedAcc.username
  120. validateEmail(selectedAcc.username)
  121. switchView(getCurrentView(), VIEWS.login)
  122. toggleOverlay(false)
  123. })
  124. setDismissHandler(() => {
  125. if(accLen > 2){
  126. prepareAccountSelectionList()
  127. $('#overlayContent').fadeOut(250, () => {
  128. $('#accountSelectContent').fadeIn(250)
  129. })
  130. } else {
  131. const accountsObj = ConfigManager.getAuthAccounts()
  132. const accounts = Array.from(Object.keys(accountsObj), v=>accountsObj[v]);
  133. const selectedUUID = ConfigManager.getSelectedAccount().uuid
  134. for(let i=0; i<accounts.length; i++){
  135. if(accounts[i].uuid !== selectedUUID){
  136. setSelectedAccount(accounts[i].uuid)
  137. toggleOverlay(false)
  138. validateSelectedAccount()
  139. }
  140. }
  141. }
  142. })
  143. toggleOverlay(true, accLen > 1)
  144. } else {
  145. return true
  146. }
  147. } else {
  148. return true
  149. }
  150. }
  151. /**
  152. * Temporary function to update the selected account along
  153. * with the relevent UI elements.
  154. *
  155. * @param {string} uuid The UUID of the account.
  156. */
  157. function setSelectedAccount(uuid){
  158. const authAcc = ConfigManager.setSelectedAccount(uuid)
  159. ConfigManager.save()
  160. updateSelectedAccount(authAcc)
  161. //validateSelectedAccount()
  162. }
  163. // Synchronous Listener
  164. document.addEventListener('readystatechange', function(){
  165. if (document.readyState === 'complete'){
  166. if(rscShouldLoad){
  167. if(!fatalStartupError){
  168. showMainUI()
  169. } else {
  170. showFatalStartupError()
  171. }
  172. }
  173. } else if(document.readyState === 'interactive'){
  174. //toggleOverlay(true, 'loadingContent')
  175. }
  176. /*if (document.readyState === 'interactive'){
  177. }*/
  178. }, false)
  179. // Actions that must be performed after the distribution index is downloaded.
  180. ipcRenderer.on('distributionIndexDone', (event, data) => {
  181. if(data != null) {
  182. if(document.readyState === 'complete'){
  183. showMainUI()
  184. } else {
  185. rscShouldLoad = true
  186. }
  187. } else {
  188. fatalStartupError = true
  189. if(document.readyState === 'complete'){
  190. showFatalStartupError()
  191. } else {
  192. rscShouldLoad = true
  193. }
  194. }
  195. })