| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-01-02 21:03:40 +01:00
										 |  |  |  * Keeps track of a dictionary 'elementID' -> UIEventSource<tags> | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { UIEventSource } from "./UIEventSource" | 
					
						
							|  |  |  | import { GeoJSONObject } from "@turf/turf" | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class ElementStorage { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public ContainingFeatures = new Map<string, any>() | 
					
						
							|  |  |  |     private _elements = new Map<string, UIEventSource<any>>() | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor() {} | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     addElementById(id: string, eventSource: UIEventSource<any>) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this._elements.set(id, eventSource) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 15:19:44 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Creates a UIEventSource for the tags of the given feature. | 
					
						
							|  |  |  |      * If an UIEventsource has been created previously, the same UIEventSource will be returned | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Note: it will cleverly merge the tags, if needed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     addOrGetElement(feature: any): UIEventSource<any> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const elementId = feature.properties.id | 
					
						
							|  |  |  |         const newProperties = feature.properties | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |         const es = this.addOrGetById(elementId, newProperties) | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |         // At last, we overwrite the tag of the new feature to use the tags in the already existing event source
 | 
					
						
							|  |  |  |         feature.properties = es.data | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!this.ContainingFeatures.has(elementId)) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             this.ContainingFeatures.set(elementId, feature) | 
					
						
							| 
									
										
										
										
											2021-05-16 15:34:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return es | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |     getEventSourceById(elementId): UIEventSource<any> { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (elementId === undefined) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return this._elements.get(elementId) | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     has(id) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return this._elements.has(id) | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     addAlias(oldId: string, newId: string) { | 
					
						
							| 
									
										
										
										
											2022-04-08 04:18:53 +02:00
										 |  |  |         if (newId === undefined) { | 
					
						
							|  |  |  |             // We removed the node/way/relation with type 'type' and id 'oldId' on openstreetmap!
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const element = this.getEventSourceById(oldId) | 
					
						
							| 
									
										
										
										
											2022-04-08 04:18:53 +02:00
										 |  |  |             element.data._deleted = "yes" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             element.ping() | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2022-04-08 04:18:53 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-08 04:18:53 +02:00
										 |  |  |         if (oldId == newId) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return undefined | 
					
						
							| 
									
										
										
										
											2022-04-08 04:18:53 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const element = this.getEventSourceById(oldId) | 
					
						
							| 
									
										
										
										
											2022-04-08 04:18:53 +02:00
										 |  |  |         if (element === undefined) { | 
					
						
							|  |  |  |             // Element to rewrite not found, probably a node or relation that is not rendered
 | 
					
						
							|  |  |  |             return undefined | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         element.data.id = newId | 
					
						
							|  |  |  |         this.addElementById(newId, element) | 
					
						
							|  |  |  |         this.ContainingFeatures.set(newId, this.ContainingFeatures.get(oldId)) | 
					
						
							|  |  |  |         element.ping() | 
					
						
							| 
									
										
										
										
											2022-04-08 04:18:53 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |     private addOrGetById(elementId: string, newProperties: any): UIEventSource<any> { | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |         if (!this._elements.has(elementId)) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const eventSource = new UIEventSource<any>(newProperties, "tags of " + elementId) | 
					
						
							|  |  |  |             this._elements.set(elementId, eventSource) | 
					
						
							|  |  |  |             return eventSource | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const es = this._elements.get(elementId) | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |         if (es.data == newProperties) { | 
					
						
							|  |  |  |             // Reference comparison gives the same object! we can just return the event source
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return es | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const keptKeys = es.data | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |         // The element already exists
 | 
					
						
							|  |  |  |         // We use the new feature to overwrite all the properties in the already existing eventsource
 | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |         const debug_msg = [] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         let somethingChanged = false | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |         for (const k in newProperties) { | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |             if (!newProperties.hasOwnProperty(k)) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2021-05-17 00:17:21 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const v = newProperties[k] | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |             if (keptKeys[k] !== v) { | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |                 if (v === undefined) { | 
					
						
							|  |  |  |                     // The new value is undefined; the tag might have been removed
 | 
					
						
							|  |  |  |                     // It might be a metatag as well
 | 
					
						
							|  |  |  |                     // In the latter case, we do keep the tag!
 | 
					
						
							|  |  |  |                     if (!k.startsWith("_")) { | 
					
						
							|  |  |  |                         delete keptKeys[k] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         debug_msg.push("Erased " + k) | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     keptKeys[k] = v | 
					
						
							| 
									
										
										
										
											2021-05-10 23:51:03 +02:00
										 |  |  |                     debug_msg.push(k + " --> " + v) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 somethingChanged = true | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (somethingChanged) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             es.ping() | 
					
						
							| 
									
										
										
										
											2021-04-21 01:26:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return es | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |