| 
									
										
										
										
											2023-06-16 02:36:11 +02:00
										 |  |  | <script lang="ts"> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   import { UIEventSource } from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  |   import type { ConfigMeta } from "./configMeta"; | 
					
						
							|  |  |  |   import TagRenderingEditable from "../Popup/TagRendering/TagRenderingEditable.svelte"; | 
					
						
							|  |  |  |   import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig"; | 
					
						
							|  |  |  |   import nmd from "nano-markdown"; | 
					
						
							|  |  |  |   import type { | 
					
						
							|  |  |  |     QuestionableTagRenderingConfigJson | 
					
						
							|  |  |  |   } from "../../Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"; | 
					
						
							|  |  |  |   import EditLayerState from "./EditLayerState"; | 
					
						
							|  |  |  |   import { onDestroy } from "svelte"; | 
					
						
							|  |  |  |   import type { JsonSchemaType } from "./jsonSchema"; | 
					
						
							|  |  |  |   import { ConfigMetaUtils } from "./configMeta.ts"; | 
					
						
							| 
									
										
										
										
											2023-06-16 02:36:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   export let state: EditLayerState; | 
					
						
							|  |  |  |   export let path: (string | number)[] = []; | 
					
						
							|  |  |  |   export let schema: ConfigMeta; | 
					
						
							| 
									
										
										
										
											2023-11-02 04:35:32 +01:00
										 |  |  |   export let startInEditModeIfUnset: boolean = false | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   let value = new UIEventSource<string | any>(undefined); | 
					
						
							| 
									
										
										
										
											2023-06-21 17:13:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   const isTranslation = schema.hints.typehint === "translation" || schema.hints.typehint === "rendered" || ConfigMetaUtils.isTranslation(schema); | 
					
						
							|  |  |  |   let type = schema.hints.typehint ?? "string"; | 
					
						
							| 
									
										
										
										
											2023-10-21 09:35:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-24 22:01:10 +02:00
										 |  |  |   let rendervalue = (schema.hints.inline ?? schema.path.join(".")) + (isTranslation ? " <b>{translated(value)}</b>": " <b>{value}</b>"); | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2023-10-22 01:30:05 +02:00
										 |  |  |   if(schema.type === "boolean"){ | 
					
						
							|  |  |  |     rendervalue = undefined | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-10-24 22:01:10 +02:00
										 |  |  |   if(schema.hints.typehint === "tag" || schema.hints.typehint === "simple_tag") { | 
					
						
							| 
									
										
										
										
											2023-10-22 01:30:05 +02:00
										 |  |  |     rendervalue = "{tags()}" | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2023-10-21 09:35:54 +02:00
										 |  |  |   let helperArgs = schema.hints.typehelper?.split(","); | 
					
						
							|  |  |  |   let inline = schema.hints.inline !== undefined; | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   if (isTranslation) { | 
					
						
							|  |  |  |     type = "translation"; | 
					
						
							| 
									
										
										
										
											2023-10-21 09:35:54 +02:00
										 |  |  |     if (schema.hints.inline) { | 
					
						
							|  |  |  |       const inlineValue = schema.hints.inline; | 
					
						
							|  |  |  |       rendervalue = inlineValue; | 
					
						
							|  |  |  |       inline = false; | 
					
						
							|  |  |  |       helperArgs = [inlineValue.substring(0, inlineValue.indexOf("{")), inlineValue.substring(inlineValue.indexOf("}") + 1)]; | 
					
						
							| 
									
										
										
										
											2023-06-16 02:36:11 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (type.endsWith("[]")) { | 
					
						
							|  |  |  |     type = type.substring(0, type.length - 2); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-06-16 02:36:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   const configJson: QuestionableTagRenderingConfigJson = { | 
					
						
							|  |  |  |     id: path.join("_"), | 
					
						
							|  |  |  |     render: rendervalue, | 
					
						
							|  |  |  |     question: schema.hints.question, | 
					
						
							|  |  |  |     questionHint: nmd(schema.description), | 
					
						
							|  |  |  |     freeform: schema.type === "boolean" ? undefined : { | 
					
						
							|  |  |  |       key: "value", | 
					
						
							|  |  |  |       type, | 
					
						
							|  |  |  |       inline, | 
					
						
							|  |  |  |       helperArgs | 
					
						
							| 
									
										
										
										
											2023-06-16 02:36:11 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2023-06-23 16:14:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   if (schema.hints.default) { | 
					
						
							|  |  |  |     configJson.mappings = [{ | 
					
						
							|  |  |  |       if: "value=", // We leave this blank | 
					
						
							| 
									
										
										
										
											2023-10-24 22:01:10 +02:00
										 |  |  |       then: path.at(-1) + " is not set. The default value <b>" + schema.hints.default + "</b> will be used. " + (schema.hints.ifunset ?? "") | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |     }]; | 
					
						
							|  |  |  |   } else if (!schema.required) { | 
					
						
							|  |  |  |     configJson.mappings = [{ | 
					
						
							|  |  |  |       if: "value=", | 
					
						
							| 
									
										
										
										
											2023-10-24 22:01:10 +02:00
										 |  |  |       then: path.at(-1) + " is not set. " + (schema.hints.ifunset ?? "") | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |     }]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function mightBeBoolean(type: undefined | JsonSchemaType): boolean { | 
					
						
							|  |  |  |     if (type === undefined) { | 
					
						
							|  |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2023-08-23 11:11:53 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |     if (type["type"]) { | 
					
						
							|  |  |  |       type = type["type"]; | 
					
						
							| 
									
										
										
										
											2023-06-23 16:14:43 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |     if (type === "boolean") { | 
					
						
							|  |  |  |       return true; | 
					
						
							| 
									
										
										
										
											2023-06-23 16:14:43 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |     if (!Array.isArray(type)) { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return type.some(t => mightBeBoolean(t)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (mightBeBoolean(schema.type)) { | 
					
						
							|  |  |  |     configJson.mappings = configJson.mappings ?? []; | 
					
						
							|  |  |  |     configJson.mappings.push( | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         if: "value=true", | 
					
						
							|  |  |  |         then: schema.hints?.iftrue ?? "Yes" | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         if: "value=false", | 
					
						
							|  |  |  |         then: schema.hints?.iffalse ?? "No" | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (schema.hints.suggestions) { | 
					
						
							|  |  |  |     if (!configJson.mappings) { | 
					
						
							|  |  |  |       configJson.mappings = []; | 
					
						
							| 
									
										
										
										
											2023-06-23 16:14:43 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |     configJson.mappings.push(...schema.hints.suggestions); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   let config: TagRenderingConfig; | 
					
						
							|  |  |  |   let err: string = undefined; | 
					
						
							| 
									
										
										
										
											2023-10-24 22:01:10 +02:00
										 |  |  |   let messages = state.messagesFor(path) | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   try { | 
					
						
							|  |  |  |     config = new TagRenderingConfig(configJson, "config based on " + schema.path.join(".")); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     console.error(e, config); | 
					
						
							|  |  |  |     err = path.join(".") + " " + e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   let startValue = state.getCurrentValueFor(path); | 
					
						
							|  |  |  |   const tags = new UIEventSource<Record<string, string>>({ value: startValue }); | 
					
						
							| 
									
										
										
										
											2023-11-02 04:35:32 +01:00
										 |  |  |   let startInEditMode = !startValue && startInEditModeIfUnset | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   try { | 
					
						
							|  |  |  |     onDestroy(state.register(path, tags.map(tgs => { | 
					
						
							|  |  |  |       const v = tgs["value"]; | 
					
						
							|  |  |  |       if (typeof v !== "string") { | 
					
						
							| 
									
										
										
										
											2023-10-24 22:01:10 +02:00
										 |  |  |         return { ... v }; | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |       if (schema.type === "boolan") { | 
					
						
							|  |  |  |         return v === "true" || v === "yes" || v === "1"; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (mightBeBoolean(schema.type)) { | 
					
						
							|  |  |  |         if (v === "true" || v === "yes" || v === "1") { | 
					
						
							|  |  |  |           return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (v === "false" || v === "no" || v === "0") { | 
					
						
							|  |  |  |           return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (schema.type === "number") { | 
					
						
							|  |  |  |         if (v === "") { | 
					
						
							|  |  |  |           return undefined; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return Number(v); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (isTranslation && typeof v === "string") { | 
					
						
							|  |  |  |         if (v === "") { | 
					
						
							|  |  |  |           return {}; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return JSON.parse(v); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return v; | 
					
						
							|  |  |  |     }), isTranslation)); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     console.error("Could not register", path, "due to", e); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-06-16 02:36:11 +02:00
										 |  |  | </script> | 
					
						
							|  |  |  | {#if err !== undefined} | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   <span class="alert">{err}</span> | 
					
						
							| 
									
										
										
										
											2023-06-16 02:36:11 +02:00
										 |  |  | {:else} | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   <div class="w-full flex flex-col"> | 
					
						
							| 
									
										
										
										
											2023-11-02 04:35:32 +01:00
										 |  |  |     <TagRenderingEditable editMode={startInEditMode} {config} selectedElement={undefined} showQuestionIfUnknown={true} {state} {tags} /> | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |     {#if $messages.length > 0} | 
					
						
							|  |  |  |       {#each $messages as msg} | 
					
						
							|  |  |  |         <div class="alert">{msg.message}</div> | 
					
						
							|  |  |  |       {/each} | 
					
						
							|  |  |  |     {/if} | 
					
						
							| 
									
										
										
										
											2023-10-21 09:35:54 +02:00
										 |  |  |     {#if window.location.hostname === "127.0.0.1"} | 
					
						
							| 
									
										
										
										
											2023-10-24 22:01:10 +02:00
										 |  |  |       <span class="subtle">{path.join(".")} {schema.hints.typehint}</span> | 
					
						
							| 
									
										
										
										
											2023-10-21 09:35:54 +02:00
										 |  |  |     {/if} | 
					
						
							| 
									
										
										
										
											2023-10-20 19:04:55 +02:00
										 |  |  |   </div> | 
					
						
							| 
									
										
										
										
											2023-06-16 02:36:11 +02:00
										 |  |  | {/if} |