| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | <script lang="ts"> | 
					
						
							|  |  |  |   import FilteredLayer from "../../Models/FilteredLayer"; | 
					
						
							|  |  |  |   import type { FilterConfigOption } from "../../Models/ThemeConfig/FilterConfig"; | 
					
						
							|  |  |  |   import Locale from "../i18n/Locale"; | 
					
						
							|  |  |  |   import ValidatedInput from "../InputElement/ValidatedInput.svelte"; | 
					
						
							|  |  |  |   import { UIEventSource } from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  |   import { onDestroy } from "svelte"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   export let filteredLayer: FilteredLayer; | 
					
						
							|  |  |  |   export let option: FilterConfigOption; | 
					
						
							|  |  |  |   export let id: string; | 
					
						
							|  |  |  |   let parts: string[]; | 
					
						
							|  |  |  |   let language = Locale.language; | 
					
						
							|  |  |  |   $: { | 
					
						
							|  |  |  |     parts = option.question.textFor($language).split("{"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   let fieldValues: Record<string, UIEventSource<string>> = {}; | 
					
						
							|  |  |  |   let fieldTypes: Record<string, string> = {}; | 
					
						
							|  |  |  |   let appliedFilter = <UIEventSource<string>>filteredLayer.appliedFilters.get(id); | 
					
						
							| 
									
										
										
										
											2023-04-24 03:22:43 +02:00
										 |  |  |   let initialState: Record<string, string> = JSON.parse(appliedFilter?.data ?? "{}"); | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function setFields() { | 
					
						
							|  |  |  |     const properties: Record<string, string> = {}; | 
					
						
							|  |  |  |     for (const key in fieldValues) { | 
					
						
							|  |  |  |       const v = fieldValues[key].data; | 
					
						
							|  |  |  |       const k = key.substring(0, key.length - 1); | 
					
						
							|  |  |  |       if (v === undefined) { | 
					
						
							|  |  |  |         properties[k] = undefined; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         properties[k] = v; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-24 03:22:43 +02:00
										 |  |  |     appliedFilter?.setData(FilteredLayer.fieldsToString(properties)); | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (const field of option.fields) { | 
					
						
							|  |  |  |     // A bit of cheating: the 'parts' will have '}' suffixed for fields | 
					
						
							|  |  |  |     fieldTypes[field.name + "}"] = field.type; | 
					
						
							|  |  |  |     const src = new UIEventSource<string>(initialState[field.name] ?? ""); | 
					
						
							|  |  |  |     fieldValues[field.name + "}"] = src; | 
					
						
							| 
									
										
										
										
											2023-04-24 03:22:43 +02:00
										 |  |  |     onDestroy(src.stabilized(200).addCallback(() => { | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  |       setFields(); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div> | 
					
						
							|  |  |  |   {#each parts as part, i} | 
					
						
							|  |  |  |     {#if part.endsWith("}")} | 
					
						
							|  |  |  |       <!-- This is a field! --> | 
					
						
							|  |  |  |       <ValidatedInput value={fieldValues[part]} type={fieldTypes[part]} /> | 
					
						
							|  |  |  |     {:else} | 
					
						
							|  |  |  |       {part} | 
					
						
							|  |  |  |     {/if} | 
					
						
							|  |  |  |   {/each} | 
					
						
							|  |  |  | </div> |