2020-10-02 19:00:24 +02:00
|
|
|
import * as $ from "jquery"
|
2020-07-31 04:58:58 +02:00
|
|
|
|
2020-07-24 01:12:57 +02:00
|
|
|
export class Utils {
|
|
|
|
|
2021-01-06 02:21:50 +01:00
|
|
|
/**
|
|
|
|
* In the 'deploy'-step, some code needs to be run by ts-node.
|
|
|
|
* However, ts-node crashes when it sees 'document'. When running from console, we flag this and disable all code where document is needed.
|
|
|
|
* This is a workaround and yet another hack
|
|
|
|
*/
|
|
|
|
public static runningFromConsole = false;
|
|
|
|
|
2020-11-17 02:22:48 +01:00
|
|
|
public static readonly assets_path = "./assets/svg/";
|
2020-09-30 22:22:58 +02:00
|
|
|
|
|
|
|
static EncodeXmlValue(str) {
|
|
|
|
return str.replace(/&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''')
|
|
|
|
}
|
|
|
|
|
2020-07-24 01:12:57 +02:00
|
|
|
/**
|
|
|
|
* Gives a clean float, or undefined if parsing fails
|
|
|
|
* @param str
|
|
|
|
*/
|
|
|
|
static asFloat(str): number {
|
|
|
|
if (str) {
|
|
|
|
const i = parseFloat(str);
|
|
|
|
if (isNaN(i)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-09-30 22:22:58 +02:00
|
|
|
|
|
|
|
public static Upper(str: string) {
|
|
|
|
return str.substr(0, 1).toUpperCase() + str.substr(1);
|
|
|
|
}
|
|
|
|
|
2020-10-04 01:04:46 +02:00
|
|
|
public static TwoDigits(i: number) {
|
|
|
|
if (i < 10) {
|
|
|
|
return "0" + i;
|
|
|
|
}
|
|
|
|
return "" + i;
|
|
|
|
}
|
|
|
|
|
2020-10-30 00:56:46 +01:00
|
|
|
public static Round(i: number) {
|
|
|
|
if(i < 0){
|
|
|
|
return "-" + Utils.Round(-i);
|
|
|
|
}
|
|
|
|
const j = "" + Math.floor(i * 10);
|
|
|
|
if (j.length == 1) {
|
|
|
|
return "0." + j;
|
|
|
|
}
|
|
|
|
return j.substr(0, j.length - 1) + "." + j.substr(j.length - 1, j.length);
|
|
|
|
}
|
|
|
|
|
2020-10-04 01:04:46 +02:00
|
|
|
public static Times(f: ((i: number) => string), count: number): string {
|
2020-09-30 22:22:58 +02:00
|
|
|
let res = "";
|
|
|
|
for (let i = 0; i < count; i++) {
|
2020-10-04 01:04:46 +02:00
|
|
|
res += f(i);
|
2020-09-30 22:22:58 +02:00
|
|
|
}
|
|
|
|
return res;
|
2020-07-24 14:46:25 +02:00
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
|
|
|
|
static DoEvery(millis: number, f: (() => void)) {
|
2021-01-06 02:21:50 +01:00
|
|
|
if (Utils.runningFromConsole) {
|
2020-07-31 17:11:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
window.setTimeout(
|
|
|
|
function () {
|
|
|
|
f();
|
|
|
|
Utils.DoEvery(millis, f);
|
|
|
|
}
|
|
|
|
, millis)
|
|
|
|
}
|
|
|
|
|
2020-08-08 02:16:42 +02:00
|
|
|
public static NoNull<T>(array: T[]): T[] {
|
|
|
|
const ls: T[] = [];
|
|
|
|
for (const t of array) {
|
|
|
|
if (t === undefined || t === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ls.push(t);
|
|
|
|
}
|
|
|
|
return ls;
|
|
|
|
}
|
2020-08-22 13:02:31 +02:00
|
|
|
|
|
|
|
public static NoEmpty(array: string[]): string[]{
|
|
|
|
const ls: string[] = [];
|
|
|
|
for (const t of array) {
|
|
|
|
if (t === "") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ls.push(t);
|
|
|
|
}
|
|
|
|
return ls;
|
|
|
|
}
|
2020-08-08 02:16:42 +02:00
|
|
|
|
2020-08-26 00:21:34 +02:00
|
|
|
public static EllipsesAfter(str : string, l : number = 100){
|
2020-09-03 03:16:43 +02:00
|
|
|
if(str === undefined){
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-08-26 00:21:34 +02:00
|
|
|
if(str.length <= l){
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
return str.substr(0, l - 3)+"...";
|
|
|
|
}
|
2020-08-26 15:36:04 +02:00
|
|
|
|
|
|
|
public static Dedup(arr: string[]):string[]{
|
|
|
|
if(arr === undefined){
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const newArr = [];
|
|
|
|
for (const string of arr) {
|
2020-10-02 19:00:24 +02:00
|
|
|
if (newArr.indexOf(string) < 0) {
|
2020-08-26 15:36:04 +02:00
|
|
|
newArr.push(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newArr;
|
|
|
|
}
|
2020-10-02 19:00:24 +02:00
|
|
|
|
|
|
|
public static MergeTags(a: any, b: any) {
|
2020-08-27 00:08:00 +02:00
|
|
|
const t = {};
|
|
|
|
for (const k in a) {
|
|
|
|
t[k] = a[k];
|
|
|
|
}
|
|
|
|
for (const k in b) {
|
|
|
|
t[k] = b[k];
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
2020-10-02 19:00:24 +02:00
|
|
|
|
|
|
|
public static SplitFirst(a: string, sep: string): string[] {
|
2020-08-31 02:59:47 +02:00
|
|
|
const index = a.indexOf(sep);
|
2020-10-02 19:00:24 +02:00
|
|
|
if (index < 0) {
|
2020-08-31 02:59:47 +02:00
|
|
|
return [a];
|
|
|
|
}
|
2020-10-02 19:00:24 +02:00
|
|
|
return [a.substr(0, index), a.substr(index + sep.length)];
|
2020-08-31 02:59:47 +02:00
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
|
2020-10-02 19:00:24 +02:00
|
|
|
// Date will be undefined on failure
|
2020-11-14 03:26:09 +01:00
|
|
|
public static LoadCustomCss(location: string){
|
2021-02-20 16:48:42 +01:00
|
|
|
const head = document.getElementsByTagName('head')[0];
|
|
|
|
const link = document.createElement('link');
|
2020-11-14 03:26:09 +01:00
|
|
|
link.id = "customCss";
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.type = 'text/css';
|
|
|
|
link.href = location;
|
|
|
|
link.media = 'all';
|
|
|
|
head.appendChild(link);
|
2020-11-15 00:27:55 +01:00
|
|
|
console.log("Added custom layout ",location)
|
2020-11-14 03:26:09 +01:00
|
|
|
}
|
2021-01-06 02:52:38 +01:00
|
|
|
static Merge(source: any, target: any){
|
|
|
|
target = JSON.parse(JSON.stringify(target));
|
|
|
|
source = JSON.parse(JSON.stringify(source));
|
|
|
|
for (const key in source) {
|
|
|
|
const sourceV = source[key];
|
|
|
|
const targetV = target[key]
|
|
|
|
if(typeof sourceV === "object"){
|
|
|
|
if(targetV === undefined){
|
|
|
|
target[key] = sourceV;
|
|
|
|
}else{
|
|
|
|
Utils.Merge(sourceV, targetV);
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
target[key] = sourceV;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
2020-07-24 14:46:25 +02:00
|
|
|
}
|