| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import TagRenderingConfig from "./TagRenderingConfig" | 
					
						
							|  |  |  | import SharedTagRenderings from "../../Customizations/SharedTagRenderings" | 
					
						
							|  |  |  | import { TagRenderingConfigJson } from "./Json/TagRenderingConfigJson" | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class WithContextLoader { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     protected readonly _context: string | 
					
						
							|  |  |  |     private readonly _json: any | 
					
						
							| 
									
										
										
										
											2021-10-22 18:53:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |     constructor(json: any, context: string) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this._json = json | 
					
						
							|  |  |  |         this._context = context | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Given a key, gets the corresponding property from the json (or the default if not found | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * The found value is interpreted as a tagrendering and fetched/parsed | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public tr(key: string, deflt) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const v = this._json[key] | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |         if (v === undefined || v === null) { | 
					
						
							|  |  |  |             if (deflt === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 return undefined | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return new TagRenderingConfig(deflt, `${this._context}.${key}.default value`) | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (typeof v === "string") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const shared = SharedTagRenderings.SharedTagRendering.get(v) | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |             if (shared) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 return shared | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return new TagRenderingConfig(v, `${this._context}.${key}`) | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Converts a list of tagRenderingCOnfigJSON in to TagRenderingConfig | 
					
						
							|  |  |  |      * A string is interpreted as a name to call | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-22 18:53:07 +02:00
										 |  |  |     public ParseTagRenderings( | 
					
						
							| 
									
										
										
										
											2021-12-21 18:35:31 +01:00
										 |  |  |         tagRenderings: TagRenderingConfigJson[], | 
					
						
							|  |  |  |         options?: { | 
					
						
							| 
									
										
										
										
											2021-11-11 17:14:03 +01:00
										 |  |  |             /** | 
					
						
							|  |  |  |              * Throw an error if 'question' is defined | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             readOnlyMode?: boolean | 
					
						
							| 
									
										
										
										
											2021-11-11 17:14:03 +01:00
										 |  |  |             requiresId?: boolean | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             prepConfig?: (config: TagRenderingConfigJson) => TagRenderingConfigJson | 
					
						
							| 
									
										
										
										
											2021-11-11 17:14:03 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     ): TagRenderingConfig[] { | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |         if (tagRenderings === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return [] | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-22 18:53:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |         const context = this._context | 
					
						
							| 
									
										
										
										
											2021-11-11 17:14:03 +01:00
										 |  |  |         options = options ?? {} | 
					
						
							|  |  |  |         if (options.prepConfig === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             options.prepConfig = (c) => c | 
					
						
							| 
									
										
										
										
											2021-10-22 18:53:07 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-14 18:01:48 +01:00
										 |  |  |         const renderings: TagRenderingConfig[] = [] | 
					
						
							| 
									
										
										
										
											2021-12-21 18:35:31 +01:00
										 |  |  |         for (let i = 0; i < tagRenderings.length; i++) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const preparedConfig = tagRenderings[i] | 
					
						
							|  |  |  |             const tr = new TagRenderingConfig(preparedConfig, `${context}.tagrendering[${i}]`) | 
					
						
							| 
									
										
										
										
											2021-12-21 18:35:31 +01:00
										 |  |  |             if (options.readOnlyMode && tr.question !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 throw ( | 
					
						
							|  |  |  |                     "A question is defined for " + | 
					
						
							|  |  |  |                     `${context}.tagrendering[${i}], but this is not allowed at this position - probably because this rendering is an icon, badge or label` | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2021-11-11 17:14:03 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-21 18:35:31 +01:00
										 |  |  |             if (options.requiresId && tr.id === "") { | 
					
						
							| 
									
										
										
										
											2021-11-11 17:14:03 +01:00
										 |  |  |                 throw `${context}.tagrendering[${i}] has an invalid ID - make sure it is defined and not empty` | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-21 18:35:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |             renderings.push(tr) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return renderings | 
					
						
							| 
									
										
										
										
											2021-10-19 02:31:32 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |