| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  | import { Store, UIEventSource } from "../UIEventSource" | 
					
						
							| 
									
										
										
										
											2023-03-25 02:48:24 +01:00
										 |  |  | import FilteredLayer from "../../Models/FilteredLayer" | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | import TilesourceConfig from "../../Models/ThemeConfig/TilesourceConfig" | 
					
						
							| 
									
										
										
										
											2022-12-16 13:45:07 +01:00
										 |  |  | import { QueryParameters } from "../Web/QueryParameters" | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | import ShowOverlayLayer from "../../UI/ShowDataLayer/ShowOverlayLayer" | 
					
						
							| 
									
										
										
										
											2023-04-06 01:33:08 +02:00
										 |  |  | import { FeatureSource, FeatureSourceForLayer, Tiled } from "../FeatureSource/FeatureSource" | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  | import StaticFeatureSource, { | 
					
						
							|  |  |  |     TiledStaticFeatureSource, | 
					
						
							|  |  |  | } from "../FeatureSource/Sources/StaticFeatureSource" | 
					
						
							|  |  |  | import { Feature } from "geojson" | 
					
						
							|  |  |  | import { MapProperties } from "../../Models/MapProperties" | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Contains all the leaflet-map related state | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  | export default class MapState { | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Last location where a click was registered | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public readonly LastClickLocation: UIEventSource<{ | 
					
						
							|  |  |  |         lat: number | 
					
						
							|  |  |  |         lon: number | 
					
						
							|  |  |  |     }> = new UIEventSource<{ lat: number; lon: number }>(undefined) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 17:30:50 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * The bounds of the current map view | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public currentView: FeatureSourceForLayer & Tiled | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-09 13:58:41 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * A builtin layer which contains the selected element. | 
					
						
							|  |  |  |      * Loads 'selected_element.json' | 
					
						
							|  |  |  |      * This _might_ contain multiple points, e.g. every center of a multipolygon | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public selectedElementsLayer: FeatureSourceForLayer & Tiled | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Which overlays are shown | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public overlayToggles: { config: TilesourceConfig; isDisplayed: UIEventSource<boolean> }[] | 
					
						
							| 
									
										
										
										
											2021-11-12 18:39:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         this.availableBackgroundLayers = AvailableBaseLayers.AvailableLayersAt(this.locationControl) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-21 02:44:35 +01:00
										 |  |  |         let defaultLayer = AvailableBaseLayers.osmCarto | 
					
						
							|  |  |  |         const available = this.availableBackgroundLayers.data | 
					
						
							|  |  |  |         for (const layer of available) { | 
					
						
							|  |  |  |             if (this.backgroundLayerId.data === layer.id) { | 
					
						
							|  |  |  |                 defaultLayer = layer | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const self = this | 
					
						
							|  |  |  |         this.backgroundLayer = new UIEventSource<BaseLayer>(defaultLayer) | 
					
						
							|  |  |  |         this.backgroundLayer.addCallbackAndRunD((layer) => self.backgroundLayerId.setData(layer.id)) | 
					
						
							| 
									
										
										
										
											2022-01-08 22:11:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |         this.overlayToggles = | 
					
						
							|  |  |  |             this.layoutToUse?.tileLayerSources | 
					
						
							|  |  |  |                 ?.filter((c) => c.name !== undefined) | 
					
						
							|  |  |  |                 ?.map((c) => ({ | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |                     config: c, | 
					
						
							|  |  |  |                     isDisplayed: QueryParameters.GetBooleanQueryParameter( | 
					
						
							|  |  |  |                         "overlay-" + c.id, | 
					
						
							|  |  |  |                         c.defaultState, | 
					
						
							|  |  |  |                         "Wether or not the overlay " + c.id + " is shown" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     ), | 
					
						
							| 
									
										
										
										
											2022-07-25 16:55:44 +02:00
										 |  |  |                 })) ?? [] | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 14:52:11 +02:00
										 |  |  |         this.AddAllOverlaysToMap(this.leafletMap) | 
					
						
							| 
									
										
										
										
											2021-11-12 18:39:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 15:51:08 +01:00
										 |  |  |         this.initCurrentView() | 
					
						
							| 
									
										
										
										
											2022-12-09 13:58:41 +01:00
										 |  |  |         this.initSelectedElement() | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     public AddAllOverlaysToMap(leafletMap: UIEventSource<any>) { | 
					
						
							|  |  |  |         const initialized = new Set() | 
					
						
							|  |  |  |         for (const overlayToggle of this.overlayToggles) { | 
					
						
							|  |  |  |             new ShowOverlayLayer(overlayToggle.config, leafletMap, overlayToggle.isDisplayed) | 
					
						
							|  |  |  |             initialized.add(overlayToggle.config) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-22 14:13:41 +01:00
										 |  |  |         for (const tileLayerSource of this.layoutToUse?.tileLayerSources ?? []) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             if (initialized.has(tileLayerSource)) { | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             new ShowOverlayLayer(tileLayerSource, leafletMap) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-25 02:48:24 +01:00
										 |  |  |     private static initCurrentView(mapproperties: MapProperties): FeatureSource { | 
					
						
							| 
									
										
										
										
											2021-12-10 17:30:50 +01:00
										 |  |  |         let i = 0 | 
					
						
							| 
									
										
										
										
											2023-03-25 02:48:24 +01:00
										 |  |  |         const features: Store<Feature[]> = mapproperties.bounds.map((bounds) => { | 
					
						
							| 
									
										
										
										
											2023-03-23 01:42:47 +01:00
										 |  |  |             if (bounds === undefined) { | 
					
						
							|  |  |  |                 return [] | 
					
						
							| 
									
										
										
										
											2021-12-10 15:51:08 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-23 01:42:47 +01:00
										 |  |  |             i++ | 
					
						
							| 
									
										
										
										
											2023-03-25 02:48:24 +01:00
										 |  |  |             return [ | 
					
						
							|  |  |  |                 bounds.asGeoJson({ | 
					
						
							| 
									
										
										
										
											2023-03-23 01:42:47 +01:00
										 |  |  |                     id: "current_view-" + i, | 
					
						
							|  |  |  |                     current_view: "yes", | 
					
						
							| 
									
										
										
										
											2023-03-25 02:48:24 +01:00
										 |  |  |                     zoom: "" + mapproperties.zoom.data, | 
					
						
							|  |  |  |                 }), | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2023-03-23 01:42:47 +01:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-01-08 22:11:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-25 02:48:24 +01:00
										 |  |  |         return new StaticFeatureSource(features) | 
					
						
							| 
									
										
										
										
											2021-12-10 15:51:08 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-12 18:39:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-16 13:45:07 +01:00
										 |  |  |     private initSelectedElement() { | 
					
						
							| 
									
										
										
										
											2022-12-09 13:58:41 +01:00
										 |  |  |         const layerDef: FilteredLayer = this.filteredLayers.data.filter( | 
					
						
							|  |  |  |             (l) => l.layerDef.id === "selected_element" | 
					
						
							|  |  |  |         )[0] | 
					
						
							|  |  |  |         const empty = [] | 
					
						
							| 
									
										
										
										
											2022-12-16 13:45:07 +01:00
										 |  |  |         const store = this.selectedElement.map((feature) => { | 
					
						
							|  |  |  |             if (feature === undefined || feature === null) { | 
					
						
							| 
									
										
										
										
											2022-12-09 13:58:41 +01:00
										 |  |  |                 return empty | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-12-16 13:45:07 +01:00
										 |  |  |             return [ | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     feature: { | 
					
						
							|  |  |  |                         type: "Feature", | 
					
						
							|  |  |  |                         properties: { | 
					
						
							|  |  |  |                             selected: "yes", | 
					
						
							|  |  |  |                             id: "selected" + feature.properties.id, | 
					
						
							|  |  |  |                         }, | 
					
						
							|  |  |  |                         geometry: feature.geometry, | 
					
						
							| 
									
										
										
										
											2022-12-09 13:58:41 +01:00
										 |  |  |                     }, | 
					
						
							| 
									
										
										
										
											2022-12-16 13:45:07 +01:00
										 |  |  |                     freshness: new Date(), | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         this.selectedElementsLayer = new TiledStaticFeatureSource(store, layerDef) | 
					
						
							| 
									
										
										
										
											2022-12-09 13:58:41 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | } |