From 17eaffd30f26550ee357f0cf93f2d3cc0dbb8eaa Mon Sep 17 00:00:00 2001
From: Svornost <11434-svornost@users.noreply.gitgud.io>
Date: Wed, 18 May 2022 17:29:56 -0400
Subject: [PATCH] Better yet, kill off the stupid "pretending to be data"
 dialog system and just use a wrapper for the perfectly good callback
 mechanism that already exists.

---
 devTools/types/FC/facilities.d.ts | 11 ++-----
 src/004-base/facilityFramework.js | 25 +--------------
 src/facilities/dairy/dairy.js     | 53 ++++++++-----------------------
 src/gui/options/optionsGroup.js   | 22 +++++++++++++
 4 files changed, 38 insertions(+), 73 deletions(-)

diff --git a/devTools/types/FC/facilities.d.ts b/devTools/types/FC/facilities.d.ts
index e57b89ab250..d5063841037 100644
--- a/devTools/types/FC/facilities.d.ts
+++ b/devTools/types/FC/facilities.d.ts
@@ -59,18 +59,11 @@ declare namespace FC {
 				/** The value to set `property` to when the rule is active. */
 				value: any;
 				/** Any handler to run upon setting the value. */
-				handler?: () => void;
+				handler?: (value: any) => void;
 				/** Any additional information to display with on the link. */
 				note?: string;
 				/** Any prerequisites that must be met for the option to be displayed. */
-				prereqs?: Array<() => boolean>
-				/** Any dialog to display upon setting the value. */
-				dialog?: {
-					/** The content displayed in the dialog. */
-					content: string|HTMLElement|DocumentFragment;
-					/** The name of the dialog to display. */
-					name?: string;
-				};
+				prereqs?: Array<() => boolean>;
 			}>
 			/** Any additional nodes to attach. */
 			nodes?: Array<string|HTMLElement|DocumentFragment>
