| 
									
										
										
										
											2023-06-11 01:32:30 +02:00
										 |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   import { Unit } from "../../Models/Unit" | 
					
						
							|  |  |  |   import { Store, UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  |   import Tr from "../Base/Tr.svelte" | 
					
						
							|  |  |  |   import { onDestroy } from "svelte" | 
					
						
							| 
									
										
										
										
											2023-06-11 01:32:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02: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 | 
					
						
							|  |  |  |    * Not necessarily a correct number | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   export let textValue: UIEventSource<string> | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * The actual _valid_ value that is upstreamed | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   export let upstreamValue: Store<string> | 
					
						
							| 
									
										
										
										
											2023-06-11 01:32:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   let isSingle: Store<boolean> = textValue.map((v) => Number(v) === 1) | 
					
						
							| 
									
										
										
										
											2023-06-11 01:32:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |   export let selectedUnit: UIEventSource<string> = new UIEventSource<string>(undefined) | 
					
						
							|  |  |  |   export let getCountry = () => "be" | 
					
						
							|  |  |  |   console.log("Unit", unit) | 
					
						
							|  |  |  |   onDestroy( | 
					
						
							|  |  |  |     upstreamValue.addCallbackAndRun((v) => { | 
					
						
							|  |  |  |       if (v === undefined) { | 
					
						
							|  |  |  |         if (!selectedUnit.data) { | 
					
						
							|  |  |  |           selectedUnit.setData(unit.getDefaultDenomination(getCountry).canonical) | 
					
						
							| 
									
										
										
										
											2023-06-11 01:32:30 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2023-06-14 20:39:36 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |       const selected = unit.findDenomination(v, getCountry) | 
					
						
							|  |  |  |       if (selected === undefined) { | 
					
						
							|  |  |  |         selectedUnit.setData(unit.getDefaultDenomination(getCountry).canonical) | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const [value, denomination] = selected | 
					
						
							|  |  |  |       selectedUnit.setData(denomination.canonical) | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |       const [value, denomination] = unit.findDenomination(v, getCountry) | 
					
						
							|  |  |  |       if (value === undefined || denomination === undefined) { | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       textValue.setData(value) | 
					
						
							|  |  |  |       selectedUnit.setData(denomination.canonical) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ) | 
					
						
							| 
									
										
										
										
											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} | 
					
						
							|  |  |  |         <Tr t={denom.human} /> | 
					
						
							|  |  |  |       {/if} | 
					
						
							|  |  |  |     </option> | 
					
						
							|  |  |  |   {/each} | 
					
						
							| 
									
										
										
										
											2023-06-11 01:32:30 +02:00
										 |  |  | </select> |