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

This commit is contained in:
Steven Evans 2018-07-07 23:19:19 -04:00
parent 7edf5b5f1a
commit 8016321bfb
17 changed files with 41 additions and 31 deletions

@ -11,8 +11,7 @@ import {Settings} from "./Settings";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {factionInvitationBoxCreate} from "../utils/FactionInvitationBox"; import {factionInvitationBoxCreate} from "../utils/FactionInvitationBox";
import {clearEventListeners, import {removeChildrenFromElement} from "../utils/HelperFunctions";
removeChildrenFromElement} from "../utils/HelperFunctions";
import {createElement} from "../utils/uiHelpers/createElement"; import {createElement} from "../utils/uiHelpers/createElement";
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver"; Generic_fromJSON} from "../utils/JSONReviver";

@ -5,7 +5,7 @@ import {iTutorialSteps, iTutorialNextStep,
iTutorialIsRunning, currITutorialStep} from "./InteractiveTutorial"; iTutorialIsRunning, currITutorialStep} from "./InteractiveTutorial";
import {Player} from "./Player"; import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners} from "../utils/HelperFunctions"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
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";

@ -3,7 +3,7 @@ import {CONSTANTS} from "./Constants";
import {Engine} from "./engine"; import {Engine} from "./engine";
import {Player} from "./Player"; import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners} from "../utils/HelperFunctions"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {getRandomInt} from "../utils/helpers/getRandomInt"; import {getRandomInt} from "../utils/helpers/getRandomInt";
import {infiltrationBoxCreate} from "../utils/InfiltrationBox"; import {infiltrationBoxCreate} from "../utils/InfiltrationBox";
import {formatNumber} from "../utils/StringHelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions";

@ -1,7 +1,7 @@
import {Engine} from "./engine"; import {Engine} from "./engine";
import {Player} from "./Player"; import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners} from "../utils/HelperFunctions"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
/* InteractiveTutorial.js */ /* InteractiveTutorial.js */
let iTutorialSteps = { let iTutorialSteps = {

@ -15,7 +15,7 @@ import {Settings} from "./Settings";
import {SpecialServerNames, SpecialServerIps} from "./SpecialServerIps"; import {SpecialServerNames, SpecialServerIps} from "./SpecialServerIps";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners} from "../utils/HelperFunctions"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {createRandomIp} from "../utils/IPAddress"; import {createRandomIp} from "../utils/IPAddress";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions";

@ -3,7 +3,7 @@ import {Engine} from "./engine";
import {displayFactionContent} from "./Faction"; import {displayFactionContent} from "./Faction";
import {Player} from "./Player"; import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners} from "../utils/HelperFunctions"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {addOffset} from "../utils/helpers/addOffset"; import {addOffset} from "../utils/helpers/addOffset";
import {formatNumber} from "../utils/StringHelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt"; import {getRandomInt} from "../utils/helpers/getRandomInt";

@ -21,7 +21,7 @@ import {SourceFiles, applySourceFile} from "./SourceFile";
import Decimal from "decimal.js"; import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners} from "../utils/HelperFunctions"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {createRandomIp} from "../utils/IPAddress"; import {createRandomIp} from "../utils/IPAddress";
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver"; Generic_fromJSON} from "../utils/JSONReviver";

