2022-11-07 15:55:27 +01:00
|
|
|
import {UrlMatcher, UrlSegment} from "@angular/router";
|
|
|
|
|
2019-04-24 12:39:15 +02:00
|
|
|
export class HelperFunctions {
|
|
|
|
|
2023-02-15 22:56:16 +01:00
|
|
|
public static scroll(smooth: boolean = false) {
|
2022-07-13 19:25:19 +02:00
|
|
|
HelperFunctions.scrollTo(0,0);
|
2020-05-21 15:17:28 +02:00
|
|
|
}
|
2022-07-13 19:25:19 +02:00
|
|
|
|
2023-02-15 22:56:16 +01:00
|
|
|
public static scrollTo(x,y, smooth: boolean = false) {
|
2019-04-24 12:39:15 +02:00
|
|
|
if (typeof document !== 'undefined') {
|
2023-02-15 22:56:16 +01:00
|
|
|
if(!smooth) {
|
|
|
|
window.scrollTo(x, y);
|
|
|
|
} else {
|
|
|
|
window.scrollTo({
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
behavior: "smooth"
|
|
|
|
})
|
|
|
|
}
|
2019-04-24 12:39:15 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 12:51:39 +02:00
|
|
|
|
2020-05-21 15:17:28 +02:00
|
|
|
public static scrollToId(elementId:string) {
|
|
|
|
if (typeof document !== 'undefined' && document.getElementById(elementId)) {
|
2022-02-16 11:01:41 +01:00
|
|
|
document.getElementById(elementId).scrollIntoView({behavior: "smooth"});
|
2020-05-21 15:17:28 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 11:01:41 +01:00
|
|
|
|
2019-11-22 09:51:17 +01:00
|
|
|
public static copy(obj: any): any {
|
|
|
|
let copy;
|
|
|
|
|
|
|
|
// Handle the 3 simple types, and null or undefined
|
|
|
|
if (null == obj || "object" != typeof obj) return obj;
|
|
|
|
|
|
|
|
// Handle Date
|
|
|
|
if (obj instanceof Date) {
|
|
|
|
copy = new Date();
|
|
|
|
copy.setTime(obj.getTime());
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Array
|
|
|
|
if (obj instanceof Array) {
|
|
|
|
copy = [];
|
|
|
|
for (let i = 0, len = obj.length; i < len; i++) {
|
|
|
|
copy[i] = HelperFunctions.copy(obj[i]);
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Map
|
|
|
|
if (obj instanceof Map) {
|
2019-11-24 17:28:25 +01:00
|
|
|
return new Map(obj.entries());
|
2019-11-22 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Object
|
|
|
|
if (obj instanceof Object) {
|
|
|
|
copy = {};
|
|
|
|
for (let attr in obj) {
|
|
|
|
if (obj.hasOwnProperty(attr)) {
|
|
|
|
copy[attr] = HelperFunctions.copy(obj[attr]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
throw new Error("Unable to copy obj! Its type isn't supported.");
|
2019-11-11 14:27:18 +01:00
|
|
|
}
|
2021-04-21 17:28:35 +02:00
|
|
|
|
|
|
|
public static equals(object1, object2) {
|
|
|
|
return object1 === object2 || JSON.stringify(object1) === JSON.stringify(object2);
|
|
|
|
}
|
2019-11-12 16:54:03 +01:00
|
|
|
|
|
|
|
public static encodeArray(elements: string[]): string[] {
|
|
|
|
let encoded: string[] = [];
|
|
|
|
elements.forEach(element => {
|
|
|
|
encoded.push(encodeURIComponent(element));
|
|
|
|
});
|
|
|
|
return encoded;
|
|
|
|
}
|
2021-04-28 17:03:45 +02:00
|
|
|
|
|
|
|
public static getValues(value: any): any[] {
|
|
|
|
return Object.keys(value).map(key => value[key]);
|
|
|
|
}
|
2022-03-15 22:31:10 +01:00
|
|
|
|
2022-09-08 23:35:11 +02:00
|
|
|
public static getVocabularyLabel(value: any, vocabulary: any, returnIfNotFound: boolean = true) {
|
|
|
|
if(value == undefined || value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-06 15:28:51 +02:00
|
|
|
if(vocabulary && value in vocabulary) {
|
|
|
|
return vocabulary[value];
|
|
|
|
}
|
2022-09-08 23:35:11 +02:00
|
|
|
if(returnIfNotFound) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return null;
|
2022-09-06 15:28:51 +02:00
|
|
|
}
|
|
|
|
|
2022-03-15 22:31:10 +01:00
|
|
|
public static sortSDGs(sgd1: string, sdg2: string): number {
|
|
|
|
let splitA: string[] = sgd1.split(".");
|
|
|
|
let numA: number;
|
|
|
|
if(splitA && splitA.length > 0) {
|
|
|
|
numA = +splitA[0];
|
|
|
|
}
|
|
|
|
let splitB: string[] = sdg2.split(".");
|
|
|
|
let numB: number;
|
|
|
|
if(splitB && splitB.length > 0) {
|
|
|
|
numB = +splitB[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(numA && numB) {
|
|
|
|
return numA - numB;
|
|
|
|
} else {
|
|
|
|
return sgd1.localeCompare(sdg2);
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 13:37:07 +02:00
|
|
|
|
|
|
|
public static swap(array: any[], from, to) {
|
|
|
|
array.splice(to, 0, array.splice(from, 1)[0]);
|
|
|
|
}
|
2022-11-07 15:55:27 +01:00
|
|
|
|
|
|
|
public static routingMatcher:((paths: string[]) => UrlMatcher) = (paths: string[]) => {
|
|
|
|
return (segments) => {
|
|
|
|
const matchingPathIndex = paths.findIndex((path, index) => {
|
|
|
|
const pathSegments = path.split("/");
|
|
|
|
return segments.every((segment, i) =>
|
|
|
|
pathSegments.length > i && (
|
|
|
|
pathSegments[i].startsWith(":") ? true : segment.path.toLowerCase() === pathSegments[i].toLowerCase()));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (matchingPathIndex >= 0) {
|
|
|
|
const matchingPath = paths[matchingPathIndex];
|
|
|
|
|
|
|
|
const consumed: UrlSegment[] = [];
|
|
|
|
const params = {};
|
|
|
|
|
|
|
|
matchingPath.split("/").forEach((path, i) => {
|
|
|
|
consumed.push(segments[i]);
|
|
|
|
if (path.startsWith(":")) {
|
|
|
|
const param = path.substring(1);
|
|
|
|
params[param] = segments[i];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
consumed: consumed,
|
|
|
|
posParams: params
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
};
|
2019-06-06 12:51:39 +02:00
|
|
|
}
|