113 lines
3.8 KiB
TypeScript
113 lines
3.8 KiB
TypeScript
import * as I from "../types/types";
|
|
import { config } from "./config";
|
|
var iconv = require('iconv-lite');
|
|
|
|
export default class Utils {
|
|
|
|
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 {
|
|
const element: boolean | HTMLElement = inSwitch === "selector" ? jQuery(<string>inSelector).get(0) : Utils.getSeatmapElement();
|
|
|
|
if (element) {
|
|
Utils.consoleLog("ready");
|
|
inCallback();
|
|
}
|
|
else {
|
|
Utils.consoleLog(`Repeating for ${inSwitch}`);
|
|
setTimeout(() => {
|
|
if (inTimeoutInMs && Date.now() - startTimeInMs > inTimeoutInMs)
|
|
return;
|
|
|
|
loopSearch();
|
|
}, inCheckIntervalInMs);
|
|
}
|
|
})();
|
|
}
|
|
|
|
static getSeatmapElement(): boolean {
|
|
const seatCharts: CallableFunction = (<any>window).jQuery("#containerSeatmap").seatCharts;
|
|
return Utils.isFunction(seatCharts);
|
|
}
|
|
|
|
static waitForSeatmap(inCallback: Function): void {
|
|
const seatCharts: CallableFunction = (<any>window).jQuery("#containerSeatmap").seatCharts;
|
|
this.waitForElement(seatCharts, inCallback, "seatmap");
|
|
}
|
|
|
|
static querySelector(selector: string): boolean {
|
|
if (document.querySelector(selector) != null)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
static isFunction(inSelector: CallableFunction): boolean {
|
|
return typeof inSelector === "function" ? true : false;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
static appendScript(inScript: I.TypeInjectScript, inLoc: string): void {
|
|
if (inLoc === "body")
|
|
document.body.appendChild(inScript);
|
|
else if (inLoc === "head")
|
|
document.head.appendChild(inScript);
|
|
}
|
|
|
|
static getDayName(inDate: Date, inLocale: string): string {
|
|
return inDate.toLocaleDateString(inLocale, { weekday: 'long' });
|
|
}
|
|
|
|
static consoleLog(inMessage: any) {
|
|
if (process.env["NODE_ENV"] === "development")
|
|
console.log(inMessage);
|
|
}
|
|
|
|
static getConfigValue(inProperty: I.TypeConfigResourcesProperty): string {
|
|
const branch: I.TypeConfigBranch = config.branch;
|
|
const resources = config.resources[branch];
|
|
return resources[inProperty];
|
|
}
|
|
|
|
static encodeCP850DecodeUTF8(inString: string): string {
|
|
return <string> iconv.decode( iconv.encode(inString, "cp850"), "utf8");
|
|
}
|
|
} |