From 4da8035f3b8185f1be0f087f3ca7dce62e90e10c Mon Sep 17 00:00:00 2001
From: DCoded <dicoded@email.com>
Date: Sun, 16 Jan 2022 17:05:41 -0500
Subject: [PATCH] Added prerequisites to options dialog

---
 devTools/types/FC/facilities.d.ts |  2 ++
 src/gui/options/optionsGroup.js   | 33 ++++++++++++++++++-------------
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/devTools/types/FC/facilities.d.ts b/devTools/types/FC/facilities.d.ts
index fc65a86be8f..b0a63a0d2e7 100644
--- a/devTools/types/FC/facilities.d.ts
+++ b/devTools/types/FC/facilities.d.ts
@@ -70,6 +70,8 @@ declare namespace FC {
 					content: string|HTMLElement|DocumentFragment;
 					/** The name of the dialog to display. */
 					name?: string;
+					/** Any prerequisites that must be met for the dialog to be displayed. */
+					prereqs?: Array<() => boolean>
 				};
 			}>
 			/** Any additional nodes to attach. */
diff --git a/src/gui/options/optionsGroup.js b/src/gui/options/optionsGroup.js
index 6274b50693e..c8ab55f4d2a 100644
--- a/src/gui/options/optionsGroup.js
+++ b/src/gui/options/optionsGroup.js
@@ -564,10 +564,11 @@ App.UI.OptionsGroup = (function() {
 		 *
 		 * @param {HTMLElement|string|DocumentFragment} content
 		 * @param {string} name
+		 * @param {Array.<() => boolean>} [prereqs]
 		 * @returns {this}
 		 */
-		addDialog(content, name) {
-			this.dialog = {content, name};
+		addDialog(content, name, prereqs = []) {
+			this.dialog = {content, name, prereqs};
 
 			return this;
 		}
@@ -583,18 +584,22 @@ App.UI.OptionsGroup = (function() {
 			}
 
 			if (this.dialog) {
-				container.onclick = () => {
-					if (Dialog.isOpen()) {
-						Dialog.close();
-					}
-					if (this.dialog.name) {
-						Dialog.setup(this.dialog.name);
-					}
-					const frag = new DocumentFragment();
-					frag.append(this.dialog.content);
-					$(Dialog.body()).empty().append(frag);
-					Dialog.open();
-				};
+				if (this.dialog.content &&
+					(!this.dialog.prereqs ||
+					this.dialog.prereqs.every(prereq => prereq() === true))) {
+					container.onclick = () => {
+						if (Dialog.isOpen()) {
+							Dialog.close();
+						}
+						if (this.dialog.name) {
+							Dialog.setup(this.dialog.name);
+						}
+						const frag = new DocumentFragment();
+						frag.append(this.dialog.content);
+						$(Dialog.body()).empty().append(frag);
+						Dialog.open();
+					};
+				}
 			}
 
 			for (const row of this.rows) {
-- 
GitLab