From 085d762bff787ecd106ca705d60124b10f1a3342 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Tue, 9 Mar 2021 13:10:48 +0100 Subject: [PATCH] Small code cleanups, documentation --- UI/Base/LazyElement.ts | 2 +- UI/Popup/FeatureInfoBox.ts | 15 +++-------- UI/Reviews/ReviewElement.ts | 3 ++- Utils.ts | 51 ++++++++++++++++++++++--------------- 4 files changed, 37 insertions(+), 34 deletions(-) diff --git a/UI/Base/LazyElement.ts b/UI/Base/LazyElement.ts index 99161dd..ea7d30c 100644 --- a/UI/Base/LazyElement.ts +++ b/UI/Base/LazyElement.ts @@ -18,7 +18,7 @@ export default class LazyElement extends UIElement { } self.Update(); // @ts-ignore - if(this._content.Activate){ + if (this._content.Activate) { // THis is ugly - I know // @ts-ignore this._content.Activate(); diff --git a/UI/Popup/FeatureInfoBox.ts b/UI/Popup/FeatureInfoBox.ts index 1e92baf..30432cf 100644 --- a/UI/Popup/FeatureInfoBox.ts +++ b/UI/Popup/FeatureInfoBox.ts @@ -8,6 +8,7 @@ import TagRenderingAnswer from "./TagRenderingAnswer"; import State from "../../State"; import TagRenderingConfig from "../../Customizations/JSON/TagRenderingConfig"; import ScrollableFullScreen from "../Base/ScrollableFullScreen"; +import {Utils} from "../../Utils"; export default class FeatureInfoBox extends ScrollableFullScreen { private static featureInfoboxCache: Map, FeatureInfoBox>> = new Map, FeatureInfoBox>>(); @@ -24,18 +25,8 @@ export default class FeatureInfoBox extends ScrollableFullScreen { } static construct(tags: UIEventSource, layer: LayerConfig): FeatureInfoBox { - let innerMap = FeatureInfoBox.featureInfoboxCache.get(layer); - if (innerMap === undefined) { - innerMap = new Map, FeatureInfoBox>(); - FeatureInfoBox.featureInfoboxCache.set(layer, innerMap); - } - - let featureInfoBox = innerMap.get(tags); - if (featureInfoBox === undefined) { - featureInfoBox = new FeatureInfoBox(tags, layer); - innerMap.set(tags, featureInfoBox); - } - return featureInfoBox; + let innerMap = Utils.getOrSetDefault(FeatureInfoBox.featureInfoboxCache, layer,() => new Map, FeatureInfoBox>()) + return Utils.getOrSetDefault(innerMap, tags, () => new FeatureInfoBox(tags, layer)); } private static GenerateTitleBar(tags: UIEventSource, diff --git a/UI/Reviews/ReviewElement.ts b/UI/Reviews/ReviewElement.ts index 2b6de01..6eef928 100644 --- a/UI/Reviews/ReviewElement.ts +++ b/UI/Reviews/ReviewElement.ts @@ -1,5 +1,6 @@ /** - * Shows the reviews and scoring base on mangrove.reviesw + * Shows the reviews and scoring base on mangrove.reviews + * The middle element is some other component shown in the middle, e.g. the review input element */ import {UIEventSource} from "../../Logic/UIEventSource"; import {Review} from "../../Logic/Web/Review"; diff --git a/Utils.ts b/Utils.ts index 3af878a..5439a69 100644 --- a/Utils.ts +++ b/Utils.ts @@ -8,7 +8,7 @@ export class Utils { * This is a workaround and yet another hack */ public static runningFromConsole = false; - + public static readonly assets_path = "./assets/svg/"; static EncodeXmlValue(str) { @@ -46,7 +46,7 @@ export class Utils { } public static Round(i: number) { - if(i < 0){ + if (i < 0) { return "-" + Utils.Round(-i); } const j = "" + Math.floor(i * 10); @@ -86,8 +86,8 @@ export class Utils { } return ls; } - - public static NoEmpty(array: string[]): string[]{ + + public static NoEmpty(array: string[]): string[] { const ls: string[] = []; for (const t of array) { if (t === "") { @@ -98,18 +98,18 @@ export class Utils { return ls; } - public static EllipsesAfter(str : string, l : number = 100){ - if(str === undefined){ + public static EllipsesAfter(str: string, l: number = 100) { + if (str === undefined) { return undefined; } - if(str.length <= l){ + if (str.length <= l) { return str; } - return str.substr(0, l - 3)+"..."; + return str.substr(0, l - 3) + "..."; } - - public static Dedup(arr: string[]):string[]{ - if(arr === undefined){ + + public static Dedup(arr: string[]): string[] { + if (arr === undefined) { return undefined; } const newArr = []; @@ -141,7 +141,7 @@ export class Utils { } // Date will be undefined on failure - public static LoadCustomCss(location: string){ + public static LoadCustomCss(location: string) { const head = document.getElementsByTagName('head')[0]; const link = document.createElement('link'); link.id = "customCss"; @@ -150,26 +150,37 @@ export class Utils { link.href = location; link.media = 'all'; head.appendChild(link); - console.log("Added custom layout ",location) + console.log("Added custom layout ", location) } - static Merge(source: any, target: any){ + + 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){ + if (typeof sourceV === "object") { + if (targetV === undefined) { target[key] = sourceV; - }else{ + } else { Utils.Merge(sourceV, targetV); } - - }else{ + + } else { target[key] = sourceV; } - + } return target; } + + static getOrSetDefault(dict: Map, k: K, v: () => V) { + let found = dict.get(k); + if (found !== undefined) { + return found; + } + dict.set(k, v()); + return dict.get(k); + + } }