diff --git a/src/ActiveScriptsUI.js b/src/ActiveScriptsUI.js index d41d63a26..83a75b8a4 100644 --- a/src/ActiveScriptsUI.js +++ b/src/ActiveScriptsUI.js @@ -1,18 +1,19 @@ -import {Engine} from "./engine"; +import {Engine} from "./engine"; import {workerScripts, addWorkerScript, - killWorkerScript} from "./NetscriptWorker"; -import {Player} from "./Player"; -import {getServer} from "./Server"; -import {dialogBoxCreate} from "../utils/DialogBox"; -import {createAccordionElement, removeElement, - removeChildrenFromElement} from "../utils/HelperFunctions"; -import {arrayToString} from "../utils/helpers/arrayToString"; -import {createElement} from "../utils/uiHelpers/createElement"; -import {exceptionAlert} from "../utils/helpers/exceptionAlert"; -import {logBoxCreate} from "../utils/LogBox"; -import numeral from "numeral/min/numeral.min"; -import {formatNumber} from "../utils/StringHelperFunctions"; + killWorkerScript} from "./NetscriptWorker"; +import {Player} from "./Player"; +import {getServer} from "./Server"; +import {dialogBoxCreate} from "../utils/DialogBox"; +import {createAccordionElement, + removeChildrenFromElement} from "../utils/HelperFunctions"; +import {arrayToString} from "../utils/helpers/arrayToString"; +import {createElement} from "../utils/uiHelpers/createElement"; +import {exceptionAlert} from "../utils/helpers/exceptionAlert"; +import {logBoxCreate} from "../utils/LogBox"; +import numeral from "numeral/min/numeral.min"; +import {formatNumber} from "../utils/StringHelperFunctions"; +import {removeElement} from "../utils/uiHelpers/removeElement"; /* { * serverName: { diff --git a/src/Bladeburner.js b/src/Bladeburner.js index 914bd66d7..17c5f9cb2 100644 --- a/src/Bladeburner.js +++ b/src/Bladeburner.js @@ -11,8 +11,7 @@ import {KEY} from "./Terminal"; import {createProgressBarText} from "../utils/helpers/createProgressBarText"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {removeChildrenFromElement, - removeElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/HelperFunctions"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import numeral from "numeral/min/numeral.min"; @@ -24,6 +23,7 @@ import {createPopup} from "../utils/uiHelpers/cre import {exceptionAlert} from "../utils/helpers/exceptionAlert"; import {formatNumber} from "../utils/StringHelperFunctions"; import {getRandomInt} from "../utils/helpers/getRandomInt"; +import {removeElement} from "../utils/uiHelpers/removeElement"; import {removeElementById} from "../utils/uiHelpers/removeElementById"; diff --git a/src/Gang.js b/src/Gang.js index 29f1e67de..dcebb42b7 100644 --- a/src/Gang.js +++ b/src/Gang.js @@ -7,13 +7,13 @@ import {dialogBoxCreate} from "../utils/DialogBox"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import {removeChildrenFromElement, - createAccordionElement, - removeElement} from "../utils/HelperFunctions"; + createAccordionElement} from "../utils/HelperFunctions"; import {createElement} from "../utils/uiHelpers/createElement"; import {createPopup} from "../utils/uiHelpers/createPopup"; import numeral from "numeral/min/numeral.min"; import {formatNumber} from "../utils/StringHelperFunctions"; import {getRandomInt} from "../utils/helpers/getRandomInt"; +import {removeElement} from "../utils/uiHelpers/removeElement"; import {removeElementById} from "../utils/uiHelpers/removeElementById"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, diff --git a/utils/HelperFunctions.js b/utils/HelperFunctions.js index 71a5fe47b..5b30996a5 100644 --- a/utils/HelperFunctions.js +++ b/utils/HelperFunctions.js @@ -1,12 +1,6 @@ //General helper functions import {isString} from "./helpers/isString"; -function removeElement(elem) { - if (elem == null || !(elem instanceof Element)) {return;} - while(elem.firstChild) {elem.removeChild(elem.firstChild);} - elem.parentNode.removeChild(elem); -} - function removeChildrenFromElement(el) { if (isString(el)) { el = document.getElementById(el); @@ -54,7 +48,6 @@ function clearSelector(selector) { } } -export {removeElement, - createAccordionElement, +export {createAccordionElement, removeChildrenFromElement, clearSelector}; diff --git a/utils/uiHelpers/removeElement.ts b/utils/uiHelpers/removeElement.ts new file mode 100644 index 000000000..07bbdb7a1 --- /dev/null +++ b/utils/uiHelpers/removeElement.ts @@ -0,0 +1,26 @@ +/** + * For a given element, this function removes it AND its children + * @param elem The element to remove. + */ +export function removeElement(elem: Element | null) { + if (elem === null) { + // tslint:disable-next-line:no-console + console.debug("The element passed into 'removeElement' was null."); + + return; + } + if (!(elem instanceof Element)) { + // tslint:disable-next-line:no-console + console.debug("The element passed into 'removeElement' was not an instance of an Element."); + + return; + } + + while (elem.firstChild) { + elem.removeChild(elem.firstChild); + } + + if (elem.parentNode) { + elem.parentNode.removeChild(elem); + } +} diff --git a/utils/uiHelpers/removeElementById.ts b/utils/uiHelpers/removeElementById.ts index 96718b0f6..f0fe4125b 100644 --- a/utils/uiHelpers/removeElementById.ts +++ b/utils/uiHelpers/removeElementById.ts @@ -1,6 +1,6 @@ import { getElementById } from "./getElementById"; +import { removeElement } from "./removeElement"; -// /** * Given its id, this function removes an element AND its children * @param id The HTML identifier to search for and remove. @@ -8,13 +8,7 @@ import { getElementById } from "./getElementById"; export function removeElementById(id: string) { try { const elem: HTMLElement = getElementById(id); - while (elem.firstChild) { - elem.removeChild(elem.firstChild); - } - - if (elem.parentNode) { - elem.parentNode.removeChild(elem); - } + removeElement(elem); } catch (e) { // Probably should log this as we're trying to remove elements that don't exist. }