From 875f7b44380c9eb2008256a8370a32c8c29f3c0f Mon Sep 17 00:00:00 2001 From: Steven Evans Date: Thu, 5 Jul 2018 13:36:02 -0400 Subject: [PATCH] [refactor] Moved 'removeElementById' to its own TS file --- src/Bladeburner.js | 3 ++- src/CompanyManagement.js | 5 ++--- src/Gang.js | 3 ++- src/Prestige.js | 2 +- src/SaveObject.js | 3 ++- src/StockMarket.js | 2 +- utils/HelperFunctions.js | 9 --------- utils/uiHelpers/removeElementById.ts | 21 +++++++++++++++++++++ 8 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 utils/uiHelpers/removeElementById.ts diff --git a/src/Bladeburner.js b/src/Bladeburner.js index 5f2c07533..8abc87871 100644 --- a/src/Bladeburner.js +++ b/src/Bladeburner.js @@ -14,7 +14,7 @@ import {dialogBoxCreate} from "../utils/DialogBox"; import {addOffset, clearObject, removeChildrenFromElement, appendLineBreaks, - removeElementById, removeElement} from "../utils/HelperFunctions"; + removeElement} from "../utils/HelperFunctions"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import numeral from "numeral/min/numeral.min"; @@ -23,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 {removeElementById} from "../utils/uiHelpers/removeElementById"; var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"]; diff --git a/src/CompanyManagement.js b/src/CompanyManagement.js index 7be89ebba..2b434c91b 100644 --- a/src/CompanyManagement.js +++ b/src/CompanyManagement.js @@ -7,9 +7,7 @@ import {Player} from "./Player"; import Decimal from "decimal.js"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {removeElementById, - createAccordionElement, - removeChildrenFromElement, +import {removeChildrenFromElement, clearSelector} from "../utils/HelperFunctions"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; @@ -19,6 +17,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 {removeElementById} from "../utils/uiHelpers/removeElementById"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/src/Gang.js b/src/Gang.js index a61441b50..29f1e67de 100644 --- a/src/Gang.js +++ b/src/Gang.js @@ -8,12 +8,13 @@ import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import {removeChildrenFromElement, createAccordionElement, - removeElementById, removeElement} from "../utils/HelperFunctions"; + removeElement} 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 {removeElementById} from "../utils/uiHelpers/removeElementById"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/src/Prestige.js b/src/Prestige.js index c4952a4e1..83b92b23c 100644 --- a/src/Prestige.js +++ b/src/Prestige.js @@ -30,7 +30,7 @@ import {initStockMarket, initSymbolToStockMap, import {Terminal, postNetburnerText} from "./Terminal"; import Decimal from "decimal.js"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {removeElementById} from "../utils/HelperFunctions"; +import {removeElementById} from "../utils/uiHelpers/removeElementById"; import {createElement} from "../utils/uiHelpers/createElement"; import {createPopup} from "../utils/uiHelpers/createPopup"; import {exceptionAlert} from "../utils/helpers/exceptionAlert"; diff --git a/src/SaveObject.js b/src/SaveObject.js index e77f30d2e..d37874c6a 100644 --- a/src/SaveObject.js +++ b/src/SaveObject.js @@ -19,12 +19,13 @@ import {loadSpecialServerIps, SpecialServerIps} from "./SpecialServerIps"; import {loadStockMarket, StockMarket} from "./StockMarket"; import {dialogBoxCreate} from "../utils/DialogBox"; import {gameOptionsBoxClose} from "../utils/GameOptions"; -import {clearEventListeners, removeElementById} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/HelperFunctions"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import {createElement} from "../utils/uiHelpers/createElement"; import {createPopup} from "../utils/uiHelpers/createPopup"; import {formatNumber} from "../utils/StringHelperFunctions"; +import {removeElementById} from "../utils/uiHelpers/removeElementById"; import Decimal from "decimal.js"; diff --git a/src/StockMarket.js b/src/StockMarket.js index affc5e294..aebde7427 100644 --- a/src/StockMarket.js +++ b/src/StockMarket.js @@ -7,13 +7,13 @@ import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; import {clearEventListeners, - removeElementById, clearEventListenersEl} from "../utils/HelperFunctions"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import numeral from "numeral/min/numeral.min"; import {formatNumber} from "../utils/StringHelperFunctions"; import {getRandomInt} from "../utils/helpers/getRandomInt"; +import {removeElementById} from "../utils/uiHelpers/removeElementById"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/utils/HelperFunctions.js b/utils/HelperFunctions.js index a83d94249..e6f2a76c2 100644 --- a/utils/HelperFunctions.js +++ b/utils/HelperFunctions.js @@ -48,14 +48,6 @@ function clearEventListenersEl(el) { return newElem; } -//Given its id, this function removes an element AND its children -function removeElementById(id) { - var elem = document.getElementById(id); - if (elem == null) {return;} - while(elem.firstChild) {elem.removeChild(elem.firstChild);} - elem.parentNode.removeChild(elem); -} - function removeElement(elem) { if (elem == null || !(elem instanceof Element)) {return;} while(elem.firstChild) {elem.removeChild(elem.firstChild);} @@ -147,7 +139,6 @@ export {sizeOfObject, printArray, powerOfTwo, clearEventListenersEl, - removeElementById, removeElement, createAccordionElement, appendLineBreaks, diff --git a/utils/uiHelpers/removeElementById.ts b/utils/uiHelpers/removeElementById.ts new file mode 100644 index 000000000..96718b0f6 --- /dev/null +++ b/utils/uiHelpers/removeElementById.ts @@ -0,0 +1,21 @@ +import { getElementById } from "./getElementById"; + +// +/** + * Given its id, this function removes an element AND its children + * @param id The HTML identifier to search for and remove. + */ +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); + } + } catch (e) { + // Probably should log this as we're trying to remove elements that don't exist. + } +}