revised utils commented old code

This commit is contained in:
zino
2021-05-17 21:19:40 +02:00
parent 5a78e909b2
commit 5eb8f11486
2 changed files with 139 additions and 76 deletions

View File

@@ -1,49 +1,72 @@
import * as I from "../types/types";
export default class Utils { export default class Utils {
static waitForElement(selector: any, callback: Function, type: string = "selector", checkFrequencyInMs: number = 1000, timeoutInMs: number = 10000) { static waitForElement(inSelector: string | CallableFunction, inCallback: Function, inSwitch: I.TypeWaitForElementSwitch = "selector", inCheckIntervalInMs: number = 1000, inTimeoutInMs: number = 10000): void {
let startTimeInMs: number = Date.now(); const startTimeInMs: number = Date.now();
(function loopSearch(): void { (function loopSearch(): void {
let value: boolean; const element: boolean | HTMLElement = inSwitch === "selector" ? jQuery(<string>inSelector).get(0) : Utils.getSeatmapElement();
console.log(selector);
if (type === "selector") if (element) {
value = Utils.querySelector(selector); console.log("ready");
else if (type === "function") inCallback();
value = Utils.isFunction(selector);
else
value = false;
if (value) {
console.log("defined");
callback();
} }
else { else {
setTimeout(function (): void { console.log(`Repeating for ${inSwitch}`);
console.log("repeating"); setTimeout(() => {
if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs) if (inTimeoutInMs && Date.now() - startTimeInMs > inTimeoutInMs)
return; return;
loopSearch(); 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) { static getSeatmapElement(): boolean {
const seatCharts: any = (<any>window).jQuery("#containerSeatmap").seatCharts; const seatCharts: CallableFunction = (<any>window).jQuery("#containerSeatmap").seatCharts;
let startTimeInMs: number = Date.now(); return Utils.isFunction(seatCharts);
}
if (Utils.isFunction(seatCharts)) { static waitForSeatmap(inCallback: Function): void {
console.log("defined"); const seatCharts: CallableFunction = (<any>window).jQuery("#containerSeatmap").seatCharts;
callback(); this.waitForElement(seatCharts, inCallback, "seatmap");
} // const startTimeInMs: number = Date.now();
else {
setTimeout(function (): void { // if (Utils.isFunction(seatCharts))
console.log("repeating"); // inCallback();
if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs) // else {
return; // setTimeout(() => {
Utils.waitForSeatmap(callback); // if (inTimeoutInMs && Date.now() - startTimeInMs > inTimeoutInMs)
}, checkFrequencyInMs); // return;
}
// Utils.waitForSeatmap(inCallback);
// }, inCheckIntervalInMs);
// }
} }
static querySelector(selector: string): boolean { static querySelector(selector: string): boolean {
@@ -53,52 +76,90 @@ export default class Utils {
return false; return false;
} }
static isFunction(selector: string): boolean { static isFunction(inSelector: CallableFunction): boolean {
if (typeof selector === "function") return typeof inSelector === "function" ? true : false;
return true;
else // if (typeof inSelector === "function")
return false; // return true;
// else
// return false;
} }
static async delay(ms: number) { static inject(inContent: string, inType: string = "js", inLoc: string = "head"): void {
return new Promise(resolve => setTimeout(resolve, ms)); var script: I.TypeInjectScript | undefined;
}
static inject(content: string, inType: string = "js", inLoc: string = "head") { switch (inType) {
var script: HTMLLinkElement | HTMLScriptElement | HTMLStyleElement; case "js":
if (inType === "js") {
script = document.createElement('script'); script = document.createElement('script');
script.type = 'text/javascript'; script.type = 'text/javascript';
script.src = content; script.src = inContent;
} break;
else if (inType === "css") {
case "css":
script = document.createElement('link'); script = document.createElement('link');
script.type = 'text/css'; script.type = 'text/css';
(<HTMLLinkElement>script).rel = "stylesheet"; (<HTMLLinkElement>script).rel = "stylesheet";
(<HTMLLinkElement>script).href = content; (<HTMLLinkElement>script).href = inContent;
} break;
else if (inType === "cssCustom") {
case "cssCustom":
script = document.createElement('style'); script = document.createElement('style');
script.type = 'text/css'; script.type = 'text/css';
script.innerHTML = content; script.innerHTML = inContent;
} break;
else if (inType === "script") {
case "script":
script = document.createElement('script'); script = document.createElement('script');
script.type = 'text/javascript'; script.type = 'text/javascript';
script.innerHTML = content; script.innerHTML = inContent;
} break;
else
return;
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';
// (<HTMLLinkElement>script).rel = "stylesheet";
// (<HTMLLinkElement>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 appendScript(inScript: I.TypeInjectScript, inLoc: string): void {
if (inLoc === "body") if (inLoc === "body")
document.body.appendChild(script); document.body.appendChild(inScript);
else if (inLoc === "head") else if (inLoc === "head")
document.head.appendChild(script); document.head.appendChild(inScript);
} }
static getDayName(date: Date, locale: string) { static getDayName(inDate: Date, inLocale: string): string {
return date.toLocaleDateString(locale, { weekday: 'long' }); return inDate.toLocaleDateString(inLocale, { weekday: 'long' });
} }
} }

View File

@@ -426,3 +426,5 @@ export interface TrimPos {
export type TypeBuyerType = (string | undefined)[][] | undefined; export type TypeBuyerType = (string | undefined)[][] | undefined;
export type TypeBuyerTypeArr = (string | undefined)[]; export type TypeBuyerTypeArr = (string | undefined)[];
export type TypeWaitForElementSwitch = "selector" | "seatmap";
export type TypeInjectScript = HTMLLinkElement | HTMLScriptElement | HTMLStyleElement;