[refactor] Moved 'removeElementById' to its own TS file

This commit is contained in:
Steven Evans 2018-07-05 13:36:02 -04:00
parent 488f947a5b
commit 875f7b4438
8 changed files with 31 additions and 17 deletions

@ -14,7 +14,7 @@ import {dialogBoxCreate} from "../utils/DialogBox";
import {addOffset, clearObject, import {addOffset, clearObject,
removeChildrenFromElement, removeChildrenFromElement,
appendLineBreaks, appendLineBreaks,
removeElementById, removeElement} 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";
@ -23,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 {removeElementById} from "../utils/uiHelpers/removeElementById";
var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"]; var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"];

@ -7,9 +7,7 @@ import {Player} from "./Player";
import Decimal from "decimal.js"; import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {removeElementById, import {removeChildrenFromElement,
createAccordionElement,
removeChildrenFromElement,
clearSelector} from "../utils/HelperFunctions"; clearSelector} from "../utils/HelperFunctions";
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver"; Generic_fromJSON} from "../utils/JSONReviver";
@ -19,6 +17,7 @@ import {createPopup} from "../utils/uiHelpers
import {formatNumber, generateRandomString} from "../utils/StringHelperFunctions"; import {formatNumber, generateRandomString} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt"; import {getRandomInt} from "../utils/helpers/getRandomInt";
import {isString} from "../utils/helpers/isString"; import {isString} from "../utils/helpers/isString";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate, import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -8,12 +8,13 @@ import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver"; Generic_fromJSON} from "../utils/JSONReviver";
import {removeChildrenFromElement, import {removeChildrenFromElement,
createAccordionElement, createAccordionElement,
removeElementById, removeElement} 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 {removeElementById} from "../utils/uiHelpers/removeElementById";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate, import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -30,7 +30,7 @@ import {initStockMarket, initSymbolToStockMap,
import {Terminal, postNetburnerText} from "./Terminal"; import {Terminal, postNetburnerText} from "./Terminal";
import Decimal from "decimal.js"; import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {removeElementById} from "../utils/HelperFunctions"; import {removeElementById} from "../utils/uiHelpers/removeElementById";
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 {exceptionAlert} from "../utils/helpers/exceptionAlert"; import {exceptionAlert} from "../utils/helpers/exceptionAlert";

@ -19,12 +19,13 @@ import {loadSpecialServerIps, SpecialServerIps} from "./SpecialServerIps";
import {loadStockMarket, StockMarket} from "./StockMarket"; import {loadStockMarket, StockMarket} from "./StockMarket";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {gameOptionsBoxClose} from "../utils/GameOptions"; import {gameOptionsBoxClose} from "../utils/GameOptions";
import {clearEventListeners, removeElementById} from "../utils/HelperFunctions"; import {clearEventListeners} 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 {createPopup} from "../utils/uiHelpers/createPopup"; import {createPopup} from "../utils/uiHelpers/createPopup";
import {formatNumber} from "../utils/StringHelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
import Decimal from "decimal.js"; import Decimal from "decimal.js";

@ -7,13 +7,13 @@ import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners, import {clearEventListeners,
removeElementById,
clearEventListenersEl} from "../utils/HelperFunctions"; clearEventListenersEl} 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";
import {formatNumber} from "../utils/StringHelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt"; import {getRandomInt} from "../utils/helpers/getRandomInt";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate, import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -48,14 +48,6 @@ function clearEventListenersEl(el) {
return newElem; 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) { function removeElement(elem) {
if (elem == null || !(elem instanceof Element)) {return;} if (elem == null || !(elem instanceof Element)) {return;}
while(elem.firstChild) {elem.removeChild(elem.firstChild);} while(elem.firstChild) {elem.removeChild(elem.firstChild);}
@ -147,7 +139,6 @@ export {sizeOfObject,
printArray, printArray,
powerOfTwo, powerOfTwo,
clearEventListenersEl, clearEventListenersEl,
removeElementById,
removeElement, removeElement,
createAccordionElement, createAccordionElement,
appendLineBreaks, appendLineBreaks,

@ -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.
}
}