settings.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. const settingsNavDone = document.getElementById('settingsNavDone')
  2. // Account Management Tab
  3. const settingsAddAccount = document.getElementById('settingsAddAccount')
  4. const settingsCurrentAccounts = document.getElementById('settingsCurrentAccounts')
  5. // Minecraft Tab
  6. const settingsGameWidth = document.getElementById('settingsGameWidth')
  7. const settingsGameHeight = document.getElementById('settingsGameHeight')
  8. const settingsState = {
  9. invalid: new Set()
  10. }
  11. /**
  12. * General Settings Functions
  13. */
  14. /**
  15. * Bind value validators to the settings UI elements. These will
  16. * validate against the criteria defined in the ConfigManager (if
  17. * and). If the value is invalid, the UI will reflect this and saving
  18. * will be disabled until the value is corrected. This is an automated
  19. * process. More complex UI may need to be bound separately.
  20. */
  21. function initSettingsValidators(){
  22. const sEls = document.getElementById('settingsContainer').querySelectorAll('[cValue]')
  23. Array.from(sEls).map((v, index, arr) => {
  24. const vFn = ConfigManager['validate' + v.getAttribute('cValue')]
  25. if(typeof vFn === 'function'){
  26. if(v.tagName === 'INPUT'){
  27. if(v.type === 'number' || v.type === 'text'){
  28. v.addEventListener('keyup', (e) => {
  29. const v = e.target
  30. if(!vFn(v.value)){
  31. settingsState.invalid.add(v.id)
  32. v.setAttribute('error', '')
  33. settingsSaveDisabled(true)
  34. } else {
  35. if(v.hasAttribute('error')){
  36. v.removeAttribute('error')
  37. settingsState.invalid.delete(v.id)
  38. if(settingsState.invalid.size === 0){
  39. settingsSaveDisabled(false)
  40. }
  41. }
  42. }
  43. })
  44. }
  45. }
  46. }
  47. })
  48. }
  49. /**
  50. * Load configuration values onto the UI. This is an automated process.
  51. */
  52. function initSettingsValues(){
  53. const sEls = document.getElementById('settingsContainer').querySelectorAll('[cValue]')
  54. Array.from(sEls).map((v, index, arr) => {
  55. const gFn = ConfigManager['get' + v.getAttribute('cValue')]
  56. if(typeof gFn === 'function'){
  57. if(v.tagName === 'INPUT'){
  58. if(v.type === 'number' || v.type === 'text'){
  59. v.value = gFn()
  60. } else if(v.type === 'checkbox'){
  61. v.checked = gFn()
  62. }
  63. }
  64. }
  65. })
  66. }
  67. /**
  68. * Save the settings values.
  69. */
  70. function saveSettingsValues(){
  71. const sEls = document.getElementById('settingsContainer').querySelectorAll('[cValue]')
  72. Array.from(sEls).map((v, index, arr) => {
  73. const sFn = ConfigManager['set' + v.getAttribute('cValue')]
  74. if(typeof sFn === 'function'){
  75. if(v.tagName === 'INPUT'){
  76. if(v.type === 'number' || v.type === 'text'){
  77. sFn(v.value)
  78. } else if(v.type === 'checkbox'){
  79. sFn(v.checked)
  80. // Special Conditions
  81. const cVal = v.getAttribute('cValue')
  82. if(cVal === 'AllowPrerelease'){
  83. changeAllowPrerelease(v.checked)
  84. }
  85. }
  86. }
  87. }
  88. })
  89. }
  90. let selectedTab = 'settingsTabAccount'
  91. /**
  92. * Bind functionality for the settings navigation items.
  93. */
  94. function setupSettingsTabs(){
  95. Array.from(document.getElementsByClassName('settingsNavItem')).map((val) => {
  96. val.onclick = (e) => {
  97. if(val.hasAttribute('selected')){
  98. return
  99. }
  100. const navItems = document.getElementsByClassName('settingsNavItem')
  101. for(let i=0; i<navItems.length; i++){
  102. if(navItems[i].hasAttribute('selected')){
  103. navItems[i].removeAttribute('selected')
  104. }
  105. }
  106. val.setAttribute('selected', '')
  107. let prevTab = selectedTab
  108. selectedTab = val.getAttribute('rSc')
  109. $(`#${prevTab}`).fadeOut(250, () => {
  110. $(`#${selectedTab}`).fadeIn(250)
  111. })
  112. }
  113. })
  114. }
  115. /**
  116. * Set if the settings save (done) button is disabled.
  117. *
  118. * @param {boolean} v True to disable, false to enable.
  119. */
  120. function settingsSaveDisabled(v){
  121. settingsNavDone.disabled = v
  122. }
  123. /* Closes the settings view and saves all data. */
  124. settingsNavDone.onclick = () => {
  125. saveSettingsValues()
  126. ConfigManager.save()
  127. switchView(getCurrentView(), VIEWS.landing)
  128. }
  129. /**
  130. * Account Management Tab
  131. */
  132. // Bind the add account button.
  133. settingsAddAccount.onclick = (e) => {
  134. switchView(getCurrentView(), VIEWS.login, 500, 500, () => {
  135. loginViewOnCancel = VIEWS.settings
  136. loginViewOnSuccess = VIEWS.settings
  137. loginCancelEnabled(true)
  138. })
  139. }
  140. /**
  141. * Bind functionality for the account selection buttons. If another account
  142. * is selected, the UI of the previously selected account will be updated.
  143. */
  144. function bindAuthAccountSelect(){
  145. Array.from(document.getElementsByClassName('settingsAuthAccountSelect')).map((val) => {
  146. val.onclick = (e) => {
  147. if(val.hasAttribute('selected')){
  148. return
  149. }
  150. const selectBtns = document.getElementsByClassName('settingsAuthAccountSelect')
  151. for(let i=0; i<selectBtns.length; i++){
  152. if(selectBtns[i].hasAttribute('selected')){
  153. selectBtns[i].removeAttribute('selected')
  154. selectBtns[i].innerHTML = 'Select Account'
  155. }
  156. }
  157. val.setAttribute('selected', '')
  158. val.innerHTML = 'Selected Account &#10004;'
  159. setSelectedAccount(val.closest('.settingsAuthAccount').getAttribute('uuid'))
  160. }
  161. })
  162. }
  163. /**
  164. * Bind functionality for the log out button. If the logged out account was
  165. * the selected account, another account will be selected and the UI will
  166. * be updated accordingly.
  167. */
  168. function bindAuthAccountLogOut(){
  169. Array.from(document.getElementsByClassName('settingsAuthAccountLogOut')).map((val) => {
  170. val.onclick = (e) => {
  171. let isLastAccount = false
  172. if(Object.keys(ConfigManager.getAuthAccounts()).length === 1){
  173. isLastAccount = true
  174. setOverlayContent(
  175. 'Warning<br>This is Your Last Account',
  176. 'In order to use the launcher you must be logged into at least one account. You will need to login again after.<br><br>Are you sure you want to log out?',
  177. 'I\'m Sure',
  178. 'Cancel'
  179. )
  180. setOverlayHandler(() => {
  181. processLogOut(val, isLastAccount)
  182. switchView(getCurrentView(), VIEWS.login)
  183. toggleOverlay(false)
  184. })
  185. setDismissHandler(() => {
  186. toggleOverlay(false)
  187. })
  188. toggleOverlay(true, true)
  189. } else {
  190. processLogOut(val, isLastAccount)
  191. }
  192. }
  193. })
  194. }
  195. /**
  196. * Process a log out.
  197. *
  198. * @param {Element} val The log out button element.
  199. * @param {boolean} isLastAccount If this logout is on the last added account.
  200. */
  201. function processLogOut(val, isLastAccount){
  202. const parent = val.closest('.settingsAuthAccount')
  203. const uuid = parent.getAttribute('uuid')
  204. const prevSelAcc = ConfigManager.getSelectedAccount()
  205. AuthManager.removeAccount(uuid).then(() => {
  206. if(!isLastAccount && uuid === prevSelAcc.uuid){
  207. const selAcc = ConfigManager.getSelectedAccount()
  208. refreshAuthAccountSelected(selAcc.uuid)
  209. updateSelectedAccount(selAcc)
  210. validateSelectedAccount()
  211. }
  212. })
  213. $(parent).fadeOut(250, () => {
  214. parent.remove()
  215. })
  216. }
  217. /**
  218. * Refreshes the status of the selected account on the auth account
  219. * elements.
  220. *
  221. * @param {string} uuid The UUID of the new selected account.
  222. */
  223. function refreshAuthAccountSelected(uuid){
  224. Array.from(document.getElementsByClassName('settingsAuthAccount')).map((val) => {
  225. const selBtn = val.getElementsByClassName('settingsAuthAccountSelect')[0]
  226. if(uuid === val.getAttribute('uuid')){
  227. selBtn.setAttribute('selected', '')
  228. selBtn.innerHTML = 'Selected Account &#10004;'
  229. } else {
  230. if(selBtn.hasAttribute('selected')){
  231. selBtn.removeAttribute('selected')
  232. }
  233. selBtn.innerHTML = 'Select Account'
  234. }
  235. })
  236. }
  237. /**
  238. * Add auth account elements for each one stored in the authentication database.
  239. */
  240. function populateAuthAccounts(){
  241. const authAccounts = ConfigManager.getAuthAccounts()
  242. const authKeys = Object.keys(authAccounts)
  243. const selectedUUID = ConfigManager.getSelectedAccount().uuid
  244. let authAccountStr = ``
  245. authKeys.map((val) => {
  246. const acc = authAccounts[val]
  247. authAccountStr += `<div class="settingsAuthAccount" uuid="${acc.uuid}">
  248. <div class="settingsAuthAccountLeft">
  249. <img class="settingsAuthAccountImage" alt="${acc.displayName}" src="https://crafatar.com/renders/body/${acc.uuid}?scale=3&default=MHF_Steve&overlay">
  250. </div>
  251. <div class="settingsAuthAccountRight">
  252. <div class="settingsAuthAccountDetails">
  253. <div class="settingsAuthAccountDetailPane">
  254. <div class="settingsAuthAccountDetailTitle">Username</div>
  255. <div class="settingsAuthAccountDetailValue">${acc.displayName}</div>
  256. </div>
  257. <div class="settingsAuthAccountDetailPane">
  258. <div class="settingsAuthAccountDetailTitle">${acc.displayName === acc.username ? 'UUID' : 'Email'}</div>
  259. <div class="settingsAuthAccountDetailValue">${acc.displayName === acc.username ? acc.uuid : acc.username}</div>
  260. </div>
  261. </div>
  262. <div class="settingsAuthAccountActions">
  263. <button class="settingsAuthAccountSelect" ${selectedUUID === acc.uuid ? 'selected>Selected Account &#10004;' : '>Select Account'}</button>
  264. <div class="settingsAuthAccountWrapper">
  265. <button class="settingsAuthAccountLogOut">Log Out</button>
  266. </div>
  267. </div>
  268. </div>
  269. </div>`
  270. })
  271. settingsCurrentAccounts.innerHTML = authAccountStr
  272. }
  273. function prepareAccountsTab() {
  274. populateAuthAccounts()
  275. bindAuthAccountSelect()
  276. bindAuthAccountLogOut()
  277. }
  278. /**
  279. * Minecraft Tab
  280. */
  281. /**
  282. * Disable decimals, negative signs, and scientific notation.
  283. */
  284. settingsGameWidth.addEventListener('keydown', (e) => {
  285. if(/[-\.eE]/.test(e.key)){
  286. e.preventDefault()
  287. }
  288. })
  289. settingsGameHeight.addEventListener('keydown', (e) => {
  290. if(/[-\.eE]/.test(e.key)){
  291. e.preventDefault()
  292. }
  293. })
  294. /**
  295. * Settings preparation functions.
  296. */
  297. /**
  298. * Prepare the entire settings UI.
  299. *
  300. * @param {boolean} first Whether or not it is the first load.
  301. */
  302. function prepareSettings(first = false) {
  303. if(first){
  304. setupSettingsTabs()
  305. initSettingsValidators()
  306. }
  307. initSettingsValues()
  308. prepareAccountsTab()
  309. }
  310. // Prepare the settings UI on startup.
  311. prepareSettings(true)