2023-06-11 01:32:30 +02:00
|
|
|
<script lang="ts">
|
2023-12-12 03:46:51 +01:00
|
|
|
import { Unit } from "../../Models/Unit";
|
|
|
|
import { Store, UIEventSource } from "../../Logic/UIEventSource";
|
|
|
|
import Tr from "../Base/Tr.svelte";
|
|
|
|
import { onDestroy, onMount } from "svelte";
|
|
|
|
import { Denomination } from "../../Models/Denomination";
|
2023-06-11 01:32:30 +02:00
|
|
|
|
2023-12-12 03:46:51 +01:00
|
|
|
export let unit: Unit;
|
2023-06-11 01:32:30 +02:00
|
|
|
|
2023-06-14 20:39:36 +02:00
|
|
|
/**
|
|
|
|
* The current value of the input field
|
2023-12-12 03:46:51 +01:00
|
|
|
* Not necessarily a correct number, should not contain the denomination
|
2023-06-14 20:39:36 +02:00
|
|
|
*/
|
2023-12-12 03:46:51 +01:00
|
|
|
export let textValue: UIEventSource<string>;
|
2023-06-14 20:39:36 +02:00
|
|
|
/**
|
2023-12-12 03:46:51 +01:00
|
|
|
* The actual _valid_ value that is upstreamed, including the denomination
|
2023-06-14 20:39:36 +02:00
|
|
|
*/
|
2023-12-12 03:46:51 +01:00
|
|
|
export let upstreamValue: Store<string>;
|
2023-06-11 01:32:30 +02:00
|
|
|
|
2023-12-12 03:46:51 +01:00
|
|
|
let isSingle: Store<boolean> = textValue.map((v) => Number(v) === 1);
|
2023-06-11 01:32:30 +02:00
|
|
|
|
2023-12-12 03:46:51 +01:00
|
|
|
export let selectedUnit: UIEventSource<string> = new UIEventSource<string>(undefined);
|
|
|
|
export let getCountry = () => "?";
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
console.log("Setting selected unit based on country", getCountry(), upstreamValue.data)
|
|
|
|
if(upstreamValue.data === undefined || upstreamValue.data === ""){
|
|
|
|
// Init the selected unit
|
|
|
|
let denomination: Denomination = unit.getDefaultDenomination(getCountry);
|
|
|
|
console.log("Found denom", denomination.canonical)
|
|
|
|
selectedUnit.setData(denomination.canonical)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-06-14 20:39:36 +02:00
|
|
|
onDestroy(
|
|
|
|
upstreamValue.addCallbackAndRun((v) => {
|
2023-12-12 03:46:51 +01:00
|
|
|
if(v === undefined || v === ""){
|
2023-06-11 01:32:30 +02:00
|
|
|
return
|
2023-06-14 20:39:36 +02:00
|
|
|
}
|
2023-12-12 03:46:51 +01:00
|
|
|
let denomination: Denomination = unit.getDefaultDenomination(getCountry);
|
|
|
|
const selected = unit.findDenomination(v, getCountry);
|
|
|
|
if(selected){
|
|
|
|
denomination = selected[1];
|
2023-06-14 20:39:36 +02:00
|
|
|
}
|
2023-12-12 03:46:51 +01:00
|
|
|
selectedUnit.setData(denomination.canonical);
|
2023-06-14 20:39:36 +02:00
|
|
|
})
|
2023-12-12 03:46:51 +01:00
|
|
|
);
|
2023-06-11 01:32:30 +02:00
|
|
|
|
2023-06-14 20:39:36 +02:00
|
|
|
onDestroy(
|
|
|
|
textValue.addCallbackAndRunD((v) => {
|
|
|
|
// Fallback in case that the user manually types a denomination
|
2023-12-12 03:46:51 +01:00
|
|
|
const [value, denomination] = unit.findDenomination(v, getCountry);
|
2023-06-14 20:39:36 +02:00
|
|
|
if (value === undefined || denomination === undefined) {
|
2023-12-12 03:46:51 +01:00
|
|
|
return;
|
2023-06-14 20:39:36 +02:00
|
|
|
}
|
2023-12-12 03:46:51 +01:00
|
|
|
if(value === v){
|
|
|
|
// The input value actually didn't have a denomination typed out - so lets ignore this one
|
|
|
|
// If a denomination is given, it is the default value anyway
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
textValue.setData(value);
|
|
|
|
selectedUnit.setData(denomination.canonical);
|
2023-06-14 20:39:36 +02:00
|
|
|
})
|
2023-12-12 03:46:51 +01:00
|
|
|
);
|
2023-06-11 01:32:30 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<select bind:value={$selectedUnit}>
|
2023-06-14 20:39:36 +02:00
|
|
|
{#each unit.denominations as denom (denom.canonical)}
|
|
|
|
<option value={denom.canonical}>
|
|
|
|
{#if $isSingle}
|
|
|
|
<Tr t={denom.humanSingular} />
|
|
|
|
{:else}
|
2023-12-12 03:46:51 +01:00
|
|
|
<Tr t={denom.human.Subs({quantity: ""})} />
|
2023-06-14 20:39:36 +02:00
|
|
|
{/if}
|
|
|
|
</option>
|
|
|
|
{/each}
|
2023-06-11 01:32:30 +02:00
|
|
|
</select>
|