mapcomplete/Utils.ts

183 lines
5.2 KiB
TypeScript
Raw Normal View History

2020-07-31 04:58:58 +02:00
import {UIElement} from "./UI/UIElement";
import * as $ from "jquery"
2020-07-31 04:58:58 +02:00
2020-07-24 01:12:57 +02:00
export class Utils {
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, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
}
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;
}
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-31 04:58:58 +02:00
static DoEvery(millis: number, f: (() => void)) {
if (UIElement.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)
}
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;
}
public static NoEmpty(array: string[]): string[]{
const ls: string[] = [];
for (const t of array) {
if (t === "") {
continue;
}
ls.push(t);
}
return ls;
}
2020-08-26 00:21:34 +02:00
public static EllipsesAfter(str : string, l : number = 100){
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)+"...";
}
public static Dedup(arr: string[]):string[]{
if(arr === undefined){
return undefined;
}
const newArr = [];
for (const string of arr) {
if (newArr.indexOf(string) < 0) {
newArr.push(string);
}
}
return newArr;
}
public static MergeTags(a: any, b: any) {
const t = {};
for (const k in a) {
t[k] = a[k];
}
for (const k in b) {
t[k] = b[k];
}
return t;
}
public static SplitFirst(a: string, sep: string): string[] {
const index = a.indexOf(sep);
if (index < 0) {
return [a];
}
return [a.substr(0, index), a.substr(index + sep.length)];
}
2020-07-31 04:58:58 +02:00
public static isRetina(): boolean {
if (UIElement.runningFromConsole) {
2020-09-27 21:01:46 +02:00
return;
}
2020-09-27 21:00:37 +02:00
// The cause for this line of code: https://github.com/pietervdvn/MapComplete/issues/115
// See https://stackoverflow.com/questions/19689715/what-is-the-best-way-to-detect-retina-support-on-a-device-using-javascript
return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio >= 2));
}
// Date will be undefined on failure
public static changesetDate(id: number, action: ((isFound: Date) => void)): void {
$.getJSON("https://www.openstreetmap.org/api/0.6/changeset/" + id,
function (data) {
console.log(data)
action(new Date(data.elements[0].created_at));
})
.fail(() => {
action(undefined);
});
}
2020-11-14 03:26:09 +01:00
public static LoadCustomCss(location: string){
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = "customCss";
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = location;
link.media = 'all';
head.appendChild(link);
console.log("Added custom layout ",location)
2020-11-14 03:26:09 +01:00
}
2020-11-17 02:22:48 +01:00
2020-12-05 03:22:17 +01:00
static MatchKeys(object: any, prototype: any, context?: string){
for (const objectKey in object) {
if(prototype[objectKey] === undefined){
console.error("Key ", objectKey, "might be not supported (in context",context,")")
}
}
}
2021-01-04 20:09:07 +01:00
}