| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { FlowStep } from "./FlowStep" | 
					
						
							|  |  |  | import Combine from "../Base/Combine" | 
					
						
							|  |  |  | import { Store } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  | import LayerConfig from "../../Models/ThemeConfig/LayerConfig" | 
					
						
							|  |  |  | import { InputElement } from "../Input/InputElement" | 
					
						
							|  |  |  | import { AllKnownLayouts } from "../../Customizations/AllKnownLayouts" | 
					
						
							|  |  |  | import { FixedInputElement } from "../Input/FixedInputElement" | 
					
						
							|  |  |  | import Img from "../Base/Img" | 
					
						
							|  |  |  | import Title from "../Base/Title" | 
					
						
							|  |  |  | import { RadioButton } from "../Input/RadioButton" | 
					
						
							|  |  |  | import { And } from "../../Logic/Tags/And" | 
					
						
							|  |  |  | import { VariableUiElement } from "../Base/VariableUIElement" | 
					
						
							|  |  |  | import Toggleable from "../Base/Toggleable" | 
					
						
							|  |  |  | import { BBox } from "../../Logic/BBox" | 
					
						
							|  |  |  | import BaseUIElement from "../BaseUIElement" | 
					
						
							|  |  |  | import PresetConfig from "../../Models/ThemeConfig/PresetConfig" | 
					
						
							|  |  |  | import List from "../Base/List" | 
					
						
							|  |  |  | import Translations from "../i18n/Translations" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class SelectTheme | 
					
						
							|  |  |  |     extends Combine | 
					
						
							|  |  |  |     implements | 
					
						
							|  |  |  |         FlowStep<{ | 
					
						
							|  |  |  |             features: any[] | 
					
						
							|  |  |  |             theme: string | 
					
						
							|  |  |  |             layer: LayerConfig | 
					
						
							|  |  |  |             bbox: BBox | 
					
						
							|  |  |  |         }> | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |     public readonly Value: Store<{ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         features: any[] | 
					
						
							|  |  |  |         theme: string | 
					
						
							|  |  |  |         layer: LayerConfig | 
					
						
							|  |  |  |         bbox: BBox | 
					
						
							|  |  |  |     }> | 
					
						
							|  |  |  |     public readonly IsValid: Store<boolean> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(params: { features: any[]; layer: LayerConfig; bbox: BBox }) { | 
					
						
							| 
									
										
										
										
											2022-04-14 01:32:04 +02:00
										 |  |  |         const t = Translations.t.importHelper.selectTheme | 
					
						
							| 
									
										
										
										
											2023-03-02 05:20:53 +01:00
										 |  |  |         let options: InputElement<string>[] = Array.from(AllKnownLayouts.allKnownLayouts.values()) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             .filter((th) => th.layers.some((l) => l.id === params.layer.id)) | 
					
						
							|  |  |  |             .filter((th) => th.id !== "personal") | 
					
						
							|  |  |  |             .map( | 
					
						
							|  |  |  |                 (th) => | 
					
						
							|  |  |  |                     new FixedInputElement<string>( | 
					
						
							|  |  |  |                         new Combine([ | 
					
						
							|  |  |  |                             new Img(th.icon).SetClass("block h-12 w-12 br-4"), | 
					
						
							|  |  |  |                             new Title(th.title), | 
					
						
							|  |  |  |                         ]).SetClass("flex items-center"), | 
					
						
							|  |  |  |                         th.id | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const themeRadios = new RadioButton<string>(options, { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             selectFirstAsDefault: false, | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const applicablePresets = themeRadios.GetValue().map((theme) => { | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |             if (theme === undefined) { | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |                 return [] | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // we get the layer with the correct ID via the actual theme config, as the actual theme might have different presets due to overrides
 | 
					
						
							| 
									
										
										
										
											2023-03-02 05:20:53 +01:00
										 |  |  |             const themeConfig = AllKnownLayouts.allKnownLayouts.get(theme) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const layer = themeConfig.layers.find((l) => l.id === params.layer.id) | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |             return layer.presets | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const nonMatchedElements = applicablePresets.map((presets) => { | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |             if (presets === undefined || presets.length === 0) { | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |                 return undefined | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return params.features.filter( | 
					
						
							|  |  |  |                 (feat) => | 
					
						
							|  |  |  |                     !presets.some((preset) => | 
					
						
							|  |  |  |                         new And(preset.tags).matchesProperties(feat.properties) | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super([ | 
					
						
							| 
									
										
										
										
											2022-04-14 01:32:04 +02:00
										 |  |  |             new Title(t.title), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             t.intro, | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |             themeRadios, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             new VariableUiElement( | 
					
						
							|  |  |  |                 applicablePresets.map( | 
					
						
							|  |  |  |                     (applicablePresets) => { | 
					
						
							|  |  |  |                         if (themeRadios.GetValue().data === undefined) { | 
					
						
							|  |  |  |                             return undefined | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         if (applicablePresets === undefined || applicablePresets.length === 0) { | 
					
						
							|  |  |  |                             return t.noMatchingPresets.SetClass("alert") | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     [themeRadios.GetValue()] | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             new VariableUiElement( | 
					
						
							|  |  |  |                 nonMatchedElements.map( | 
					
						
							|  |  |  |                     (unmatched) => | 
					
						
							|  |  |  |                         SelectTheme.nonMatchedElementsPanel(unmatched, applicablePresets.data), | 
					
						
							|  |  |  |                     [applicablePresets] | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ]) | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |         this.SetClass("flex flex-col") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.Value = themeRadios.GetValue().map((theme) => ({ | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |             features: params.features, | 
					
						
							|  |  |  |             layer: params.layer, | 
					
						
							|  |  |  |             bbox: params.bbox, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             theme, | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |         })) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.IsValid = this.Value.map( | 
					
						
							|  |  |  |             (obj) => { | 
					
						
							|  |  |  |                 if (obj === undefined) { | 
					
						
							|  |  |  |                     return false | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if ([obj.theme, obj.features].some((v) => v === undefined)) { | 
					
						
							|  |  |  |                     return false | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (applicablePresets.data === undefined || applicablePresets.data.length === 0) { | 
					
						
							|  |  |  |                     return false | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if ((nonMatchedElements.data?.length ?? 0) > 0) { | 
					
						
							|  |  |  |                     return false | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 return true | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             [applicablePresets] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-03-24 03:11:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private static nonMatchedElementsPanel( | 
					
						
							|  |  |  |         unmatched: any[], | 
					
						
							|  |  |  |         applicablePresets: PresetConfig[] | 
					
						
							|  |  |  |     ): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |         if (unmatched === undefined || unmatched.length === 0) { | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const t = Translations.t.importHelper.selectTheme | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const applicablePresetsOverview = applicablePresets.map((preset) => | 
					
						
							|  |  |  |             t.needsTags | 
					
						
							|  |  |  |                 .Subs({ | 
					
						
							|  |  |  |                     title: preset.title, | 
					
						
							|  |  |  |                     tags: preset.tags.map((t) => t.asHumanString()).join(" & "), | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2022-04-14 01:32:04 +02:00
										 |  |  |                 .SetClass("thanks") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const unmatchedPanels: BaseUIElement[] = [] | 
					
						
							|  |  |  |         for (const feat of unmatched) { | 
					
						
							|  |  |  |             const parts: BaseUIElement[] = [] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             parts.push( | 
					
						
							|  |  |  |                 new Combine( | 
					
						
							|  |  |  |                     Object.keys(feat.properties).map((k) => k + "=" + feat.properties[k]) | 
					
						
							|  |  |  |                 ).SetClass("flex flex-col") | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             for (const preset of applicablePresets) { | 
					
						
							|  |  |  |                 const tags = new And(preset.tags).asChange({}) | 
					
						
							|  |  |  |                 const missing = [] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 for (const { k, v } of tags) { | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |                     if (preset[k] === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         missing.push(t.missing.Subs({ k, v })) | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |                     } else if (feat.properties[k] !== v) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         missing.push(t.misMatch.Subs({ k, v, properties: feat.properties })) | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (missing.length > 0) { | 
					
						
							|  |  |  |                     parts.push( | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         new Combine([t.notApplicable.Subs(preset), new List(missing)]).SetClass( | 
					
						
							|  |  |  |                             "flex flex-col alert" | 
					
						
							|  |  |  |                         ) | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |                     ) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             unmatchedPanels.push(new Combine(parts).SetClass("flex flex-col")) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return new Combine([ | 
					
						
							| 
									
										
										
										
											2022-04-14 01:32:04 +02:00
										 |  |  |             t.displayNonMatchingCount.Subs(unmatched).SetClass("alert"), | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |             ...applicablePresetsOverview, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             new Toggleable(new Title(t.unmatchedTitle), new Combine(unmatchedPanels)), | 
					
						
							| 
									
										
										
										
											2022-04-04 04:54:54 +02:00
										 |  |  |         ]).SetClass("flex flex-col") | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |