uibinder.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. settings: 'settingsContainer',
  17. welcome: 'welcomeContainer'
  18. }
  19. // The currently shown view container.
  20. let currentView = VIEWS.landing
  21. /**
  22. * Switch launcher views.
  23. *
  24. * @param {string} current The ID of the current view container.
  25. * @param {*} next The ID of the next view container.
  26. * @param {*} currentFadeTime Optional. The fade out time for the current view.
  27. * @param {*} nextFadeTime Optional. The fade in time for the next view.
  28. * @param {*} onCurrentFade Optional. Callback function to execute when the current
  29. * view fades out.
  30. * @param {*} onNextFade Optional. Callback function to execute when the next view
  31. * fades in.
  32. */
  33. function switchView(current, next, currentFadeTime = 500, nextFadeTime = 500, onCurrentFade = () => {}, onNextFade = () => {}){
  34. currentView = current
  35. $(`#${current}`).fadeOut(currentFadeTime, () => {
  36. onCurrentFade()
  37. $(`#${next}`).fadeIn(nextFadeTime, () => {
  38. onNextFade()
  39. })
  40. })
  41. }
  42. /**
  43. * Get the currently shown view container.
  44. *
  45. * @returns {string} The currently shown view container.
  46. */
  47. function getCurrentView(){
  48. return currentView
  49. }
  50. function showMainUI(){
  51. updateSelectedServer(AssetGuard.getServerById(ConfigManager.getSelectedServer()).name)
  52. refreshServerStatus()
  53. setTimeout(() => {
  54. document.getElementById('frameBar').style.backgroundColor = 'rgba(1, 2, 1, 0.5)'
  55. document.body.style.backgroundImage = `url('assets/images/backgrounds/${document.body.getAttribute('bkid')}.jpg')`
  56. $('#main').show()
  57. //validateSelectedAccount()
  58. if(ConfigManager.isFirstLaunch()){
  59. $('#welcomeContainer').fadeIn(1000)
  60. } else {
  61. $('#landingContainer').fadeIn(1000)
  62. }
  63. setTimeout(() => {
  64. $('#loadingContainer').fadeOut(500, () => {
  65. $('#loadSpinnerImage').removeClass('rotating')
  66. })
  67. }, 250)
  68. }, 750)
  69. // Disable tabbing to the news container.
  70. initNews().then(() => {
  71. $("#newsContainer *").attr('tabindex', '-1')
  72. })
  73. }
  74. function showFatalStartupError(){
  75. setTimeout(() => {
  76. $('#loadingContainer').fadeOut(250, () => {
  77. document.getElementById('overlayContainer').style.background = 'none'
  78. setOverlayContent(
  79. 'Fatal Error: Unable to Load Distribution Index',
  80. '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.',
  81. 'Close'
  82. )
  83. setOverlayHandler(() => {
  84. const window = remote.getCurrentWindow()
  85. window.close()
  86. })
  87. toggleOverlay(true)
  88. })
  89. }, 750)
  90. }
  91. function onDistroRefresh(data){
  92. updateSelectedServer(AssetGuard.getServerById(ConfigManager.getSelectedServer()).name)
  93. refreshServerStatus()
  94. initNews()
  95. }
  96. function refreshDistributionIndex(remote, onSuccess, onError){
  97. if(remote){
  98. AssetGuard.refreshDistributionDataRemote(ConfigManager.getLauncherDirectory())
  99. .then(onSuccess)
  100. .catch(onError)
  101. } else {
  102. AssetGuard.refreshDistributionDataLocal(ConfigManager.getLauncherDirectory())
  103. .then(onSuccess)
  104. .catch(onError)
  105. }
  106. }
  107. async function validateSelectedAccount(){
  108. const selectedAcc = ConfigManager.getSelectedAccount()
  109. if(selectedAcc != null){
  110. const val = await AuthManager.validateSelected()
  111. if(!val){
  112. const accLen = Object.keys(ConfigManager.getAuthAccounts()).length
  113. setOverlayContent(
  114. 'Failed to Refresh Login',
  115. `We were unable to refresh the login for <strong>${selectedAcc.displayName}</strong>. Please ${accLen > 1 ? 'select another account or ' : ''} login again.`,
  116. 'Login',
  117. 'Select Another Account'
  118. )
  119. setOverlayHandler(() => {
  120. document.getElementById('loginUsername').value = selectedAcc.username
  121. validateEmail(selectedAcc.username)
  122. switchView(getCurrentView(), VIEWS.login)
  123. toggleOverlay(false)
  124. })
  125. setDismissHandler(() => {
  126. if(accLen > 2){
  127. prepareAccountSelectionList()
  128. $('#overlayContent').fadeOut(250, () => {
  129. $('#accountSelectContent').fadeIn(250)
  130. })
  131. } else {
  132. const accountsObj = ConfigManager.getAuthAccounts()
  133. const accounts = Array.from(Object.keys(accountsObj), v=>accountsObj[v]);
  134. const selectedUUID = ConfigManager.getSelectedAccount().uuid
  135. for(let i=0; i<accounts.length; i++){
  136. if(accounts[i].uuid !== selectedUUID){
  137. setSelectedAccount(accounts[i].uuid)
  138. toggleOverlay(false)
  139. validateSelectedAccount()
  140. }
  141. }
  142. }
  143. })
  144. toggleOverlay(true, accLen > 1)
  145. } else {
  146. return true
  147. }
  148. } else {
  149. return true
  150. }
  151. }
  152. /**
  153. * Temporary function to update the selected account along
  154. * with the relevent UI elements.
  155. *
  156. * @param {string} uuid The UUID of the account.
  157. */
  158. function setSelectedAccount(uuid){
  159. const authAcc = ConfigManager.setSelectedAccount(uuid)
  160. ConfigManager.save()
  161. updateSelectedAccount(authAcc)
  162. //validateSelectedAccount()
  163. }
  164. // Synchronous Listener
  165. document.addEventListener('readystatechange', function(){
  166. if (document.readyState === 'complete'){
  167. if(rscShouldLoad){
  168. if(!fatalStartupError){
  169. showMainUI()
  170. } else {
  171. showFatalStartupError()
  172. }
  173. }
  174. } else if(document.readyState === 'interactive'){
  175. //toggleOverlay(true, 'loadingContent')
  176. }
  177. /*if (document.readyState === 'interactive'){
  178. }*/
  179. }, false)
  180. // Actions that must be performed after the distribution index is downloaded.
  181. ipcRenderer.on('distributionIndexDone', (event, data) => {
  182. if(data != null) {
  183. if(document.readyState === 'complete'){
  184. showMainUI()
  185. } else {
  186. rscShouldLoad = true
  187. }
  188. } else {
  189. fatalStartupError = true
  190. if(document.readyState === 'complete'){
  191. showFatalStartupError()
  192. } else {
  193. rscShouldLoad = true
  194. }
  195. }
  196. })