forked from MapComplete/MapComplete
Merge branch 'develop' into alpha
This commit is contained in:
commit
3be0dbd22f
8 changed files with 3754 additions and 3629 deletions
100
Logic/Actors/SelectedElementTagsUpdater.ts
Normal file
100
Logic/Actors/SelectedElementTagsUpdater.ts
Normal file
|
@ -0,0 +1,100 @@
|
|||
/**
|
||||
* This actor will download the latest version of the selected element from OSM and update the tags if necessary.
|
||||
*/
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {ElementStorage} from "../ElementStorage";
|
||||
import {Changes} from "../Osm/Changes";
|
||||
import {OsmObject} from "../Osm/OsmObject";
|
||||
import {OsmConnection} from "../Osm/OsmConnection";
|
||||
|
||||
export default class SelectedElementTagsUpdater {
|
||||
|
||||
constructor(state: {
|
||||
selectedElement: UIEventSource<any>,
|
||||
allElements: ElementStorage,
|
||||
changes: Changes,
|
||||
osmConnection: OsmConnection
|
||||
}) {
|
||||
|
||||
|
||||
state.osmConnection.isLoggedIn.addCallbackAndRun(isLoggedIn => {
|
||||
if(isLoggedIn){
|
||||
SelectedElementTagsUpdater.installCallback(state)
|
||||
return true;
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
private static installCallback(state: {
|
||||
selectedElement: UIEventSource<any>,
|
||||
allElements: ElementStorage,
|
||||
changes: Changes,
|
||||
osmConnection: OsmConnection
|
||||
}) {
|
||||
|
||||
|
||||
state.selectedElement.addCallbackAndRunD(s => {
|
||||
const id = s.properties?.id
|
||||
OsmObject.DownloadObjectAsync(id).then(obj => {
|
||||
SelectedElementTagsUpdater.applyUpdate(state, obj, id)
|
||||
}).catch(e => {
|
||||
console.error("Could not update tags of ", id, "due to", e)
|
||||
})
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private static applyUpdate(state: {
|
||||
selectedElement: UIEventSource<any>,
|
||||
allElements: ElementStorage,
|
||||
changes: Changes,
|
||||
osmConnection: OsmConnection
|
||||
}, obj: OsmObject, id: string
|
||||
) {
|
||||
const pendingChanges = state.changes.pendingChanges.data
|
||||
.filter(change => change.type === obj.type && change.id === obj.id)
|
||||
.filter(change => change.tags !== undefined);
|
||||
const latestTags = obj.tags
|
||||
console.log("Applying updates of ", id, " got tags", latestTags, "and still have to apply changes: ", pendingChanges)
|
||||
|
||||
for (const pendingChange of pendingChanges) {
|
||||
const tagChanges = pendingChange.tags;
|
||||
for (const tagChange of tagChanges) {
|
||||
const key = tagChange.k
|
||||
const v = tagChange.v
|
||||
if (v === undefined || v === "") {
|
||||
delete latestTags[key]
|
||||
} else {
|
||||
latestTags[key] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// With the changes applied, we merge them onto the upstream object
|
||||
let somethingChanged = false;
|
||||
const currentTagsSource = state.allElements.getEventSourceById(id);
|
||||
const currentTags = currentTagsSource.data
|
||||
for (const key in latestTags) {
|
||||
let osmValue = latestTags[key]
|
||||
|
||||
if(typeof osmValue === "number"){
|
||||
osmValue = ""+osmValue
|
||||
}
|
||||
|
||||
const localValue = currentTags[key]
|
||||
if (localValue !== osmValue) {
|
||||
console.log("Local value:", localValue, "upstream", osmValue)
|
||||
somethingChanged = true;
|
||||
currentTags[key] = osmValue
|
||||
}
|
||||
}
|
||||
if (somethingChanged) {
|
||||
console.log("Detected upstream changes to the object when opening it, updating...")
|
||||
currentTagsSource.ping()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -7,12 +7,14 @@ export default class ImgurUploader {
|
|||
public readonly failed: UIEventSource<string[]> = new UIEventSource<string[]>([]);
|
||||
public readonly success: UIEventSource<string[]> = new UIEventSource<string[]>([]);
|
||||
private readonly _handleSuccessUrl: (string) => void;
|
||||
|
||||
public maxFileSizeInMegabytes = 10;
|
||||
|
||||
constructor(handleSuccessUrl: (string) => void) {
|
||||
this._handleSuccessUrl = handleSuccessUrl;
|
||||
}
|
||||
|
||||
public uploadMany(title: string, description: string, files: FileList) {
|
||||
public uploadMany(title: string, description: string, files: FileList): void {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
this.queue.data.push(files.item(i).name)
|
||||
}
|
||||
|
|
|
@ -36,6 +36,8 @@ export interface ChangeDescription {
|
|||
/**
|
||||
* All changes to tags
|
||||
* v = "" or v = undefined to erase this tag
|
||||
*
|
||||
* Note that this list will only contain the _changes_ to the tags, not the full set of tags
|
||||
*/
|
||||
tags?: { k: string, v: string }[],
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue