mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-19 12:45:45 +01:00
[refactor] Moved 'removeElement' to its own TS file; deduped logic
This commit is contained in:
parent
3a20809943
commit
95939a8818
@ -5,7 +5,7 @@ import {workerScripts,
|
|||||||
import {Player} from "./Player";
|
import {Player} from "./Player";
|
||||||
import {getServer} from "./Server";
|
import {getServer} from "./Server";
|
||||||
import {dialogBoxCreate} from "../utils/DialogBox";
|
import {dialogBoxCreate} from "../utils/DialogBox";
|
||||||
import {createAccordionElement, removeElement,
|
import {createAccordionElement,
|
||||||
removeChildrenFromElement} from "../utils/HelperFunctions";
|
removeChildrenFromElement} from "../utils/HelperFunctions";
|
||||||
import {arrayToString} from "../utils/helpers/arrayToString";
|
import {arrayToString} from "../utils/helpers/arrayToString";
|
||||||
import {createElement} from "../utils/uiHelpers/createElement";
|
import {createElement} from "../utils/uiHelpers/createElement";
|
||||||
@ -13,6 +13,7 @@ import {exceptionAlert} from "../utils/helpers/excep
|
|||||||
import {logBoxCreate} from "../utils/LogBox";
|
import {logBoxCreate} from "../utils/LogBox";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
|
import {removeElement} from "../utils/uiHelpers/removeElement";
|
||||||
|
|
||||||
/* {
|
/* {
|
||||||
* serverName: {
|
* serverName: {
|
||||||
|
@ -11,8 +11,7 @@ import {KEY} from "./Terminal";
|
|||||||
|
|
||||||
import {createProgressBarText} from "../utils/helpers/createProgressBarText";
|
import {createProgressBarText} from "../utils/helpers/createProgressBarText";
|
||||||
import {dialogBoxCreate} from "../utils/DialogBox";
|
import {dialogBoxCreate} from "../utils/DialogBox";
|
||||||
import {removeChildrenFromElement,
|
import {removeChildrenFromElement} from "../utils/HelperFunctions";
|
||||||
removeElement} from "../utils/HelperFunctions";
|
|
||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver";
|
Generic_fromJSON} from "../utils/JSONReviver";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
@ -24,6 +23,7 @@ import {createPopup} from "../utils/uiHelpers/cre
|
|||||||
import {exceptionAlert} from "../utils/helpers/exceptionAlert";
|
import {exceptionAlert} from "../utils/helpers/exceptionAlert";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
import {getRandomInt} from "../utils/helpers/getRandomInt";
|
import {getRandomInt} from "../utils/helpers/getRandomInt";
|
||||||
|
import {removeElement} from "../utils/uiHelpers/removeElement";
|
||||||
import {removeElementById} from "../utils/uiHelpers/removeElementById";
|
import {removeElementById} from "../utils/uiHelpers/removeElementById";
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,13 +7,13 @@ import {dialogBoxCreate} from "../utils/DialogBox";
|
|||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver";
|
Generic_fromJSON} from "../utils/JSONReviver";
|
||||||
import {removeChildrenFromElement,
|
import {removeChildrenFromElement,
|
||||||
createAccordionElement,
|
createAccordionElement} from "../utils/HelperFunctions";
|
||||||
removeElement} from "../utils/HelperFunctions";
|
|
||||||
import {createElement} from "../utils/uiHelpers/createElement";
|
import {createElement} from "../utils/uiHelpers/createElement";
|
||||||
import {createPopup} from "../utils/uiHelpers/createPopup";
|
import {createPopup} from "../utils/uiHelpers/createPopup";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
import {getRandomInt} from "../utils/helpers/getRandomInt";
|
import {getRandomInt} from "../utils/helpers/getRandomInt";
|
||||||
|
import {removeElement} from "../utils/uiHelpers/removeElement";
|
||||||
import {removeElementById} from "../utils/uiHelpers/removeElementById";
|
import {removeElementById} from "../utils/uiHelpers/removeElementById";
|
||||||
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
||||||
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
||||||
|
@ -1,12 +1,6 @@
|
|||||||
//General helper functions
|
//General helper functions
|
||||||
import {isString} from "./helpers/isString";
|
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) {
|
function removeChildrenFromElement(el) {
|
||||||
if (isString(el)) {
|
if (isString(el)) {
|
||||||
el = document.getElementById(el);
|
el = document.getElementById(el);
|
||||||
@ -54,7 +48,6 @@ function clearSelector(selector) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {removeElement,
|
export {createAccordionElement,
|
||||||
createAccordionElement,
|
|
||||||
removeChildrenFromElement,
|
removeChildrenFromElement,
|
||||||
clearSelector};
|
clearSelector};
|
||||||
|
26
utils/uiHelpers/removeElement.ts
Normal file
26
utils/uiHelpers/removeElement.ts
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import { getElementById } from "./getElementById";
|
import { getElementById } from "./getElementById";
|
||||||
|
import { removeElement } from "./removeElement";
|
||||||
|
|
||||||
//
|
|
||||||
/**
|
/**
|
||||||
* Given its id, this function removes an element AND its children
|
* Given its id, this function removes an element AND its children
|
||||||
* @param id The HTML identifier to search for and remove.
|
* @param id The HTML identifier to search for and remove.
|
||||||
@ -8,13 +8,7 @@ import { getElementById } from "./getElementById";
|
|||||||
export function removeElementById(id: string) {
|
export function removeElementById(id: string) {
|
||||||
try {
|
try {
|
||||||
const elem: HTMLElement = getElementById(id);
|
const elem: HTMLElement = getElementById(id);
|
||||||
while (elem.firstChild) {
|
removeElement(elem);
|
||||||
elem.removeChild(elem.firstChild);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elem.parentNode) {
|
|
||||||
elem.parentNode.removeChild(elem);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Probably should log this as we're trying to remove elements that don't exist.
|
// Probably should log this as we're trying to remove elements that don't exist.
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user