forked from MapComplete/MapComplete
Add MapComplete
This commit is contained in:
commit
6187122294
61 changed files with 107059 additions and 0 deletions
56
Logic/ElementStorage.ts
Normal file
56
Logic/ElementStorage.ts
Normal file
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
* Keeps track of a dictionary 'elementID' -> element
|
||||
*/
|
||||
import {UIEventSource} from "../UI/UIEventSource";
|
||||
|
||||
export class ElementStorage {
|
||||
|
||||
private _elements = [];
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
addElementById(id: string, eventSource: UIEventSource<any>) {
|
||||
this._elements[id] = eventSource;
|
||||
}
|
||||
|
||||
addElement(element): UIEventSource<any> {
|
||||
const eventSource = new UIEventSource<any>(element.properties);
|
||||
this._elements[element.properties.id] = eventSource;
|
||||
return eventSource;
|
||||
}
|
||||
|
||||
addOrGetElement(element: any) {
|
||||
const elementId = element.properties.id;
|
||||
if (elementId in this._elements) {
|
||||
const es = this._elements[elementId];
|
||||
const keptKeys = es.data;
|
||||
// The element already exists
|
||||
// We add all the new keys to the old keys
|
||||
for (const k in element.properties) {
|
||||
const v = element.properties[k];
|
||||
if (keptKeys[k] !== v) {
|
||||
keptKeys[k] = v;
|
||||
es.ping();
|
||||
}
|
||||
}
|
||||
|
||||
return es;
|
||||
}else{
|
||||
return this.addElement(element);
|
||||
}
|
||||
}
|
||||
|
||||
getElement(elementId): UIEventSource<any> {
|
||||
if (elementId in this._elements) {
|
||||
return this._elements[elementId];
|
||||
}
|
||||
console.log("Can not find eventsource with id ", elementId);
|
||||
}
|
||||
|
||||
|
||||
removeId(oldId: string) {
|
||||
delete this._elements[oldId];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue