forked from MapComplete/MapComplete
OsmObjects can now be used as featureSource, load selected object immediately, zoom to selected object on open; fix #191
This commit is contained in:
parent
5ce4140510
commit
a0c1bc2137
13 changed files with 205 additions and 98 deletions
|
@ -1,8 +1,12 @@
|
|||
import {UIEventSource} from "../UIEventSource";
|
||||
import FeatureSource from "../FeatureSource/FeatureSource";
|
||||
import {OsmObject, OsmObjectMeta} from "../Osm/OsmObject";
|
||||
import Loc from "../../Models/Loc";
|
||||
import FeaturePipeline from "../FeatureSource/FeaturePipeline";
|
||||
import OsmApiFeatureSource from "../FeatureSource/OsmApiFeatureSource";
|
||||
|
||||
/**
|
||||
* Makes sure the hash shows the selected element and vice-versa
|
||||
* Makes sure the hash shows the selected element and vice-versa.
|
||||
*/
|
||||
export default class SelectedFeatureHandler {
|
||||
private readonly _featureSource: FeatureSource;
|
||||
|
@ -10,13 +14,16 @@ export default class SelectedFeatureHandler {
|
|||
private readonly _selectedFeature: UIEventSource<any>;
|
||||
|
||||
private static readonly _no_trigger_on = ["welcome","copyright","layers"]
|
||||
private readonly _osmApiSource: OsmApiFeatureSource;
|
||||
|
||||
constructor(hash: UIEventSource<string>,
|
||||
selectedFeature: UIEventSource<any>,
|
||||
featureSource: FeatureSource) {
|
||||
featureSource: FeaturePipeline,
|
||||
osmApiSource: OsmApiFeatureSource) {
|
||||
this._hash = hash;
|
||||
this._selectedFeature = selectedFeature;
|
||||
this._featureSource = featureSource;
|
||||
this._osmApiSource = osmApiSource;
|
||||
const self = this;
|
||||
hash.addCallback(h => {
|
||||
if (h === undefined || h === "") {
|
||||
|
@ -26,6 +33,8 @@ export default class SelectedFeatureHandler {
|
|||
}
|
||||
})
|
||||
|
||||
hash.addCallbackAndRun(h => self.downloadFeature(h))
|
||||
|
||||
featureSource.features.addCallback(_ => self.selectFeature());
|
||||
|
||||
selectedFeature.addCallback(feature => {
|
||||
|
@ -45,6 +54,32 @@ export default class SelectedFeatureHandler {
|
|||
|
||||
}
|
||||
|
||||
// If a feature is selected via the hash, zoom there
|
||||
public zoomToSelectedFeature(location: UIEventSource<Loc>){
|
||||
const hash = this._hash.data;
|
||||
if(hash === undefined || SelectedFeatureHandler._no_trigger_on.indexOf(hash) >= 0){
|
||||
return; // No valid feature selected
|
||||
}
|
||||
// We should have a valid osm-ID and zoom to it
|
||||
OsmObject.DownloadObject(hash, (element: OsmObject, meta: OsmObjectMeta) => {
|
||||
const centerpoint = element.centerpoint();
|
||||
console.log("Zooming to location for select point: ", centerpoint)
|
||||
location.data.lat = centerpoint[0]
|
||||
location.data.lon = centerpoint[1]
|
||||
location.ping();
|
||||
})
|
||||
}
|
||||
|
||||
private downloadFeature(hash: string){
|
||||
if(hash === undefined || hash === ""){
|
||||
return;
|
||||
}
|
||||
if(SelectedFeatureHandler._no_trigger_on.indexOf(hash) >= 0){
|
||||
return;
|
||||
}
|
||||
this._osmApiSource.load(hash)
|
||||
}
|
||||
|
||||
private selectFeature(){
|
||||
const features = this._featureSource?.features?.data;
|
||||
if(features === undefined){
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
import {UIEventSource} from "../UIEventSource";
|
||||
import {ElementStorage} from "../ElementStorage";
|
||||
import {OsmObject, OsmObjectMeta} from "../Osm/OsmObject";
|
||||
|
||||
export default class UpdateTagsFromOsmAPI {
|
||||
|
||||
|
||||
/***
|
||||
* This actor downloads the element from the OSM-API and updates the corresponding tags in the UI-updater.
|
||||
*/
|
||||
constructor(idToDownload: UIEventSource<string>, allElements: ElementStorage, callback? : (element: OsmObject) => void) {
|
||||
|
||||
idToDownload.addCallbackAndRun(id => {
|
||||
if (id === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
OsmObject.DownloadObject(id, (element: OsmObject, meta: OsmObjectMeta) => {
|
||||
console.debug("Updating tags from the OSM-API: ", element)
|
||||
|
||||
|
||||
const tags = element.tags;
|
||||
tags["_last_edit:contributor"] = meta["_last_edit:contributor"]
|
||||
tags["_last_edit:contributor:uid"] = meta["_last_edit:contributor:uid"]
|
||||
tags["_last_edit:changeset"] = meta["_last_edit:changeset"]
|
||||
tags["_last_edit:timestamp"] = meta["_last_edit:timestamp"].toLocaleString()
|
||||
tags["_version_number"] = meta._version_number
|
||||
if (!allElements.has(id)) {
|
||||
console.warn("Adding element by id")
|
||||
allElements.addElementById(id, new UIEventSource<any>(tags))
|
||||
} else {
|
||||
// We merge
|
||||
console.warn("merging by OSM API UPDATE")
|
||||
allElements.addOrGetById(id, tags)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue