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
|
|
|
*/
|
2020-08-17 17:23:15 +02:00
|
|
|
import {UIEventSource} from "./UIEventSource";
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
export class ElementStorage {
|
|
|
|
|
|
|
|
private _elements = [];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
addElementById(id: string, eventSource: UIEventSource<any>) {
|
|
|
|
this._elements[id] = eventSource;
|
|
|
|
}
|
|
|
|
|
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> {
|
|
|
|
const elementId = feature.properties.id;
|
2020-06-24 00:35:19 +02:00
|
|
|
if (elementId in this._elements) {
|
|
|
|
const es = this._elements[elementId];
|
2021-03-25 15:19:44 +01:00
|
|
|
if (es.data == feature.properties) {
|
|
|
|
// Reference comparison gives the same object! we can just return the event source
|
|
|
|
return es;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
const keptKeys = es.data;
|
|
|
|
// The element already exists
|
|
|
|
// We add all the new keys to the old keys
|
2021-03-25 15:19:44 +01:00
|
|
|
let somethingChanged = false;
|
|
|
|
for (const k in feature.properties) {
|
|
|
|
const v = feature.properties[k];
|
2020-06-24 00:35:19 +02:00
|
|
|
if (keptKeys[k] !== v) {
|
|
|
|
keptKeys[k] = v;
|
2021-03-25 15:19:44 +01:00
|
|
|
somethingChanged = true;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 15:19:44 +01:00
|
|
|
if (somethingChanged) {
|
|
|
|
es.ping();
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
return es;
|
2021-03-25 15:19:44 +01:00
|
|
|
} else {
|
|
|
|
const eventSource = new UIEventSource<any>(feature.properties, "tags of " + feature.properties.id);
|
|
|
|
this._elements[feature.properties.id] = eventSource;
|
|
|
|
return eventSource;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 03:22:17 +01:00
|
|
|
getEventSourceById(elementId): UIEventSource<any> {
|
2020-06-24 00:35:19 +02:00
|
|
|
if (elementId in this._elements) {
|
|
|
|
return this._elements[elementId];
|
|
|
|
}
|
2020-12-05 03:22:17 +01:00
|
|
|
console.error("Can not find eventsource with id ", elementId);
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|