2018-12-24 14:02:10 +01:00
|
|
|
/**
|
|
|
|
* Generic Yes-No Pop-up box
|
|
|
|
* Used to create pop-up boxes that require a yes/no response from player
|
|
|
|
*
|
|
|
|
* There are two types of pop ups:
|
|
|
|
* 1. Just a Yes/No response from player
|
|
|
|
* 2. Popup also includes a text input field in addition to the Yes/No response
|
|
|
|
*/
|
2019-04-01 11:23:25 +02:00
|
|
|
import { clearEventListeners } from "./uiHelpers/clearEventListeners";
|
|
|
|
import { KEY } from "./helpers/keyCodes";
|
2021-03-31 06:45:21 +02:00
|
|
|
import * as ReactDOM from "react-dom";
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-04-30 05:52:56 +02:00
|
|
|
export let yesNoBoxOpen = false;
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const yesNoBoxContainer: HTMLElement | null = document.getElementById(
|
|
|
|
"yes-no-box-container",
|
|
|
|
);
|
|
|
|
const yesNoBoxTextElement: HTMLElement | null =
|
|
|
|
document.getElementById("yes-no-box-text");
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
function yesNoBoxHotkeyHandler(e: KeyboardEvent): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (e.keyCode === KEY.ESC) {
|
|
|
|
yesNoBoxClose();
|
|
|
|
} else if (e.keyCode === KEY.ENTER) {
|
|
|
|
const yesBtn: HTMLElement | null =
|
|
|
|
document.getElementById("yes-no-box-yes");
|
|
|
|
if (yesBtn) {
|
|
|
|
yesBtn.click();
|
|
|
|
} else {
|
|
|
|
console.error(`Could not find YesNoBox Yes button DOM element`);
|
2018-12-25 08:55:54 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2018-12-25 08:55:54 +01:00
|
|
|
}
|
|
|
|
|
2018-12-24 14:02:10 +01:00
|
|
|
export function yesNoBoxClose(): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (yesNoBoxContainer) {
|
|
|
|
yesNoBoxContainer.style.display = "none";
|
|
|
|
} else {
|
|
|
|
console.error("Container not found for YesNoBox");
|
|
|
|
}
|
|
|
|
yesNoBoxOpen = false;
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Remove hotkey handler
|
|
|
|
document.removeEventListener("keydown", yesNoBoxHotkeyHandler);
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return false; //So that 'return yesNoBoxClose()' is return false in event listeners
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function yesNoBoxGetYesButton(): HTMLElement | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
return clearEventListeners("yes-no-box-yes");
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function yesNoBoxGetNoButton(): HTMLElement | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
return clearEventListeners("yes-no-box-no");
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function yesNoBoxCreate(txt: string | JSX.Element): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (yesNoBoxOpen) {
|
|
|
|
return false;
|
|
|
|
} //Already open
|
|
|
|
yesNoBoxOpen = true;
|
|
|
|
|
|
|
|
if (yesNoBoxTextElement) {
|
|
|
|
ReactDOM.unmountComponentAtNode(yesNoBoxTextElement);
|
|
|
|
yesNoBoxTextElement.innerHTML = "";
|
|
|
|
if (typeof txt === "string") {
|
|
|
|
yesNoBoxTextElement.innerHTML = txt as string;
|
2018-12-25 08:55:54 +01:00
|
|
|
} else {
|
2021-09-05 01:09:30 +02:00
|
|
|
ReactDOM.render(txt, yesNoBoxTextElement);
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
console.error(`Text element not found for YesNoBox`);
|
|
|
|
}
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (yesNoBoxContainer) {
|
|
|
|
yesNoBoxContainer.style.display = "flex";
|
|
|
|
} else {
|
|
|
|
console.error("Container not found for YesNoBox");
|
|
|
|
}
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Add event listener for Esc and Enter hotkeys
|
|
|
|
document.addEventListener("keydown", yesNoBoxHotkeyHandler);
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Yes-No pop up box with text input field
|
|
|
|
*/
|
2021-09-05 01:09:30 +02:00
|
|
|
const yesNoTextInputBoxContainer: HTMLElement | null = document.getElementById(
|
|
|
|
"yes-no-text-input-box-container",
|
|
|
|
);
|
|
|
|
const yesNoTextInputBoxInput: HTMLInputElement | null = document.getElementById(
|
|
|
|
"yes-no-text-input-box-input",
|
|
|
|
) as HTMLInputElement;
|
|
|
|
const yesNoTextInputBoxTextElement: HTMLElement | null =
|
|
|
|
document.getElementById("yes-no-text-input-box-text");
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function yesNoTxtInpBoxHotkeyHandler(e: KeyboardEvent): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (e.keyCode === KEY.ESC) {
|
|
|
|
yesNoTxtInpBoxClose();
|
|
|
|
} else if (e.keyCode === KEY.ENTER) {
|
|
|
|
const yesBtn: HTMLElement | null = document.getElementById(
|
|
|
|
"yes-no-text-input-box-yes",
|
|
|
|
);
|
|
|
|
if (yesBtn) {
|
|
|
|
yesBtn.click();
|
|
|
|
} else {
|
|
|
|
console.error(`Could not find YesNoTxtInputBox Yes button DOM element`);
|
2018-12-25 08:55:54 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2018-12-25 08:55:54 +01:00
|
|
|
}
|
|
|
|
|
2018-12-24 14:02:10 +01:00
|
|
|
export function yesNoTxtInpBoxClose(): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (yesNoTextInputBoxContainer != null) {
|
|
|
|
yesNoTextInputBoxContainer.style.display = "none";
|
|
|
|
} else {
|
|
|
|
console.error("Container not found for YesNoTextInputBox");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!yesNoTextInputBoxInput)
|
|
|
|
throw new Error("yesNoTextInputBoxInput was not set");
|
|
|
|
yesNoBoxOpen = false;
|
|
|
|
yesNoTextInputBoxInput.value = "";
|
2018-12-25 08:55:54 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Remove hotkey handler
|
|
|
|
document.removeEventListener("keydown", yesNoTxtInpBoxHotkeyHandler);
|
2018-12-25 08:55:54 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function yesNoTxtInpBoxGetYesButton(): HTMLElement {
|
2021-09-05 01:09:30 +02:00
|
|
|
const elem = clearEventListeners("yes-no-text-input-box-yes");
|
|
|
|
if (elem === null)
|
|
|
|
throw new Error(
|
|
|
|
"Could not find element with id: 'yes-no-text-input-box-yes'",
|
|
|
|
);
|
|
|
|
return elem;
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function yesNoTxtInpBoxGetNoButton(): HTMLElement {
|
2021-09-05 01:09:30 +02:00
|
|
|
const elem = clearEventListeners("yes-no-text-input-box-no");
|
|
|
|
if (elem === null)
|
|
|
|
throw new Error(
|
|
|
|
"Could not find element with id: 'yes-no-text-input-box-no'",
|
|
|
|
);
|
|
|
|
return elem;
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function yesNoTxtInpBoxGetInput(): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!yesNoTextInputBoxInput) {
|
|
|
|
console.error("Could not find YesNoTextInputBox input element");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
let val: string = yesNoTextInputBoxInput.value;
|
|
|
|
val = val.replace(/\s+/g, "");
|
|
|
|
return val;
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function yesNoTxtInpBoxCreate(txt: string | JSX.Element): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
yesNoBoxOpen = true;
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (yesNoTextInputBoxTextElement) {
|
|
|
|
ReactDOM.unmountComponentAtNode(yesNoTextInputBoxTextElement);
|
|
|
|
yesNoTextInputBoxTextElement.innerHTML = "";
|
|
|
|
if (typeof txt === "string") {
|
|
|
|
yesNoTextInputBoxTextElement.innerHTML = txt;
|
2018-12-24 14:02:10 +01:00
|
|
|
} else {
|
2021-09-05 01:09:30 +02:00
|
|
|
ReactDOM.render(txt, yesNoTextInputBoxTextElement);
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (yesNoTextInputBoxContainer) {
|
|
|
|
yesNoTextInputBoxContainer.style.display = "flex";
|
|
|
|
} else {
|
|
|
|
console.error("Container not found for YesNoTextInputBox");
|
|
|
|
}
|
2018-12-24 14:02:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Add event listener for Esc and Enter hotkeys
|
|
|
|
document.addEventListener("keydown", yesNoTxtInpBoxHotkeyHandler);
|
2018-12-25 08:55:54 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!yesNoTextInputBoxInput)
|
|
|
|
throw new Error("yesNoTextInputBoxInput was not set");
|
|
|
|
yesNoTextInputBoxInput.focus();
|
2018-12-24 14:02:10 +01:00
|
|
|
}
|