Partial rewrite of Config.js

This commit is contained in:
Daniel van Noord
2020-12-18 17:17:54 +01:00
parent bd59f7b4a9
commit 73ca07a0e9
4 changed files with 61 additions and 34 deletions

View File

@@ -2,10 +2,23 @@
* Config *
**********/
/********
* Section: Functions related to saving, loading and restoring configs */
/**
* This function saves the config of CookieMonster to localStorage
* It is called by CM.LoadConfig(), CM.RestoreDefault(),
* any of the CM.ToggleConfig() functions and upon changes to URL or volume settings
* @param {object} config The Config to be saved (normally CM.Config)
*/
CM.SaveConfig = function(config) {
localStorage.setItem(CM.ConfigPrefix, JSON.stringify(config));
}
/**
* This function loads the config of CookieMonster saved in localStorage and loads it into CM.Config
* It is called by CM.DelayInit() and CM.RestoreDefault()
*/
CM.LoadConfig = function() {
if (localStorage.getItem(CM.ConfigPrefix) != null) {
CM.Config = JSON.parse(localStorage.getItem(CM.ConfigPrefix));
@@ -61,6 +74,10 @@ CM.LoadConfig = function() {
}
}
/**
* This function reloads and resaves the default config as stored in CM.Data.ConfigDefault
* It is called by resDefBut.onclick loaded in the options page or by CM.LoadConfig is no localStorage is found
*/
CM.RestoreDefault = function() {
CM.Config = {};
CM.SaveConfig(CM.Data.ConfigDefault);
@@ -68,6 +85,15 @@ CM.RestoreDefault = function() {
Game.UpdateMenu();
}
/********
* Section: Functions related to toggling or changing configs */
/**
* This function toggles options which are considered "toggles"
* These have off (1) and on (1) states
* It is called by the onclick event of options of the "bool" type
* @param {string} config The name of the option
*/
CM.ToggleConfig = function(config) {
CM.ToggleConfigUp(config);
if (CM.ConfigData[config].toggle) {
@@ -92,22 +118,10 @@ CM.ToggleConfigUp = function(config) {
CM.SaveConfig(CM.Config);
}
CM.ToggleConfigDown = function(config) {
CM.Config[config]--;
if (CM.Config[config] < 0) {
CM.Config[config] = CM.ConfigData[config].label.length - 1;
}
if (typeof CM.ConfigData[config].func !== 'undefined') {
CM.ConfigData[config].func();
}
l(CM.ConfigPrefix + config).innerHTML = CM.ConfigData[config].label[CM.Config[config]];
CM.SaveConfig(CM.Config);
}
/**
* This function sets the value of the specified volume-option and updates the display in the options menu
* It is called by CM.Disp.CreatePrefOption()
* @param {string} config The name of the option
* @param {string} config The name of the option
*/
CM.ToggleConfigVolume = function(config) {
if (l("slider" + config) != null) {