uibinder.js 7.8 KB

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