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 {
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(<string>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 = (<any>window).jQuery("#containerSeatmap").seatCharts;
let startTimeInMs: number = Date.now();
static getSeatmapElement(): boolean {
const seatCharts: CallableFunction = (<any>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 = (<any>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';
(<HTMLLinkElement>script).rel = "stylesheet";
(<HTMLLinkElement>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';
// (<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 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';
(<HTMLLinkElement>script).rel = "stylesheet";
(<HTMLLinkElement>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' });
}
}
}

View File

@@ -425,4 +425,6 @@ export interface TrimPos {
}
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;