From 2b05d79dbb22388746639ea3a3dac33e5ef7afb5 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sun, 11 Jun 2023 01:32:30 +0200 Subject: [PATCH] Refactoring: port unit picker --- Models/Denomination.ts | 10 +- Models/Unit.ts | 2 +- UI/InputElement/ValidatedInput.svelte | 139 ++++++++++++------ UI/Popup/TagRendering/FreeformInput.svelte | 8 +- .../TagRendering/TagRenderingAnswer.svelte | 2 +- .../TagRendering/TagRenderingQuestion.svelte | 19 ++- UI/Popup/UnitInput.svelte | 67 +++++++++ test.ts | 8 +- 8 files changed, 188 insertions(+), 67 deletions(-) create mode 100644 UI/Popup/UnitInput.svelte diff --git a/Models/Denomination.ts b/Models/Denomination.ts index f3466515b..c4d119ea7 100644 --- a/Models/Denomination.ts +++ b/Models/Denomination.ts @@ -23,7 +23,7 @@ export class Denomination { } this._canonicalSingular = json.canonicalDenominationSingular?.trim() - json.alternativeDenomination.forEach((v, i) => { + json.alternativeDenomination?.forEach((v, i) => { if ((v?.trim() ?? "") === "") { throw `${context}.alternativeDenomination.${i}: invalid alternative denomination: undefined, null or only whitespace` } @@ -51,12 +51,6 @@ export class Denomination { return (this._humanSingular ?? this._human).Clone() } - getToggledHuman(isSingular: Store): BaseUIElement { - if (this._humanSingular === undefined) { - return this.human - } - return new Toggle(this.humanSingular, this.human, isSingular) - } /** * Create a representation of the given value @@ -165,7 +159,7 @@ export class Denomination { return null } - isDefaultUnit(country: () => string) { + isDefaultDenomination(country: () => string) { if (this.useIfNoUnitGiven === true) { return true } diff --git a/Models/Unit.ts b/Models/Unit.ts index 74862557f..af3ab650b 100644 --- a/Models/Unit.ts +++ b/Models/Unit.ts @@ -199,7 +199,7 @@ export class Unit { ) { continue } - let countries: string | string[] = country() + let countries: string | string[] = country() ?? [] if (typeof countries === "string") { countries = countries.split(",") } diff --git a/UI/InputElement/ValidatedInput.svelte b/UI/InputElement/ValidatedInput.svelte index ca73d34cc..1e35b79b3 100644 --- a/UI/InputElement/ValidatedInput.svelte +++ b/UI/InputElement/ValidatedInput.svelte @@ -1,64 +1,109 @@ {#if validator.textArea} - + {:else } - - {#if !$isValid} + + {#if !$isValid} {/if} + + {#if unit !== undefined} + + {/if} {/if} diff --git a/UI/Popup/TagRendering/FreeformInput.svelte b/UI/Popup/TagRendering/FreeformInput.svelte index 83d638d17..2262a8366 100644 --- a/UI/Popup/TagRendering/FreeformInput.svelte +++ b/UI/Popup/TagRendering/FreeformInput.svelte @@ -7,17 +7,20 @@ import {createEventDispatcher, onDestroy} from "svelte"; import InputHelper from "../../InputElement/InputHelper.svelte"; import type {Feature} from "geojson"; + import {Unit} from "../../../Models/Unit"; export let value: UIEventSource; export let config: TagRenderingConfig; export let tags: UIEventSource>; export let feature: Feature = undefined; + export let unit: Unit | undefined let placeholder = config.freeform?.placeholder $: { placeholder = config.freeform?.placeholder } + let inline = config.freeform.inline export let feedback: UIEventSource = new UIEventSource(undefined); @@ -35,13 +38,14 @@ {#if config.freeform.inline} - dispatch("selected")} + dispatch("selected")} type={config.freeform.type} {placeholder} {value}> {:else} - dispatch("selected")} + dispatch("selected")} type={config.freeform.type} {placeholder} {value}> {/if} + diff --git a/UI/Popup/TagRendering/TagRenderingAnswer.svelte b/UI/Popup/TagRendering/TagRenderingAnswer.svelte index df950c93b..eac058a6b 100644 --- a/UI/Popup/TagRendering/TagRenderingAnswer.svelte +++ b/UI/Popup/TagRendering/TagRenderingAnswer.svelte @@ -31,7 +31,7 @@ {#if config !== undefined && (config?.condition === undefined || config.condition.matchesProperties(_tags))} -