From 89b78ee709cad734f09973f6d7136a230d87ac85 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Fri, 24 Jul 2020 17:53:09 +0200 Subject: [PATCH] Add current location in localstorage, fixes #13 --- Logic/LocalStorageSource.ts | 15 +++++++++++++++ UI/UIEventSource.ts | 3 ++- UI/i18n/Locale.ts | 21 ++------------------- index.html | 4 ++-- index.ts | 10 +++++++--- 5 files changed, 28 insertions(+), 25 deletions(-) create mode 100644 Logic/LocalStorageSource.ts diff --git a/Logic/LocalStorageSource.ts b/Logic/LocalStorageSource.ts new file mode 100644 index 0000000..ab8ac3a --- /dev/null +++ b/Logic/LocalStorageSource.ts @@ -0,0 +1,15 @@ +import {UIEventSource} from "../UI/UIEventSource"; + +export class LocalStorageSource { + + static Get(key: string, defaultValue: string = undefined): UIEventSource { + const saved = localStorage.getItem(key); + const source = new UIEventSource(saved ?? defaultValue); + + source.addCallback((data) => { + localStorage.setItem(key, data); + console.log("Wriging ", key, data) + }); + return source; + } +} \ No newline at end of file diff --git a/UI/UIEventSource.ts b/UI/UIEventSource.ts index 9529468..579cb8b 100644 --- a/UI/UIEventSource.ts +++ b/UI/UIEventSource.ts @@ -67,7 +67,7 @@ export class UIEventSource{ } - public syncWith(otherSource: UIEventSource){ + public syncWith(otherSource: UIEventSource) : UIEventSource{ this.addCallback((latest) => otherSource.setData(latest)); const self = this; otherSource.addCallback((latest) => self.setData(latest)); @@ -76,6 +76,7 @@ export class UIEventSource{ }else{ otherSource.setData(this.data); } + return this; } } \ No newline at end of file diff --git a/UI/i18n/Locale.ts b/UI/i18n/Locale.ts index 3bc55cb..0fdf932 100644 --- a/UI/i18n/Locale.ts +++ b/UI/i18n/Locale.ts @@ -1,24 +1,7 @@ import {UIEventSource} from "../UIEventSource"; -import {OsmConnection} from "../../Logic/OsmConnection"; +import {LocalStorageSource} from "../../Logic/LocalStorageSource"; export default class Locale { - public static language: UIEventSource = Locale.getInitialLanguage(); - - private static getInitialLanguage() { - // The key to save in local storage - const LANGUAGE_KEY = 'language' - - const lng = new UIEventSource("en"); - const saved = localStorage.getItem(LANGUAGE_KEY); - lng.setData(saved); - - - lng.addCallback(data => { - console.log("Selected language", data); - localStorage.setItem(LANGUAGE_KEY, data) - }); - - return lng; - } + public static language: UIEventSource = LocalStorageSource.Get('language', "en"); } diff --git a/index.html b/index.html index 2ce3672..03d6a53 100644 --- a/index.html +++ b/index.html @@ -10,18 +10,18 @@ crossorigin=""/> - +
diff --git a/index.ts b/index.ts index ba271f0..1e879b8 100644 --- a/index.ts +++ b/index.ts @@ -30,6 +30,7 @@ import Combine from "./UI/Base/Combine"; import {Img} from "./UI/Img"; import {QueryParameters} from "./Logic/QueryParameters"; import {Utils} from "./Utils"; +import {LocalStorageSource} from "./Logic/LocalStorageSource"; // --------------------- Special actions based on the parameters ----------------- @@ -92,9 +93,12 @@ const fullScreenMessage = new UIEventSource(undefined); // The latest element that was selected - used to generate the right UI at the right place const selectedElement = new UIEventSource<{ feature: any }>(undefined); -const zoom = QueryParameters.GetQueryParameter("z"); -const lat = QueryParameters.GetQueryParameter("lat"); -const lon = QueryParameters.GetQueryParameter("lon"); +const zoom = QueryParameters.GetQueryParameter("z") + .syncWith(LocalStorageSource.Get("zoom")); +const lat = QueryParameters.GetQueryParameter("lat") + .syncWith(LocalStorageSource.Get("lat")); +const lon = QueryParameters.GetQueryParameter("lon") + .syncWith(LocalStorageSource.Get("lon")); const locationControl = new UIEventSource<{ lat: number, lon: number, zoom: number }>({