Files
qt5-legatus-legacy/Lts/Tools/ArAssetsV2/src/Helpers.ts
T
2026-08-13 18:33:10 +02:00

159 lines
3.4 KiB
TypeScript
Executable File

import * as UTM from "utm";
import angular from "angular";
/**
* Checks for emptyness
* @returns obj == undefined || obj == null || obj == ""
*/
export function _(obj: any): boolean {
return obj == undefined || obj == null || obj == "" || obj == [];
}
/**
* Retrieves a DOM element
* @returns document.querySelector(query)
*/
export function $(query: string): HTMLElement | null {
return document.querySelector(query);
}
export const MOBILE: boolean = /Android/i.test(navigator.userAgent);
declare global {
interface Array<T> {
includes(obj: any): boolean;
}
}
/**
* Checks if the array includes given element
*/
Array.prototype.includes = function(obj: any) {
return this.find(element => element == obj) != undefined;
};
export module Helpers {
export namespace Calculations {
export function RadToDeg(rad: number): number {
return rad * (180 / Math.PI);
}
export function DegToRad(deg: number): number {
return (deg * Math.PI) / 180;
}
export function RotatePoint(
point: { x: number; y: number },
angleRad: number
): { x: number; y: number } {
return {
x: point.x * Math.cos(angleRad) - point.y * Math.sin(angleRad),
y: point.x * Math.sin(angleRad) + point.y * Math.cos(angleRad)
};
}
}
/**
* Retrieves angular scope
*/
export function GetScope(): any {
return angular.element($("body") as Element).scope() as any;
}
/**
* ArAssets-YYYY-MM-DDTHH:mm:ss.sssZ
*/
export function GenUuid(): string {
return `ArAssets-${new Date().toISOString()}`;
}
export class WebSocketRequest {
/**
* Execute with .GetResponseAsync()
*/
constructor(private Ip: string, private Request: string) {}
/**
* Resolves into the response of the websocket
*/
public async GetResponseAsync(): Promise<string> {
return new Promise<string>((resolve, reject) => {
try {
let ws = new WebSocket(this.Ip);
ws.onopen = () => {
ws.send(this.Request);
};
ws.onmessage = response => {
ws.close();
return resolve(response.data);
};
ws.onclose = event => {
if (event.code > 1000) return reject(event.reason);
};
ws.onerror = error => {
return reject(error);
};
} catch {
return reject(); // Just to be sure
}
});
}
}
/** @deprecated */
export async function GetPositionAsync(): Promise<Coordinates.LatLon> {
return new Promise<Coordinates.LatLon>((resolve, reject) => {
navigator.geolocation.getCurrentPosition(pos => {
resolve(
new Coordinates.LatLon(
pos.coords.latitude,
pos.coords.longitude
)
);
});
reject();
});
}
export namespace Coordinates {
// const utm: UTMLatLng = new UTMLatLng("WGS 84");
// const PRECISION: number = 7;
export class LatLon {
constructor(public Lat: number, public Lon: number) {}
public ToUtm(): Utm {
let temp = UTM.fromLatLon(
this.Lat,
this.Lon
);
return new Utm(
temp.easting,
temp.northing,
temp.zoneNum,
temp.zoneLetter
);
}
}
export class Utm {
constructor(
public Easting: number,
public Northing: number,
public ZoneNumber: number,
public ZoneLetter: string
) {}
public ToLatLon(): LatLon {
let temp = UTM.toLatLon(
this.Easting,
this.Northing,
this.ZoneNumber,
this.ZoneLetter
);
return new LatLon(temp.latitude, temp.longitude);
}
}
}
}