From 5eb8f11486384aeb8197e06a0c676f4c1807f528 Mon Sep 17 00:00:00 2001 From: zino Date: Mon, 17 May 2021 21:19:40 +0200 Subject: [PATCH] revised utils commented old code --- client/src/modules/utils.ts | 211 +++++++++++++++++++++++------------- client/src/types/types.d.ts | 4 +- 2 files changed, 139 insertions(+), 76 deletions(-) diff --git a/client/src/modules/utils.ts b/client/src/modules/utils.ts index 8025126..2bf375a 100644 --- a/client/src/modules/utils.ts +++ b/client/src/modules/utils.ts @@ -1,49 +1,72 @@ +import * as I from "../types/types"; + export default class Utils { - static waitForElement(selector: any, callback: Function, type: string = "selector", checkFrequencyInMs: number = 1000, timeoutInMs: number = 10000) { - let startTimeInMs: number = Date.now(); + static waitForElement(inSelector: string | CallableFunction, inCallback: Function, inSwitch: I.TypeWaitForElementSwitch = "selector", inCheckIntervalInMs: number = 1000, inTimeoutInMs: number = 10000): void { + const startTimeInMs: number = Date.now(); + (function loopSearch(): void { - let value: boolean; - console.log(selector); - - if (type === "selector") - value = Utils.querySelector(selector); - else if (type === "function") - value = Utils.isFunction(selector); - else - value = false; - - if (value) { - console.log("defined"); - callback(); - } + const element: boolean | HTMLElement = inSwitch === "selector" ? jQuery(inSelector).get(0) : Utils.getSeatmapElement(); + + if (element) { + console.log("ready"); + inCallback(); + } else { - setTimeout(function (): void { - console.log("repeating"); - if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs) + console.log(`Repeating for ${inSwitch}`); + setTimeout(() => { + if (inTimeoutInMs && Date.now() - startTimeInMs > inTimeoutInMs) return; + loopSearch(); - }, checkFrequencyInMs); + }, inCheckIntervalInMs); } + + // let value: boolean = Utils.querySelector(inSelector); + // console.log(inSelector); + + // if (inSwitch === "selector") + // value = Utils.querySelector(inSelector); + // else if (inSwitch === "function") + // value = Utils.isFunction(inSelector); + // else + // value = false; + + // if (value) { + // console.log("defined"); + // inCallback(); + // } + // else { + // setTimeout(function (): void { + // console.log("repeating"); + // if (inTimeoutInMs && Date.now() - startTimeInMs > inTimeoutInMs) + // return; + // loopSearch(); + // }, inCheckIntervalInMs); + // } })(); } - static waitForSeatmap(callback: Function, checkFrequencyInMs: number = 100, timeoutInMs: number = 10000) { - const seatCharts: any = (window).jQuery("#containerSeatmap").seatCharts; - let startTimeInMs: number = Date.now(); + static getSeatmapElement(): boolean { + const seatCharts: CallableFunction = (window).jQuery("#containerSeatmap").seatCharts; + return Utils.isFunction(seatCharts); + } - if (Utils.isFunction(seatCharts)) { - console.log("defined"); - callback(); - } - else { - setTimeout(function (): void { - console.log("repeating"); - if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs) - return; - Utils.waitForSeatmap(callback); - }, checkFrequencyInMs); - } + static waitForSeatmap(inCallback: Function): void { + const seatCharts: CallableFunction = (window).jQuery("#containerSeatmap").seatCharts; + this.waitForElement(seatCharts, inCallback, "seatmap"); + // const startTimeInMs: number = Date.now(); + + // if (Utils.isFunction(seatCharts)) + // inCallback(); + // else { + // setTimeout(() => { + // if (inTimeoutInMs && Date.now() - startTimeInMs > inTimeoutInMs) + // return; + + // Utils.waitForSeatmap(inCallback); + // }, inCheckIntervalInMs); + // } } static querySelector(selector: string): boolean { @@ -53,52 +76,90 @@ export default class Utils { return false; } - static isFunction(selector: string): boolean { - if (typeof selector === "function") - return true; - else - return false; + static isFunction(inSelector: CallableFunction): boolean { + return typeof inSelector === "function" ? true : false; + + // if (typeof inSelector === "function") + // return true; + // else + // return false; } - static async delay(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); + static inject(inContent: string, inType: string = "js", inLoc: string = "head"): void { + var script: I.TypeInjectScript | undefined; + + switch (inType) { + case "js": + script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = inContent; + break; + + case "css": + script = document.createElement('link'); + script.type = 'text/css'; + (script).rel = "stylesheet"; + (script).href = inContent; + break; + + case "cssCustom": + script = document.createElement('style'); + script.type = 'text/css'; + script.innerHTML = inContent; + break; + + case "script": + script = document.createElement('script'); + script.type = 'text/javascript'; + script.innerHTML = inContent; + break; + + default: + script = undefined; + break; + } + + if (script) + Utils.appendScript(script, inLoc); + + // if (inType === "js") { + // script = document.createElement('script'); + // script.type = 'text/javascript'; + // script.src = inContent; + // } + // else if (inType === "css") { + // script = document.createElement('link'); + // script.type = 'text/css'; + // (script).rel = "stylesheet"; + // (script).href = inContent; + // } + // else if (inType === "cssCustom") { + // script = document.createElement('style'); + // script.type = 'text/css'; + // script.innerHTML = inContent; + // } + // else if (inType === "script") { + // script = document.createElement('script'); + // script.type = 'text/javascript'; + // script.innerHTML = inContent; + // } + // else + // return; + + // if (inLoc === "body") + // document.body.appendChild(script); + // else if (inLoc === "head") + // document.head.appendChild(script); } - static inject(content: string, inType: string = "js", inLoc: string = "head") { - var script: HTMLLinkElement | HTMLScriptElement | HTMLStyleElement; - - if (inType === "js") { - script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = content; - } - else if (inType === "css") { - script = document.createElement('link'); - script.type = 'text/css'; - (script).rel = "stylesheet"; - (script).href = content; - } - else if (inType === "cssCustom") { - script = document.createElement('style'); - script.type = 'text/css'; - script.innerHTML = content; - } - else if (inType === "script") { - script = document.createElement('script'); - script.type = 'text/javascript'; - script.innerHTML = content; - } - else - return; - + static appendScript(inScript: I.TypeInjectScript, inLoc: string): void { if (inLoc === "body") - document.body.appendChild(script); + document.body.appendChild(inScript); else if (inLoc === "head") - document.head.appendChild(script); + document.head.appendChild(inScript); } - static getDayName(date: Date, locale: string) { - return date.toLocaleDateString(locale, { weekday: 'long' }); + static getDayName(inDate: Date, inLocale: string): string { + return inDate.toLocaleDateString(inLocale, { weekday: 'long' }); } -} - +} \ No newline at end of file diff --git a/client/src/types/types.d.ts b/client/src/types/types.d.ts index 1aad69e..0a0a988 100644 --- a/client/src/types/types.d.ts +++ b/client/src/types/types.d.ts @@ -425,4 +425,6 @@ export interface TrimPos { } export type TypeBuyerType = (string | undefined)[][] | undefined; -export type TypeBuyerTypeArr = (string | undefined)[]; \ No newline at end of file +export type TypeBuyerTypeArr = (string | undefined)[]; +export type TypeWaitForElementSwitch = "selector" | "seatmap"; +export type TypeInjectScript = HTMLLinkElement | HTMLScriptElement | HTMLStyleElement; \ No newline at end of file