diff --git a/src/004-base/facilityFramework.js b/src/004-base/facilityFramework.js
index fe284b112f2..e8351ab0f33 100644
--- a/src/004-base/facilityFramework.js
+++ b/src/004-base/facilityFramework.js
@@ -242,32 +242,9 @@ App.Facilities.Facility = class Facility {
 					rule.options.forEach(o => {
 						if (!o.prereqs || o.prereqs.every(prereq => prereq())) {
 							option.addValue(o.link, o.value);
-
-							const dialogHandler = o.dialog ? function() {
-								const dialogContent = o.dialog.content;
-								if (dialogContent) {
-									if (Dialog.isOpen()) {
-										Dialog.close();
-									}
-									if (o.dialog.name) {
-										Dialog.setup(o.dialog.name);
-									}
-									$(Dialog.body()).empty().append(dialogContent);
-									Dialog.open();
-								}
-							} : null;
-
-							if (dialogHandler && o.handler) {
-								option.addCallback(() => {
-									o.handler();
-									dialogHandler();
-								});
-							} else if (o.handler) {
+							if (o.handler) {
 								option.addCallback(o.handler);
-							} else if (dialogHandler) {
-								option.addCallback(dialogHandler);
 							}
-
 							if (o.note) {
 								option.addComment(o.note);
 							}
diff --git a/src/facilities/dairy/dairy.js b/src/facilities/dairy/dairy.js
index 2b809810a59..2af98a5ef7f 100644
--- a/src/facilities/dairy/dairy.js
+++ b/src/facilities/dairy/dairy.js
@@ -378,7 +378,6 @@ App.Facilities.Dairy.dairy = class Dairy extends App.Facilities.Facility {
 
 	/** @returns {FC.Facilities.Rule[]} */
 	get rules() {
-		const effectFunc = (arg) => this._getEffect(arg); // bind to this
 		return [
 			{
 				property: "dairyFeedersSetting",
@@ -390,25 +389,19 @@ App.Facilities.Dairy.dairy = class Dairy extends App.Facilities.Facility {
 						get text() { return `The feeders have been disabled.`; },
 						link: `Deactivate`,
 						value: 0,
-						dialog: {
-							get content() { return effectFunc('feeders'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "feeders")),
 					},
 					{
 						get text() { return `The feeders are active.`; },
 						link: `Moderate`,
 						value: 1,
-						dialog: {
-							get content() { return effectFunc('feeders'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "feeders")),
 					},
 					{
 						get text() { return `The feeders are industrial.`; },
 						link: `Industrial`,
 						value: 2,
-						dialog: {
-							get content() { return effectFunc('feeders'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "feeders")),
 						prereqs: [
 							() => V.dairyRestraintsSetting > 1,
 						],
@@ -425,25 +418,19 @@ App.Facilities.Dairy.dairy = class Dairy extends App.Facilities.Facility {
 						get text() { return `Fertile cows' wombs are not for hire.`; },
 						link: `Not for hire`,
 						value: 0,
-						dialog: {
-							get content() { return effectFunc('preg'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "preg")),
 					},
 					{
 						get text() { return `Fertile cows' wombs are for hire.`; },
 						link: `Moderate`,
 						value: 1,
-						dialog: {
-							get content() { return effectFunc('preg'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "preg")),
 					},
 					{
 						get text() { return `Fertile cows' wombs are industrially employed.`; },
 						link: `Industrial`,
 						value: 2,
-						dialog: {
-							get content() { return effectFunc('preg'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "preg")),
 						prereqs: [
 							() => V.dairyRestraintsSetting > 1,
 						],
@@ -452,9 +439,7 @@ App.Facilities.Dairy.dairy = class Dairy extends App.Facilities.Facility {
 						get text() { return `Fertile cows' wombs are worked to capacity.`; },
 						link: `Mass production`,
 						value: 3,
-						dialog: {
-							get content() { return effectFunc('preg'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "preg")),
 						prereqs: [
 							() => V.seeExtreme > 0,
 							() => V.seeHyperPreg > 0,
@@ -474,25 +459,19 @@ App.Facilities.Dairy.dairy = class Dairy extends App.Facilities.Facility {
 						get text() { return `The sodomizers are inactive.`; },
 						link: `Deactivate`,
 						value: 0,
-						dialog: {
-							get content() { return effectFunc('stimulators'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "stimulators")),
 					},
 					{
 						get text() { return `The sodomizers are active.`; },
 						link: `Moderate`,
 						value: 1,
-						dialog: {
-							get content() { return effectFunc('stimulators'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "stimulators")),
 					},
 					{
 						get text() { return `The sodomizers are industrial, employing dildos the size of horse phalli.`; },
 						link: `Industrial`,
 						value: 2,
-						dialog: {
-							get content() { return effectFunc('stimulators'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "stimulators")),
 						prereqs: [
 							() => !!V.seeExtreme,
 							() => V.dairyRestraintsSetting > 1,
@@ -510,25 +489,19 @@ App.Facilities.Dairy.dairy = class Dairy extends App.Facilities.Facility {
 						get text() { return `The cows are restrained only when necessary, allowing obedient cows freedom to range around.`; },
 						link: `Deactivate`,
 						value: 0,
-						dialog: {
-							get content() { return effectFunc('restraints'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "restraints")),
 					},
 					{
 						get text() { return `The cows are restrained when being milked, giving the machines full play.`; },
 						link: `Free range`,
 						value: 1,
-						dialog: {
-							get content() { return effectFunc('restraints'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "restraints")),
 					},
 					{
 						get text() { return `The cows are restrained permanently, allowing use of industrial techniques even devoted cows would flinch at.`; },
 						link: `Permanent machine milking`,
 						value: 2,
-						dialog: {
-							get content() { return effectFunc('restraints'); },
-						},
+						handler: App.UI.DialogHandler(this._getEffect.bind(this, "restraints")),
 					},
 				],
 			},
diff --git a/src/gui/options/optionsGroup.js b/src/gui/options/optionsGroup.js
index b7403c7b291..d51a9902e19 100644
--- a/src/gui/options/optionsGroup.js
+++ b/src/gui/options/optionsGroup.js
@@ -561,3 +561,25 @@ App.UI.OptionsGroup = (function() {
 		}
 	};
 })();
+
+/** A wrapper for option handlers that shows a dialog with the results of setting the option.
+ * @template T
+ * @param {function(T): string|HTMLElement|DocumentFragment} contentGenerator
+ * @param {string} [caption]
+ * @returns {function(T): void}
+ */
+App.UI.DialogHandler = function(contentGenerator, caption) {
+	return (arg) => {
+		const dialogContent = contentGenerator(arg);
+		if (dialogContent) {
+			if (Dialog.isOpen()) {
+				Dialog.close();
+			}
+			if (caption) {
+				Dialog.setup(caption);
+			}
+			$(Dialog.body()).empty().append(dialogContent);
+			Dialog.open();
+		}
+	};
+};
-- 
GitLab