From 9e26ee7858e7549f63e9b4605c70a819ea5b443d Mon Sep 17 00:00:00 2001 From: Steven Evans Date: Sun, 8 Jul 2018 00:00:57 -0400 Subject: [PATCH] [refactor] Moved 'removeChildrenFromElements' to its own TS file --- src/ActiveScriptsUI.js | 4 ++-- src/Augmentations.js | 4 ++-- src/Bladeburner.js | 2 +- src/CinematicText.js | 2 +- src/CompanyManagement.js | 4 ++-- src/Faction.js | 2 +- src/Gang.js | 4 ++-- src/RedPill.js | 2 +- src/engine.js | 2 +- utils/HelperFunctions.js | 16 -------------- utils/uiHelpers/removeChildrenFromElement.ts | 23 ++++++++++++++++++++ 11 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 utils/uiHelpers/removeChildrenFromElement.ts diff --git a/src/ActiveScriptsUI.js b/src/ActiveScriptsUI.js index 83a75b8a4..cb098e45e 100644 --- a/src/ActiveScriptsUI.js +++ b/src/ActiveScriptsUI.js @@ -5,14 +5,14 @@ import {workerScripts, import {Player} from "./Player"; import {getServer} from "./Server"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {createAccordionElement, - removeChildrenFromElement} from "../utils/HelperFunctions"; +import {createAccordionElement} 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 {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; import {removeElement} from "../utils/uiHelpers/removeElement"; /* { diff --git a/src/Augmentations.js b/src/Augmentations.js index 44a361769..71d11b133 100644 --- a/src/Augmentations.js +++ b/src/Augmentations.js @@ -12,13 +12,13 @@ import {Script, RunningScript} from "./Script"; import {Server} from "./Server"; import {SourceFiles} from "./SourceFile"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {createAccordionElement, - removeChildrenFromElement} from "../utils/HelperFunctions"; +import {createAccordionElement} from "../utils/HelperFunctions"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import {clearObject} from "../utils/helpers/clearObject"; import {createElement} from "../utils/uiHelpers/createElement"; import {isString} from "../utils/helpers/isString"; +import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; //Augmentations function Augmentation(params) { diff --git a/src/Bladeburner.js b/src/Bladeburner.js index 17c5f9cb2..9b3f14f34 100644 --- a/src/Bladeburner.js +++ b/src/Bladeburner.js @@ -11,7 +11,7 @@ import {KEY} from "./Terminal"; import {createProgressBarText} from "../utils/helpers/createProgressBarText"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {removeChildrenFromElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import numeral from "numeral/min/numeral.min"; diff --git a/src/CinematicText.js b/src/CinematicText.js index 566956d0f..9d6f433f4 100644 --- a/src/CinematicText.js +++ b/src/CinematicText.js @@ -1,6 +1,6 @@ import {Engine} from "./engine"; -import {removeChildrenFromElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; import {createElement} from "../utils/uiHelpers/createElement"; import {exceptionAlert} from "../utils/helpers/exceptionAlert"; import {isString} from "../utils/helpers/isString"; diff --git a/src/CompanyManagement.js b/src/CompanyManagement.js index 2b434c91b..ba818dacf 100644 --- a/src/CompanyManagement.js +++ b/src/CompanyManagement.js @@ -7,8 +7,7 @@ import {Player} from "./Player"; import Decimal from "decimal.js"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {removeChildrenFromElement, - clearSelector} from "../utils/HelperFunctions"; +import {clearSelector} from "../utils/HelperFunctions"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import numeral from "numeral/min/numeral.min"; @@ -17,6 +16,7 @@ import {createPopup} from "../utils/uiHelpers import {formatNumber, generateRandomString} from "../utils/StringHelperFunctions"; import {getRandomInt} from "../utils/helpers/getRandomInt"; import {isString} from "../utils/helpers/isString"; +import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; import {removeElementById} from "../utils/uiHelpers/removeElementById"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, diff --git a/src/Faction.js b/src/Faction.js index 8373a6744..3df608161 100644 --- a/src/Faction.js +++ b/src/Faction.js @@ -11,7 +11,7 @@ import {Settings} from "./Settings"; import {dialogBoxCreate} from "../utils/DialogBox"; import {factionInvitationBoxCreate} from "../utils/FactionInvitationBox"; -import {removeChildrenFromElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; import {createElement} from "../utils/uiHelpers/createElement"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; diff --git a/src/Gang.js b/src/Gang.js index dcebb42b7..5d420cd06 100644 --- a/src/Gang.js +++ b/src/Gang.js @@ -6,13 +6,13 @@ import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; -import {removeChildrenFromElement, - createAccordionElement} from "../utils/HelperFunctions"; +import {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 {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; import {removeElement} from "../utils/uiHelpers/removeElement"; import {removeElementById} from "../utils/uiHelpers/removeElementById"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, diff --git a/src/RedPill.js b/src/RedPill.js index 0a50b123b..cc5d54d03 100644 --- a/src/RedPill.js +++ b/src/RedPill.js @@ -8,7 +8,7 @@ import {Terminal} from "./Terminal"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {removeChildrenFromElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; import {yesNoBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox"; diff --git a/src/engine.js b/src/engine.js index 9ba2547df..b32b6126b 100644 --- a/src/engine.js +++ b/src/engine.js @@ -1,6 +1,6 @@ import {dialogBoxCreate} from "../utils/DialogBox"; import {gameOptionsBoxOpen, gameOptionsBoxClose}from "../utils/GameOptions"; -import {removeChildrenFromElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {createElement} from "../utils/uiHelpers/createElement"; import {exceptionAlert} from "../utils/helpers/exceptionAlert"; diff --git a/utils/HelperFunctions.js b/utils/HelperFunctions.js index 5b30996a5..b76317be7 100644 --- a/utils/HelperFunctions.js +++ b/utils/HelperFunctions.js @@ -1,18 +1,3 @@ -//General helper functions -import {isString} from "./helpers/isString"; - -function removeChildrenFromElement(el) { - if (isString(el)) { - el = document.getElementById(el); - } - if (el == null) {return;} - if (el instanceof Element) { - while(el.firstChild) { - el.removeChild(el.firstChild); - } - } -} - //Creates both the header and panel element of an accordion and sets the click handler function createAccordionElement(params) { var li = document.createElement("li"), @@ -49,5 +34,4 @@ function clearSelector(selector) { } export {createAccordionElement, - removeChildrenFromElement, clearSelector}; diff --git a/utils/uiHelpers/removeChildrenFromElement.ts b/utils/uiHelpers/removeChildrenFromElement.ts new file mode 100644 index 000000000..295e11969 --- /dev/null +++ b/utils/uiHelpers/removeChildrenFromElement.ts @@ -0,0 +1,23 @@ +import { isString } from "../helpers/isString"; +import { getElementById } from "./getElementById"; + +export function removeChildrenFromElement(el: string | null | Element) { + if (el === null) { + return; + } + + try { + const elem: HTMLElement | Element = (isString(el) ? getElementById(el as string) : el as Element); + + if (elem instanceof Element) { + while (elem.firstChild) { + elem.removeChild(elem.firstChild); + } + } + } catch (e) { + // tslint:disable-next-line:no-console + console.debug(e); + + return; + } +}