mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2025-03-08 03:24:48 +01:00
[refactor] Moved 'getElementById' to its own TS file.
This commit is contained in:
@ -5,12 +5,12 @@ import {iTutorialSteps, iTutorialNextStep,
|
|||||||
iTutorialIsRunning, currITutorialStep} from "./InteractiveTutorial";
|
iTutorialIsRunning, currITutorialStep} from "./InteractiveTutorial";
|
||||||
import {Player} from "./Player";
|
import {Player} from "./Player";
|
||||||
import {dialogBoxCreate} from "../utils/DialogBox";
|
import {dialogBoxCreate} from "../utils/DialogBox";
|
||||||
import {clearEventListeners,
|
import {clearEventListeners} from "../utils/HelperFunctions";
|
||||||
getElementById} from "../utils/HelperFunctions";
|
|
||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver";
|
Generic_fromJSON} from "../utils/JSONReviver";
|
||||||
import {createElement} from "../utils/uiHelpers/createElement";
|
import {createElement} from "../utils/uiHelpers/createElement";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
|
import {getElementById} from "../utils/uiHelpers/getElementById";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overwrites the inner text of the specified HTML element if it is different from what currently exists.
|
* Overwrites the inner text of the specified HTML element if it is different from what currently exists.
|
||||||
|
@ -74,21 +74,6 @@ function removeChildrenFromElement(el) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a reference to the first object with the specified value of the ID or NAME attribute, throwing an error if it is unable to find it.
|
|
||||||
* @param {string} elementId The HTML ID to retrieve the element by.
|
|
||||||
* @returns {HTMLElement} The single element.
|
|
||||||
* @throws {Error} When the 'idString' cannot be found.
|
|
||||||
*/
|
|
||||||
function getElementById(elementId) {
|
|
||||||
var el = document.getElementById(elementId);
|
|
||||||
if (el == null) {
|
|
||||||
throw new Error("Unable to find element with id '" + elementId + "'");
|
|
||||||
}
|
|
||||||
|
|
||||||
return el;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Creates both the header and panel element of an accordion and sets the click handler
|
//Creates both the header and panel element of an accordion and sets the click handler
|
||||||
function createAccordionElement(params) {
|
function createAccordionElement(params) {
|
||||||
var li = document.createElement("li"),
|
var li = document.createElement("li"),
|
||||||
@ -167,5 +152,4 @@ export {sizeOfObject,
|
|||||||
createAccordionElement,
|
createAccordionElement,
|
||||||
appendLineBreaks,
|
appendLineBreaks,
|
||||||
removeChildrenFromElement,
|
removeChildrenFromElement,
|
||||||
clearSelector,
|
clearSelector};
|
||||||
getElementById};
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { createElement } from "./createElement";
|
import { createElement } from "./createElement";
|
||||||
|
import { getElementById } from "./getElementById";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the necessary DOM elements to present an in-game popup to the player.
|
* Creates the necessary DOM elements to present an in-game popup to the player.
|
||||||
@ -20,13 +21,8 @@ export function createPopup(id: string, elems: HTMLElement[]) {
|
|||||||
content.appendChild(elem);
|
content.appendChild(elem);
|
||||||
}
|
}
|
||||||
container.appendChild(content);
|
container.appendChild(content);
|
||||||
const gameContainer: HTMLElement | null = document.getElementById("entire-game-container");
|
getElementById("entire-game-container")
|
||||||
|
.appendChild(container);
|
||||||
if (gameContainer === null) {
|
|
||||||
throw new Error("Unable to find the game container. Something is *seriously* wrong...");
|
|
||||||
}
|
|
||||||
|
|
||||||
gameContainer.appendChild(container);
|
|
||||||
|
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
14
utils/uiHelpers/getElementById.ts
Normal file
14
utils/uiHelpers/getElementById.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/**
|
||||||
|
* Returns a reference to the first object with the specified value of the ID or NAME attribute,
|
||||||
|
* throwing an error if it is unable to find it.
|
||||||
|
* @param elementId The HTML ID to retrieve the element by.
|
||||||
|
* @throws {Error} When the 'elementId' cannot be found.
|
||||||
|
*/
|
||||||
|
export function getElementById(elementId: string) {
|
||||||
|
const el: HTMLElement | null = document.getElementById(elementId);
|
||||||
|
if (el === null) {
|
||||||
|
throw new Error(`Unable to find element with id '${elementId}'`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return el;
|
||||||
|
}
|
Reference in New Issue
Block a user