@ -6,9 +6,9 @@ import {SourceFiles, SourceFile,
PlayerOwnedSourceFile} from "./SourceFile"; PlayerOwnedSourceFile} from "./SourceFile";
import {Terminal} from "./Terminal"; import {Terminal} from "./Terminal";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners, import {removeChildrenFromElement} from "../utils/HelperFunctions";
removeChildrenFromElement} from "../utils/HelperFunctions";
import {yesNoBoxCreate, yesNoBoxGetYesButton, import {yesNoBoxCreate, yesNoBoxGetYesButton,
yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox"; yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox";

@ -19,7 +19,7 @@ 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} from "../utils/HelperFunctions"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
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";

@ -6,8 +6,7 @@ import {WorkerScript} from "./NetscriptWorker";
import {Player} from "./Player"; import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners, import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
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";

@ -1,7 +1,7 @@
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {gameOptionsBoxOpen, gameOptionsBoxClose}from "../utils/GameOptions"; import {gameOptionsBoxOpen, gameOptionsBoxClose}from "../utils/GameOptions";
import {clearEventListeners, import {removeChildrenFromElement} from "../utils/HelperFunctions";
removeChildrenFromElement} from "../utils/HelperFunctions"; import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {createElement} from "../utils/uiHelpers/createElement"; import {createElement} from "../utils/uiHelpers/createElement";
import {exceptionAlert} from "../utils/helpers/exceptionAlert"; import {exceptionAlert} from "../utils/helpers/exceptionAlert";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";

@ -1,7 +1,7 @@
import {Faction, joinFaction} from "../src/Faction"; import {Faction, joinFaction} from "../src/Faction";
import {Engine} from "../src/engine"; import {Engine} from "../src/engine";
import {Player} from "../src/Player"; import {Player} from "../src/Player";
import {clearEventListeners} from "./HelperFunctions"; import {clearEventListeners} from "./uiHelpers/clearEventListeners";
/* Faction Invitation Pop-up box */ /* Faction Invitation Pop-up box */
function factionInvitationBoxClose() { function factionInvitationBoxClose() {

@ -1,16 +1,6 @@
//General helper functions //General helper functions
import {isString} from "./helpers/isString"; import {isString} from "./helpers/isString";
//Given an element by its Id(usually an 'a' element), removes all event listeners
//from that element by cloning and replacing. Then returns the new cloned element
function clearEventListeners(elemId) {
var elem = document.getElementById(elemId);
if (elem == null) {console.log("ERR: Could not find element for: " + elemId); return null;}
var newElem = elem.cloneNode(true);
elem.parentNode.replaceChild(newElem, elem);
return newElem;
}
//Same as clearEventListeners except it takes a DOM element object rather than an ID //Same as clearEventListeners except it takes a DOM element object rather than an ID
function clearEventListenersEl(el) { function clearEventListenersEl(el) {
if (el == null) {console.log("ERR: element passed into clearEventListenersEl is null"); return null;} if (el == null) {console.log("ERR: element passed into clearEventListenersEl is null"); return null;}
@ -72,8 +62,7 @@ function clearSelector(selector) {
} }
} }
export {clearEventListeners, export {clearEventListenersEl,
clearEventListenersEl,
removeElement, removeElement,
createAccordionElement, createAccordionElement,
removeChildrenFromElement, removeChildrenFromElement,

@ -3,7 +3,7 @@ import {CONSTANTS} from "../src/Constants";
import {Factions, Faction} from "../src/Faction"; import {Factions, Faction} from "../src/Faction";
import {Player} from "../src/Player"; import {Player} from "../src/Player";
import {dialogBoxCreate} from "./DialogBox"; import {dialogBoxCreate} from "./DialogBox";
import {clearEventListeners} from "./HelperFunctions"; import {clearEventListeners} from "./uiHelpers/clearEventListeners";
import {formatNumber} from "./StringHelperFunctions"; import {formatNumber} from "./StringHelperFunctions";
//Keep track of last faction //Keep track of last faction

@ -1,5 +1,5 @@
import {killWorkerScript} from "../src/NetscriptWorker"; import {killWorkerScript} from "../src/NetscriptWorker";
import {clearEventListeners} from "./HelperFunctions"; import {clearEventListeners} from "./uiHelpers/clearEventListeners";
import {arrayToString} from "./helpers/arrayToString"; import {arrayToString} from "./helpers/arrayToString";
$(document).keydown(function(event) { $(document).keydown(function(event) {

@ -1,4 +1,4 @@
import {clearEventListeners} from "./HelperFunctions"; import {clearEventListeners} from "./uiHelpers/clearEventListeners";
/* Generic Yes-No Pop-up box /* Generic Yes-No Pop-up box
* Can be used to create pop-up boxes that require a yes/no response from player * Can be used to create pop-up boxes that require a yes/no response from player
*/ */

@ -0,0 +1,23 @@
import { getElementById } from "./getElementById";
/**
* Given an element by its ID, removes all event listeners from that element by cloning and
* replacing. Then returns the new cloned element.
* @param elemId The HTML ID to retrieve the element by.
*/
export function clearEventListeners(elemId: string) {
try {
const elem: HTMLElement = getElementById(elemId);
const newElem: HTMLElement = elem.cloneNode(true) as HTMLElement;
if (elem.parentNode !== null) {
elem.parentNode.replaceChild(newElem, elem);
}
return newElem;
} catch (e) {
// tslint:disable-next-line:no-console
console.error(e);
return null;
}
}