| 
									
										
										
										
											2024-02-03 14:33:10 +01:00
										 |  |  | import { ImmutableStore, Store, UIEventSource } from "../UIEventSource" | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  | import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig" | 
					
						
							|  |  |  | import { LocalStorageSource } from "../Web/LocalStorageSource" | 
					
						
							|  |  |  | import { QueryParameters } from "../Web/QueryParameters" | 
					
						
							| 
									
										
										
										
											2024-04-30 17:20:08 +02:00
										 |  |  | import Hash from "../Web/Hash" | 
					
						
							|  |  |  | import OsmObjectDownloader from "../Osm/OsmObjectDownloader" | 
					
						
							|  |  |  | import { OsmObject } from "../Osm/OsmObject" | 
					
						
							|  |  |  | import Constants from "../../Models/Constants" | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * This actor is responsible to set the map location. | 
					
						
							|  |  |  |  * It will attempt to | 
					
						
							| 
									
										
										
										
											2024-04-30 17:20:08 +02:00
										 |  |  |  * - Set the map to the position of the selected element | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  |  * - Set the map to the position as passed in by the query parameters (if available) | 
					
						
							|  |  |  |  * - Set the map to the position remembered in LocalStorage (if available) | 
					
						
							|  |  |  |  * - Set the map to the layout default | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Additionally, it will save the map location to local storage | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export default class InitialMapPositioning { | 
					
						
							|  |  |  |     public zoom: UIEventSource<number> | 
					
						
							|  |  |  |     public location: UIEventSource<{ lon: number; lat: number }> | 
					
						
							| 
									
										
										
										
											2024-02-03 14:33:10 +01:00
										 |  |  |     public useTerrain: Store<boolean> | 
					
						
							| 
									
										
										
										
											2024-04-30 18:56:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 23:25:30 +02:00
										 |  |  |     constructor(layoutToUse: LayoutConfig) { | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  |         function localStorageSynced( | 
					
						
							|  |  |  |             key: string, | 
					
						
							|  |  |  |             deflt: number, | 
					
						
							|  |  |  |             docs: string | 
					
						
							|  |  |  |         ): UIEventSource<number> { | 
					
						
							|  |  |  |             const localStorage = LocalStorageSource.Get(key) | 
					
						
							|  |  |  |             const previousValue = localStorage.data | 
					
						
							|  |  |  |             const src = UIEventSource.asFloat( | 
					
						
							|  |  |  |                 QueryParameters.GetQueryParameter(key, "" + deflt, docs).syncWith(localStorage) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (src.data === deflt) { | 
					
						
							|  |  |  |                 const prev = Number(previousValue) | 
					
						
							|  |  |  |                 if (!isNaN(prev)) { | 
					
						
							|  |  |  |                     src.setData(prev) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return src | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 17:20:08 +02:00
										 |  |  |         const initialHash = Hash.hash.data | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  |         // -- Location control initialization
 | 
					
						
							|  |  |  |         this.zoom = localStorageSynced( | 
					
						
							|  |  |  |             "z", | 
					
						
							|  |  |  |             layoutToUse?.startZoom ?? 1, | 
					
						
							|  |  |  |             "The initial/current zoom level" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         const lat = localStorageSynced( | 
					
						
							|  |  |  |             "lat", | 
					
						
							|  |  |  |             layoutToUse?.startLat ?? 0, | 
					
						
							|  |  |  |             "The initial/current latitude" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         const lon = localStorageSynced( | 
					
						
							|  |  |  |             "lon", | 
					
						
							|  |  |  |             layoutToUse?.startLon ?? 0, | 
					
						
							|  |  |  |             "The initial/current longitude of the app" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.location = new UIEventSource({ lon: lon.data, lat: lat.data }) | 
					
						
							| 
									
										
										
										
											2024-02-03 14:33:10 +01:00
										 |  |  |         // Note: this syncs only in one direction
 | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  |         this.location.addCallbackD((loc) => { | 
					
						
							|  |  |  |             lat.setData(loc.lat) | 
					
						
							|  |  |  |             lon.setData(loc.lon) | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2024-02-03 14:33:10 +01:00
										 |  |  |         this.useTerrain = new ImmutableStore<boolean>(layoutToUse.enableTerrain) | 
					
						
							| 
									
										
										
										
											2024-04-30 17:20:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 18:56:50 +02:00
										 |  |  |         if (initialHash?.match(/^(node|way|relation)\/[0-9]+$/)) { | 
					
						
							| 
									
										
										
										
											2024-04-30 17:20:08 +02:00
										 |  |  |             const [type, id] = initialHash.split("/") | 
					
						
							| 
									
										
										
										
											2024-04-30 18:56:50 +02:00
										 |  |  |             OsmObjectDownloader.RawDownloadObjectAsync(type, Number(id), Constants.osmAuthConfig.url + "/").then(osmObject => { | 
					
						
							|  |  |  |                 if (osmObject === "deleted") { | 
					
						
							| 
									
										
										
										
											2024-04-30 17:20:08 +02:00
										 |  |  |                     return | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-04-30 18:56:50 +02:00
										 |  |  |                 const targetLayer = layoutToUse.getMatchingLayer(osmObject.tags) | 
					
						
							|  |  |  |                 this.zoom.setData(Math.max(this.zoom.data, targetLayer.minzoom)) | 
					
						
							| 
									
										
										
										
											2024-04-30 17:20:08 +02:00
										 |  |  |                 const [lat, lon] = osmObject.centerpoint() | 
					
						
							| 
									
										
										
										
											2024-04-30 18:56:50 +02:00
										 |  |  |                 this.location.setData({ lon, lat }) | 
					
						
							| 
									
										
										
										
											2024-04-30 17:20:08 +02:00
										 |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 19:21:15 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } |