From 8016321bfb2d3ee644a7daeac5ff4b11ccb4383b Mon Sep 17 00:00:00 2001 From: Steven Evans Date: Sat, 7 Jul 2018 23:19:19 -0400 Subject: [PATCH] [refactor] Moved 'clearEventListeners' to its own TS file --- src/Faction.js | 3 +-- src/HacknetNode.js | 2 +- src/Infiltration.js | 2 +- src/InteractiveTutorial.js | 2 +- src/Location.js | 2 +- src/Missions.js | 2 +- src/Player.js | 2 +- src/RedPill.js | 4 ++-- src/SaveObject.js | 2 +- src/StockMarket.js | 3 +-- src/engine.js | 4 ++-- utils/FactionInvitationBox.js | 2 +- utils/HelperFunctions.js | 13 +------------ utils/InfiltrationBox.js | 2 +- utils/LogBox.js | 2 +- utils/YesNoBox.js | 2 +- utils/uiHelpers/clearEventListeners.ts | 23 +++++++++++++++++++++++ 17 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 utils/uiHelpers/clearEventListeners.ts diff --git a/src/Faction.js b/src/Faction.js index 22ec3c75d..8373a6744 100644 --- a/src/Faction.js +++ b/src/Faction.js @@ -11,8 +11,7 @@ import {Settings} from "./Settings"; import {dialogBoxCreate} from "../utils/DialogBox"; import {factionInvitationBoxCreate} from "../utils/FactionInvitationBox"; -import {clearEventListeners, - removeChildrenFromElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/HelperFunctions"; import {createElement} from "../utils/uiHelpers/createElement"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; diff --git a/src/HacknetNode.js b/src/HacknetNode.js index 3b5651888..9c560cd9c 100644 --- a/src/HacknetNode.js +++ b/src/HacknetNode.js @@ -5,7 +5,7 @@ import {iTutorialSteps, iTutorialNextStep, iTutorialIsRunning, currITutorialStep} from "./InteractiveTutorial"; import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import {createElement} from "../utils/uiHelpers/createElement"; diff --git a/src/Infiltration.js b/src/Infiltration.js index 0f3c392cf..4fe0558da 100644 --- a/src/Infiltration.js +++ b/src/Infiltration.js @@ -3,7 +3,7 @@ import {CONSTANTS} from "./Constants"; import {Engine} from "./engine"; import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {getRandomInt} from "../utils/helpers/getRandomInt"; import {infiltrationBoxCreate} from "../utils/InfiltrationBox"; import {formatNumber} from "../utils/StringHelperFunctions"; diff --git a/src/InteractiveTutorial.js b/src/InteractiveTutorial.js index 9fa91b782..953b52cb5 100644 --- a/src/InteractiveTutorial.js +++ b/src/InteractiveTutorial.js @@ -1,7 +1,7 @@ import {Engine} from "./engine"; import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; /* InteractiveTutorial.js */ let iTutorialSteps = { diff --git a/src/Location.js b/src/Location.js index 4630b85b2..8c92fe78d 100644 --- a/src/Location.js +++ b/src/Location.js @@ -15,7 +15,7 @@ import {Settings} from "./Settings"; import {SpecialServerNames, SpecialServerIps} from "./SpecialServerIps"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {createRandomIp} from "../utils/IPAddress"; import numeral from "numeral/min/numeral.min"; import {formatNumber} from "../utils/StringHelperFunctions"; diff --git a/src/Missions.js b/src/Missions.js index 9f86d0d5a..06843f200 100644 --- a/src/Missions.js +++ b/src/Missions.js @@ -3,7 +3,7 @@ import {Engine} from "./engine"; import {displayFactionContent} from "./Faction"; import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {addOffset} from "../utils/helpers/addOffset"; import {formatNumber} from "../utils/StringHelperFunctions"; import {getRandomInt} from "../utils/helpers/getRandomInt"; diff --git a/src/Player.js b/src/Player.js index 7e83980f9..f8cbf97d8 100644 --- a/src/Player.js +++ b/src/Player.js @@ -21,7 +21,7 @@ import {SourceFiles, applySourceFile} from "./SourceFile"; import Decimal from "decimal.js"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {createRandomIp} from "../utils/IPAddress"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; diff --git a/src/RedPill.js b/src/RedPill.js index bcc3236ea..0a50b123b 100644 --- a/src/RedPill.js +++ b/src/RedPill.js @@ -6,9 +6,9 @@ import {SourceFiles, SourceFile, PlayerOwnedSourceFile} from "./SourceFile"; import {Terminal} from "./Terminal"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners, - removeChildrenFromElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/HelperFunctions"; import {yesNoBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox"; diff --git a/src/SaveObject.js b/src/SaveObject.js index d37874c6a..43593dc46 100644 --- a/src/SaveObject.js +++ b/src/SaveObject.js @@ -19,7 +19,7 @@ import {loadSpecialServerIps, SpecialServerIps} from "./SpecialServerIps"; import {loadStockMarket, StockMarket} from "./StockMarket"; import {dialogBoxCreate} from "../utils/DialogBox"; import {gameOptionsBoxClose} from "../utils/GameOptions"; -import {clearEventListeners} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import {createElement} from "../utils/uiHelpers/createElement"; diff --git a/src/StockMarket.js b/src/StockMarket.js index aebde7427..dcac2bd18 100644 --- a/src/StockMarket.js +++ b/src/StockMarket.js @@ -6,8 +6,7 @@ import {WorkerScript} from "./NetscriptWorker"; import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners, - clearEventListenersEl} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import numeral from "numeral/min/numeral.min"; diff --git a/src/engine.js b/src/engine.js index f00e536d3..9ba2547df 100644 --- a/src/engine.js +++ b/src/engine.js @@ -1,7 +1,7 @@ import {dialogBoxCreate} from "../utils/DialogBox"; import {gameOptionsBoxOpen, gameOptionsBoxClose}from "../utils/GameOptions"; -import {clearEventListeners, - removeChildrenFromElement} from "../utils/HelperFunctions"; +import {removeChildrenFromElement} from "../utils/HelperFunctions"; +import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners"; import {createElement} from "../utils/uiHelpers/createElement"; import {exceptionAlert} from "../utils/helpers/exceptionAlert"; import numeral from "numeral/min/numeral.min"; diff --git a/utils/FactionInvitationBox.js b/utils/FactionInvitationBox.js index bebd567f8..474792b06 100644 --- a/utils/FactionInvitationBox.js +++ b/utils/FactionInvitationBox.js @@ -1,7 +1,7 @@ import {Faction, joinFaction} from "../src/Faction"; import {Engine} from "../src/engine"; import {Player} from "../src/Player"; -import {clearEventListeners} from "./HelperFunctions"; +import {clearEventListeners} from "./uiHelpers/clearEventListeners"; /* Faction Invitation Pop-up box */ function factionInvitationBoxClose() { diff --git a/utils/HelperFunctions.js b/utils/HelperFunctions.js index c3a785438..a50dfbf73 100644 --- a/utils/HelperFunctions.js +++ b/utils/HelperFunctions.js @@ -1,16 +1,6 @@ //General helper functions 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 function clearEventListenersEl(el) { if (el == null) {console.log("ERR: element passed into clearEventListenersEl is null"); return null;} @@ -72,8 +62,7 @@ function clearSelector(selector) { } } -export {clearEventListeners, - clearEventListenersEl, +export {clearEventListenersEl, removeElement, createAccordionElement, removeChildrenFromElement, diff --git a/utils/InfiltrationBox.js b/utils/InfiltrationBox.js index ed95ab3cb..8d43044e4 100644 --- a/utils/InfiltrationBox.js +++ b/utils/InfiltrationBox.js @@ -3,7 +3,7 @@ import {CONSTANTS} from "../src/Constants"; import {Factions, Faction} from "../src/Faction"; import {Player} from "../src/Player"; import {dialogBoxCreate} from "./DialogBox"; -import {clearEventListeners} from "./HelperFunctions"; +import {clearEventListeners} from "./uiHelpers/clearEventListeners"; import {formatNumber} from "./StringHelperFunctions"; //Keep track of last faction diff --git a/utils/LogBox.js b/utils/LogBox.js index 85df85dd7..615567610 100644 --- a/utils/LogBox.js +++ b/utils/LogBox.js @@ -1,5 +1,5 @@ import {killWorkerScript} from "../src/NetscriptWorker"; -import {clearEventListeners} from "./HelperFunctions"; +import {clearEventListeners} from "./uiHelpers/clearEventListeners"; import {arrayToString} from "./helpers/arrayToString"; $(document).keydown(function(event) { diff --git a/utils/YesNoBox.js b/utils/YesNoBox.js index 8b35c1752..30d85b190 100644 --- a/utils/YesNoBox.js +++ b/utils/YesNoBox.js @@ -1,4 +1,4 @@ -import {clearEventListeners} from "./HelperFunctions"; +import {clearEventListeners} from "./uiHelpers/clearEventListeners"; /* Generic Yes-No Pop-up box * Can be used to create pop-up boxes that require a yes/no response from player */ diff --git a/utils/uiHelpers/clearEventListeners.ts b/utils/uiHelpers/clearEventListeners.ts new file mode 100644 index 000000000..06f4abf99 --- /dev/null +++ b/utils/uiHelpers/clearEventListeners.ts @@ -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; + } +}