Fully annotated Config.js

This commit is contained in:
Daniel van Noord
2020-12-18 18:10:39 +01:00
parent 0ae29ce2e8
commit 8aa8a7c878
5 changed files with 146 additions and 168 deletions

View File

@@ -7,19 +7,19 @@
/**
* 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
* It is called by CM.Config.LoadConfig(), CM.Config.RestoreDefault(), CM.Config.ToggleConfig(),
* CM.ToggleConfigVolume() and changes in options with type "url" or "color"
* @param {object} config The Config to be saved (normally CM.Options)
*/
CM.SaveConfig = function(config) {
CM.Config.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.Options
* It is called by CM.DelayInit() and CM.RestoreDefault()
* It is called by CM.DelayInit() and CM.Config.RestoreDefault()
*/
CM.LoadConfig = function() {
CM.Config.LoadConfig = function() {
if (localStorage.getItem(CM.ConfigPrefix) != null) {
CM.Options = JSON.parse(localStorage.getItem(CM.ConfigPrefix));
@@ -30,7 +30,7 @@ CM.LoadConfig = function() {
mod = true;
CM.Options[i] = CM.Data.ConfigDefault[i];
}
else if (i != 'StatsPref' && i != 'Colors') {
else if (i != 'Header' && i != 'Colors') {
if (i.indexOf('SoundURL') == -1) {
if (!(CM.Options[i] > -1 && CM.Options[i] < CM.ConfigData[i].label.length)) {
mod = true;
@@ -44,8 +44,8 @@ CM.LoadConfig = function() {
}
}
}
else if (i == 'StatsPref') {
for (var j in CM.Data.ConfigDefault.StatsPref) {
else if (i == 'Header') {
for (var j in CM.Data.ConfigDefault.Header) {
if (typeof CM.Options[i][j] === 'undefined' || !(CM.Options[i][j] > -1 && CM.Options[i][j] < 2)) {
mod = true;
CM.Options[i][j] = CM.Data.ConfigDefault[i][j];
@@ -61,27 +61,27 @@ CM.LoadConfig = function() {
}
}
}
if (mod) CM.SaveConfig(CM.Options);
if (mod) CM.Config.SaveConfig(CM.Options);
CM.Loop(); // Do loop once
for (var i in CM.Data.ConfigDefault) {
if (i != 'StatsPref' && i != 'OptionsPref' && typeof CM.ConfigData[i].func !== 'undefined') {
if (i != 'Header' && typeof CM.ConfigData[i].func !== 'undefined') {
CM.ConfigData[i].func();
}
}
}
else { // Default values
CM.RestoreDefault();
CM.Config.RestoreDefault();
}
}
/**
* 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
* It is called by resDefBut.onclick loaded in the options page or by CM.Config.LoadConfig if no localStorage is found
*/
CM.RestoreDefault = function() {
CM.Config.RestoreDefault = function() {
CM.Options = {};
CM.SaveConfig(CM.Data.ConfigDefault);
CM.LoadConfig();
CM.Config.SaveConfig(CM.Data.ConfigDefault);
CM.Config.LoadConfig();
Game.UpdateMenu();
}
@@ -89,71 +89,61 @@ CM.RestoreDefault = function() {
* Section: Functions related to toggling or changing configs */
/**
* This function toggles options which are considered "toggles"
* These have off (1) and on (1) states
* This function toggles options by incrementing them with 1 and handling changes
* 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) {
if (CM.Options[config] == 0) {
l(CM.ConfigPrefix + config).className = 'option off';
}
else {
l(CM.ConfigPrefix + config).className = 'option';
}
}
}
CM.ToggleConfigUp = function(config) {
CM.Config.ToggleConfig = function(config) {
CM.Options[config]++;
if (CM.Options[config] == CM.ConfigData[config].label.length) {
CM.Options[config] = 0;
if (CM.ConfigData[config].toggle) l(CM.ConfigPrefix + config).className = 'option off';
}
else l(CM.ConfigPrefix + config).className = 'option';
if (typeof CM.ConfigData[config].func !== 'undefined') {
CM.ConfigData[config].func();
}
l(CM.ConfigPrefix + config).innerHTML = CM.ConfigData[config].label[CM.Options[config]];
CM.SaveConfig(CM.Options);
CM.Config.SaveConfig(CM.Options);
}
/**
* 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()
* It is called by the oninput and onchange event of "vol" type options
* @param {string} config The name of the option
*/
CM.ToggleConfigVolume = function(config) {
CM.Config.ToggleConfigVolume = function(config) {
if (l("slider" + config) != null) {
l("slider" + config + "right").innerHTML = l("slider" + config).value + "%";
CM.Options[config] = Math.round(l("slider" + config).value);
}
CM.SaveConfig(CM.Options);
CM.Config.SaveConfig(CM.Options);
}
CM.ToggleStatsConfig = function(config) {
if (CM.Options.StatsPref[config] == 0) {
CM.Options.StatsPref[config]++;
}
else {
CM.Options.StatsPref[config]--;
}
CM.SaveConfig(CM.Options);
/**
* This function toggles header options by incrementing them with 1 and handling changes
* It is called by the onclick event of the +/- next to headers
* @param {string} config The name of the header
*/
CM.Config.ToggleHeader = function(config) {
CM.Options.Header[config]++;
if (CM.Options.Header[config] > 1) CM.Options.Header[config] = 0;
CM.Config.SaveConfig(CM.Options);
}
CM.ToggleOptionsConfig = function(config) {
if (CM.Options.OptionsPref[config] == 0) {
CM.Options.OptionsPref[config]++;
}
else {
CM.Options.OptionsPref[config]--;
}
CM.SaveConfig(CM.Options);
}
/********
* Section: Functions related to notifications */
// Checks if the browsers has permissions to produce notifications
// Should be triggered when Config related to Notifications is toggled on
CM.CheckNotificationPermissions = function(ToggleOnOff) {
/**
* This function checks if the user has given permissions for notifications
* It is called by a change in any of the notification options
* Note that most browsers will stop asking if the user has ignored the prompt around 6 times
* @param {number} ToggleOnOff A number indicating whether the option has been turned off (0) or on (1)
*/
CM.Config.CheckNotificationPermissions = function(ToggleOnOff) {
if (ToggleOnOff == 1) {
// Check if browser support Promise version of Notification Permissions
function checkNotificationPromise() {