From ded175a570c9e77588be38deca1df2eebe082980 Mon Sep 17 00:00:00 2001
From: Arkerthan <arkerthan@mailbox.org>
Date: Wed, 22 Feb 2023 17:56:23 +0100
Subject: [PATCH] Do global callback properly for options group

---
 js/rulesAssistant/conditionEditorSimple.js |  4 ++--
 src/facilities/salon/salonPassage.js       |  6 +++---
 src/facilities/surgery/geneticQuirks.js    |  2 +-
 src/futureSocieties/fsDecoration.js        |  4 ++--
 src/gui/options/options.js                 |  2 +-
 src/gui/options/optionsGroup.js            | 19 +++++++++++++------
 6 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/js/rulesAssistant/conditionEditorSimple.js b/js/rulesAssistant/conditionEditorSimple.js
index 64a557a95a6..fd73f571066 100644
--- a/js/rulesAssistant/conditionEditorSimple.js
+++ b/js/rulesAssistant/conditionEditorSimple.js
@@ -201,7 +201,7 @@ App.RA.Activation.SimpleEditor = (function() {
 		} else if (currentRule.activeRuleType === "custom") {
 			const options = new App.UI.OptionsGroup();
 			options.addOption("Mode", "customMode", currentRule)
-				.addValueList([["Boolean", "b"], ["Number", "n"], ["String", "s"]]).addCallbackToEach(refreshEditor);
+				.addValueList([["Boolean", "b"], ["Number", "n"], ["String", "s"]]).addGlobalCallback(refreshEditor);
 			outerDiv.append(options.render());
 			const textArea = document.createElement("textarea");
 			textArea.classList.add("condition-custom");
@@ -253,7 +253,7 @@ App.RA.Activation.SimpleEditor = (function() {
 	function deserializeRule(rule) {
 		// About the TS errors in this function: we can assume a lot about the rule composition because we know it's in
 		// the simple format. The rule itself is still a normal FC.RA.PostFixRule which would allow a lot more.
-		// Therefore, TS is not happy even though we now everything's fine.
+		// Therefore, TS is not happy even though we know everything's fine.
 		/**
 		 * @type {RuleState}
 		 */
diff --git a/src/facilities/salon/salonPassage.js b/src/facilities/salon/salonPassage.js
index 0e73768577e..b9ca05d00c7 100644
--- a/src/facilities/salon/salonPassage.js
+++ b/src/facilities/salon/salonPassage.js
@@ -517,7 +517,7 @@ App.UI.salon = function(slave, cheat = false, startingGirls = false) {
 				option.addValue("Match the hair", slave.hColor);
 			}
 			option.addValueList(makeAList(App.Medicine.Modification.Color.Primary.map(color => color.value)));
-			option.addCallbackToEach(billMod);
+			option.addGlobalCallback(billMod);
 			option.pulldown();
 
 			// Style
@@ -567,7 +567,7 @@ App.UI.salon = function(slave, cheat = false, startingGirls = false) {
 				option.addValue("Match the curtains", slave.hColor);
 			}
 			option.addValueList(makeAList(App.Medicine.Modification.Color.Primary.map(color => color.value)))
-				.addCallbackToEach(billMod)
+				.addGlobalCallback(billMod)
 				.pulldown();
 		}
 		if (hasPubes || cheat) {
@@ -606,7 +606,7 @@ App.UI.salon = function(slave, cheat = false, startingGirls = false) {
 				option.addValue("Match the hair", slave.hColor);
 			}
 			option.addValueList(makeAList(App.Medicine.Modification.Color.Primary.map(color => color.value)))
-				.addCallbackToEach(billMod)
+				.addGlobalCallback(billMod)
 				.pulldown();
 		}
 		if (hasPitHair || cheat) {
diff --git a/src/facilities/surgery/geneticQuirks.js b/src/facilities/surgery/geneticQuirks.js
index 62966baa269..cbb3e25451e 100644
--- a/src/facilities/surgery/geneticQuirks.js
+++ b/src/facilities/surgery/geneticQuirks.js
@@ -35,7 +35,7 @@ App.UI.SlaveInteract.geneticQuirks = function(slave, allInactive, filter) {
 				}
 			}
 			if (key === "albinism" && "albinismOverride" in slave) {
-				option.addCallbackToEach((val) => induceAlbinism(slave, val));
+				option.addGlobalCallback((val) => induceAlbinism(slave, val));
 			}
 		}
 	}
diff --git a/src/futureSocieties/fsDecoration.js b/src/futureSocieties/fsDecoration.js
index 323cb9744eb..e1d3df46d0d 100644
--- a/src/futureSocieties/fsDecoration.js
+++ b/src/futureSocieties/fsDecoration.js
@@ -24,7 +24,7 @@ App.UI.facilityRedecoration = function() {
 			option.addValue("Distribute Evenly", "even");
 		}
 	}
-	option.addCallbackToEach(value => {
+	option.addGlobalCallback(value => {
 		console.log(value);
 		let totalCost = 0;
 		if (value === "even") { // Cycles through the list of available FS decorations, and distributes them to facilities round robin style.
@@ -56,7 +56,7 @@ App.UI.facilityRedecoration = function() {
 		options.addOption(`The decoration style of ${facility.name} is`, "decoration", facility)
 			.addValue("Standard", "standard")
 			.addValueList(decorationNames)
-			.addCallbackToEach(value => {
+			.addGlobalCallback(value => {
 				if (value !== "standard") {
 					cashX(-5000, "capEx");
 				}
diff --git a/src/gui/options/options.js b/src/gui/options/options.js
index ca41733314b..a6c6d6ffa9d 100644
--- a/src/gui/options/options.js
+++ b/src/gui/options/options.js
@@ -856,7 +856,7 @@ App.Intro.display = function(isIntro) {
 	options.addOption("Help tooltips are", "tooltipsEnabled")
 		.addValue("Enabled", 1).on().addValue("Disabled", 0).off()
 		.addComment(`This is mostly for new players. <span class='exampleTooltip noteworthy'>Colored text</span> can have tooltips.`)
-		.addCallbackToEach(App.UI.GlobalTooltips.update);
+		.addGlobalCallback(App.UI.GlobalTooltips.update);
 
 	options.addOption("Main menu slave tabs are", "useSlaveSummaryTabs")
 		.addValue("Enabled", 1).on().addValue("CardStyle", 2).on().addValue("Disabled", 0).off();
diff --git a/src/gui/options/optionsGroup.js b/src/gui/options/optionsGroup.js
index d51a9902e19..b1220f9b164 100644
--- a/src/gui/options/optionsGroup.js
+++ b/src/gui/options/optionsGroup.js
@@ -35,6 +35,11 @@ App.UI.OptionsGroup = (function() {
 			 * @type {Array<value>}
 			 */
 			this.valuePairs = [];
+			/**
+			 * @type {function(any):void}
+			 * @private
+			 */
+			this._globalCallback = undefined;
 		}
 
 		/**
@@ -157,12 +162,11 @@ App.UI.OptionsGroup = (function() {
 		}
 
 		/**
-		 * TODO: Replace with a global callback
-		 *
+		 * Only executed if no specific callback for this option exists.
 		 * @param {function(any):void} callback gets executed on every button click. Selected value is given as argument.
 		 */
-		addCallbackToEach(callback) {
-			this.valuePairs.forEach(pair => pair.callback = callback);
+		addGlobalCallback(callback) {
+			this._globalCallback = callback;
 			return this;
 		}
 
@@ -261,6 +265,8 @@ App.UI.OptionsGroup = (function() {
 							this.object[this.property] = value.value;
 							if (value.callback) {
 								value.callback(value.value);
+							} else if (this._globalCallback) {
+								this._globalCallback(value.value);
 							}
 							refresh();
 						};
@@ -280,6 +286,8 @@ App.UI.OptionsGroup = (function() {
 					const originalObj = this.valuePairs.find(obj => obj.value === value);
 					if (originalObj && typeof originalObj.callback === "function") {
 						originalObj.callback(originalObj.value);
+					} else if (this._globalCallback) {
+						this._globalCallback(originalObj.value);
 					}
 					refresh();
 				}));
@@ -529,8 +537,7 @@ App.UI.OptionsGroup = (function() {
 		 * @returns {Comment}
 		 */
 		addComment(comment) {
-			const c = new Comment(comment);
-			return this._addRow(c);
+			return this._addRow(new Comment(comment));
 		}
 
 		/**
-- 
GitLab