mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-27 03:25:44 +02:00
prettify, sorry for the big ass commit
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
* @param id The (hopefully) unique identifier for the popup container
|
||||
* @param rootComponent Root React Component for the content (NOT the popup containers themselves)
|
||||
*/
|
||||
import * as React from "react";
|
||||
import * as ReactDOM from "react-dom";
|
||||
import * as React from "react";
|
||||
import * as ReactDOM from "react-dom";
|
||||
|
||||
import { Popup } from "./Popup";
|
||||
|
||||
@@ -16,18 +16,18 @@ import { removeElementById } from "../../../utils/uiHelpers/removeElementById";
|
||||
|
||||
let gameContainer: HTMLElement;
|
||||
|
||||
(function() {
|
||||
function getGameContainer(): void {
|
||||
const container = document.getElementById("entire-game-container");
|
||||
if (container == null) {
|
||||
throw new Error(`Failed to find game container DOM element`)
|
||||
}
|
||||
|
||||
gameContainer = container;
|
||||
document.removeEventListener("DOMContentLoaded", getGameContainer);
|
||||
(function () {
|
||||
function getGameContainer(): void {
|
||||
const container = document.getElementById("entire-game-container");
|
||||
if (container == null) {
|
||||
throw new Error(`Failed to find game container DOM element`);
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", getGameContainer);
|
||||
gameContainer = container;
|
||||
document.removeEventListener("DOMContentLoaded", getGameContainer);
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", getGameContainer);
|
||||
})();
|
||||
|
||||
// This variable is used to avoid setting the semi-transparent background
|
||||
@@ -35,45 +35,57 @@ let gameContainer: HTMLElement;
|
||||
let deepestPopupId = "";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export function createPopup<T>(id: string, rootComponent: (props: T) => React.ReactElement, props: T): HTMLElement | null {
|
||||
|
||||
let container = document.getElementById(id);
|
||||
if (container == null) {
|
||||
function onClick(this: HTMLElement, event: MouseEvent): any {
|
||||
if(!event.srcElement) return;
|
||||
if(!(event.srcElement instanceof HTMLElement)) return;
|
||||
const clickedId = (event.srcElement as HTMLElement).id;
|
||||
if(clickedId !== id) return;
|
||||
removePopup(id);
|
||||
}
|
||||
const backgroundColor = deepestPopupId === "" ? 'rgba(0,0,0,0.5)' : 'rgba(0,0,0,0)';
|
||||
container = createElement("div", {
|
||||
class: "popup-box-container",
|
||||
display: "flex",
|
||||
id: id,
|
||||
backgroundColor: backgroundColor,
|
||||
clickListener: onClick,
|
||||
});
|
||||
|
||||
gameContainer.appendChild(container);
|
||||
export function createPopup<T>(
|
||||
id: string,
|
||||
rootComponent: (props: T) => React.ReactElement,
|
||||
props: T,
|
||||
): HTMLElement | null {
|
||||
let container = document.getElementById(id);
|
||||
if (container == null) {
|
||||
function onClick(this: HTMLElement, event: MouseEvent): any {
|
||||
if (!event.srcElement) return;
|
||||
if (!(event.srcElement instanceof HTMLElement)) return;
|
||||
const clickedId = (event.srcElement as HTMLElement).id;
|
||||
if (clickedId !== id) return;
|
||||
removePopup(id);
|
||||
}
|
||||
const backgroundColor =
|
||||
deepestPopupId === "" ? "rgba(0,0,0,0.5)" : "rgba(0,0,0,0)";
|
||||
container = createElement("div", {
|
||||
class: "popup-box-container",
|
||||
display: "flex",
|
||||
id: id,
|
||||
backgroundColor: backgroundColor,
|
||||
clickListener: onClick,
|
||||
});
|
||||
|
||||
if(deepestPopupId === "") deepestPopupId = id;
|
||||
ReactDOM.render(<Popup content={rootComponent} id={id} props={props} removePopup={removePopup} />, container);
|
||||
gameContainer.appendChild(container);
|
||||
}
|
||||
|
||||
return container;
|
||||
if (deepestPopupId === "") deepestPopupId = id;
|
||||
ReactDOM.render(
|
||||
<Popup
|
||||
content={rootComponent}
|
||||
id={id}
|
||||
props={props}
|
||||
removePopup={removePopup}
|
||||
/>,
|
||||
container,
|
||||
);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes a popup created with the createPopup() function above
|
||||
*/
|
||||
export function removePopup(id: string): void {
|
||||
const content = document.getElementById(`${id}`);
|
||||
if (content == null) return;
|
||||
const content = document.getElementById(`${id}`);
|
||||
if (content == null) return;
|
||||
|
||||
ReactDOM.unmountComponentAtNode(content);
|
||||
ReactDOM.unmountComponentAtNode(content);
|
||||
|
||||
removeElementById(id);
|
||||
removeElementById(`${id}-close`);
|
||||
if(id === deepestPopupId) deepestPopupId = "";
|
||||
removeElementById(id);
|
||||
removeElementById(`${id}-close`);
|
||||
if (id === deepestPopupId) deepestPopupId = "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user