From 1644b0594f0b56cb74656ec67f74e244987d2c14 Mon Sep 17 00:00:00 2001 From: kruschen Date: Tue, 3 Sep 2024 20:37:32 +0000 Subject: [PATCH] clean up byId function --- src/modules/ui/options.ts | 2 +- src/utils/nodeUtils.ts | 22 +++------------------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/modules/ui/options.ts b/src/modules/ui/options.ts index e5391706..5bd21b92 100644 --- a/src/modules/ui/options.ts +++ b/src/modules/ui/options.ts @@ -1083,7 +1083,7 @@ function enterStandardView() { byId("heightmap3DView").classList.remove("pressed"); byId("viewStandard").classList.add("pressed"); - if (!byId("canvas3d")) return; + if (!byId("canvas3d",{throwOnNull:false})) return; ThreeD.stop(); byId("canvas3d").remove(); if (byId("options3dUpdate").offsetParent) $("#options3d").dialog("close"); diff --git a/src/utils/nodeUtils.ts b/src/utils/nodeUtils.ts index 3a07fe64..8d7e88b3 100644 --- a/src/utils/nodeUtils.ts +++ b/src/utils/nodeUtils.ts @@ -10,26 +10,10 @@ export type ElementMap = { // add more types as needed }; -type ElementMapKeys = keyof ElementMap; - -interface ByIdOptions { - throwOnNull?: boolean; - // add more options as needed -} - // function definition with overloads to account for different options -export function byId(id: string, options?: ByIdOptions & {throwOnNull: true}): ElementMap[K]; -export function byId(id: string, options: ByIdOptions & {throwOnNull: boolean}): ElementMap[K]|null; -/** - * Retrieves an element from the DOM by its ID. - * @template K - The key of the element in the ElementMap. - * @param {string} id - The ID of the element to retrieve. - * @param {ByIdOptions} [options] - The options for retrieving the element. - * @param {boolean} [options.throwOnNull=true] - Whether to throw an error if the element is not found. - * @returns {ElementMap[K] | null} The retrieved element or null if not found. - * @throws {Error} If the element is not found and options.throwOnNull is true. - */ -export function byId(id: string, options: ByIdOptions = {throwOnNull: true}) { +export function byId(id: string): ElementMap[K]; +export function byId(id: string, options?: {throwOnNull: false}): ElementMap[K] | null; +export function byId(id: string, options = {throwOnNull: true}) { const element = document.getElementById(id); if (!element && options.throwOnNull) { throw new Error(`Element ${id} not found`);