class SettingsGroup { constructor( configName, toggleFunction, setCallback = null, updateCallback = null ) { this.configName = configName; this.configValue = config[configName]; if (this.configValue === true || this.configValue === false) { this.onOff = true; } else { this.onOff = false; } this.toggleFunction = toggleFunction; this.setCallback = setCallback; this.updateCallback = updateCallback; this.updateButton(); $(document).on( "click", `.pageSettings .section.${this.configName} .button`, (e) => { if (this.onOff) { if ($(e.currentTarget).hasClass("on")) { this.toggleFunction(true); } else { this.toggleFunction(false); } this.updateButton(); if (this.setCallback !== null) this.setCallback(); } else { let value = $(e.currentTarget).attr(configName); let params = $(e.currentTarget).attr("params"); if (params === undefined) { this.toggleFunction(value); } else { this.toggleFunction(value, ...params); } this.updateButton(); if (this.setCallback !== null) this.setCallback(); } } ); } updateButton() { this.configValue = config[this.configName]; $(`.pageSettings .section.${this.configName} .button`).removeClass( "active" ); if (this.onOff) { let onoffstring; if (this.configValue) { onoffstring = "on"; } else { onoffstring = "off"; } $( `.pageSettings .section.${this.configName} .buttons .button.${onoffstring}` ).addClass("active"); } else { $( `.pageSettings .section.${this.configName} .button[${this.configName}='${this.configValue}']` ).addClass("active"); } if (this.updateCallback !== null) this.updateCallback(); } } let settingsGroups = {}; settingsGroups.smoothCaret = new SettingsGroup("smoothCaret", setSmoothCaret); settingsGroups.difficulty = new SettingsGroup("difficulty", setDifficulty); settingsGroups.quickTab = new SettingsGroup("quickTab", setQuickTabMode); settingsGroups.showLiveWpm = new SettingsGroup( "showLiveWpm", setShowLiveWpm, () => { settingsGroups.keymapMode.updateButton(); } ); settingsGroups.showTimerProgress = new SettingsGroup( "showTimerProgress", setShowTimerProgress ); settingsGroups.keymapMode = new SettingsGroup( "keymapMode", changeKeymapMode, () => { settingsGroups.showLiveWpm.updateButton(); }, () => { if (config.keymapMode === "off") { $(".pageSettings .section.keymapLayout").addClass("hidden"); } else { $(".pageSettings .section.keymapLayout").removeClass("hidden"); } } ); settingsGroups.keymapLayout = new SettingsGroup( "keymapLayout", changeKeymapLayout ); settingsGroups.quickTab = new SettingsGroup( "showKeyTips", setKeyTips, null, () => { if (config.showKeyTips) { $(".pageSettings .tip").removeClass("hidden"); } else { $(".pageSettings .tip").addClass("hidden"); } } ); settingsGroups.freedomMode = new SettingsGroup( "freedomMode", setFreedomMode, () => { settingsGroups.confidenceMode.updateButton(); } ); settingsGroups.confidenceMode = new SettingsGroup( "confidenceMode", setConfidenceMode, () => { settingsGroups.freedomMode.updateButton(); } ); settingsGroups.blindMode = new SettingsGroup("blindMode", setBlindMode); settingsGroups.quickEnd = new SettingsGroup("quickEnd", setQuickEnd); settingsGroups.alwaysShowWordsHistory = new SettingsGroup( "alwaysShowWordsHistory", setAlwaysShowWordsHistory ); settingsGroups.flipTestColors = new SettingsGroup( "flipTestColors", setFlipTestColors ); settingsGroups.colorfulMode = new SettingsGroup( "colorfulMode", setColorfulMode ); settingsGroups.randomTheme = new SettingsGroup("randomTheme", setRandomTheme); settingsGroups.stopOnError = new SettingsGroup("stopOnError", setStopOnError); settingsGroups.showAllLines = new SettingsGroup( "showAllLines", setShowAllLines ); settingsGroups.smoothLineScroll = new SettingsGroup( "smoothLineScroll", setSmoothLineScroll ); settingsGroups.layout = new SettingsGroup("layout", changeLayout); settingsGroups.language = new SettingsGroup("language", changeLanguage); settingsGroups.fontSize = new SettingsGroup("fontSize", changeFontSize); settingsGroups.caretStyle = new SettingsGroup("caretStyle", setCaretStyle); settingsGroups.timerStyle = new SettingsGroup("timerStyle", setTimerStyle); settingsGroups.timerOpacity = new SettingsGroup( "timerOpacity", setTimerOpacity ); settingsGroups.timerColor = new SettingsGroup("timerColor", setTimerColor); settingsGroups.fontFamily = new SettingsGroup("fontFamily", setFontFamily); settingsGroups.alwaysShowDecimalPlaces = new SettingsGroup( "alwaysShowDecimalPlaces", setAlwaysShowDecimalPlaces ); fillSettingsPage(); async function fillSettingsPage() { let themesEl = $(".pageSettings .section.themes .buttons").empty(); getThemesList().then((themes) => { themes = themes.sort((a, b) => { let b1 = hexToHSL(a.bgColor); let b2 = hexToHSL(b.bgColor); return b2.lgt - b1.lgt; }); themes.forEach((theme) => { themesEl.append( `
` ); }); }); let langEl = $(".pageSettings .section.language .buttons").empty(); Object.keys(words).forEach((language) => { if (language === "english_10k") return; langEl.append( ` ` ); if (language === "english_expanded") { langEl.append( ` ` ); } }); let layoutEl = $(".pageSettings .section.layout .buttons").empty(); Object.keys(layouts).forEach((layout) => { layoutEl.append( ` ` ); }); let keymapEl = $(".pageSettings .section.keymapLayout .buttons").empty(); Object.keys(layouts).forEach((layout) => { if (layout.toString() != "default") { keymapEl.append( ` ` ); } }); let funboxEl = $(".pageSettings .section.funbox .buttons").empty(); funboxEl.append(` `); getFunboxList().then((funboxModes) => { funboxModes.forEach((funbox) => { if (funbox.name === "mirror") { funboxEl.append( ` ` ); } else { funboxEl.append( ` ` ); } }); }); let fontsEl = $(".pageSettings .section.fontFamily .buttons").empty(); getFontsList().then((fonts) => { fonts.forEach((font) => { fontsEl.append( ` ` ); }); }); } function updateSettingsPage() { Object.keys(settingsGroups).forEach((group) => { settingsGroups[group].updateButton(); }); refreshTagsSettingsSection(); setActiveFunboxButton(); setActiveThemeButton(); setActiveThemeTab(); setCustomThemeInputs(); updateDiscordSettingsSection(); } function showCustomThemeShare() { if ($("#customThemeShareWrapper").hasClass("hidden")) { let save = []; $.each( $(".pageSettings .section.customTheme [type='color']"), (index, element) => { save.push($(element).attr("value")); } ); $("#customThemeShareWrapper input").val(JSON.stringify(save)); $("#customThemeShareWrapper") .stop(true, true) .css("opacity", 0) .removeClass("hidden") .animate({ opacity: 1 }, 100, (e) => { $("#customThemeShare input").focus(); $("#customThemeShare input").select(); $("#customThemeShare input").focus(); }); } } function hideCustomThemeShare() { if (!$("#customThemeShareWrapper").hasClass("hidden")) { try { config.customThemeColors = JSON.parse( $("#customThemeShareWrapper input").val() ); } catch (e) { showNotification( "Something went wrong. Reverting to default custom colors.", 3000 ); config.customThemeColors = defaultConfig.customThemeColors; } setCustomThemeInputs(); applyCustomThemeColors(); $("#customThemeShareWrapper input").val(""); $("#customThemeShareWrapper") .stop(true, true) .css("opacity", 1) .animate( { opacity: 0, }, 100, (e) => { $("#customThemeShareWrapper").addClass("hidden"); } ); } } $("#customThemeShareWrapper").click((e) => { if ($(e.target).attr("id") === "customThemeShareWrapper") { hideCustomThemeShare(); } }); $("#customThemeShare .button").click((e) => { hideCustomThemeShare(); }); $("#shareCustomThemeButton").click((e) => { showCustomThemeShare(); }); function refreshTagsSettingsSection() { if (firebase.auth().currentUser !== null && dbSnapshot !== null) { let tagsEl = $(".pageSettings .section.tags .tagsList").empty(); dbSnapshot.tags.forEach((tag) => { if (tag.active === true) { tagsEl.append(`