2021-01-27 01:14:16 +01:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
2021-07-28 00:09:17 +02:00
|
|
|
import {OsmObject} from "../Osm/OsmObject";
|
2021-05-06 01:33:09 +02:00
|
|
|
import Loc from "../../Models/Loc";
|
2021-09-21 03:10:15 +02:00
|
|
|
import {ElementStorage} from "../ElementStorage";
|
2021-09-22 05:02:09 +02:00
|
|
|
import FeaturePipeline from "../FeatureSource/FeaturePipeline";
|
2021-01-27 01:14:16 +01:00
|
|
|
|
|
|
|
/**
|
2021-05-06 01:33:09 +02:00
|
|
|
* Makes sure the hash shows the selected element and vice-versa.
|
2021-01-27 01:14:16 +01:00
|
|
|
*/
|
|
|
|
export default class SelectedFeatureHandler {
|
2021-09-22 05:02:09 +02:00
|
|
|
private static readonly _no_trigger_on = new Set(["welcome", "copyright", "layers", "new", "", undefined])
|
2021-09-21 03:10:15 +02:00
|
|
|
hash: UIEventSource<string>;
|
|
|
|
private readonly state: {
|
|
|
|
selectedElement: UIEventSource<any>
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
hash: UIEventSource<string>,
|
|
|
|
state: {
|
|
|
|
selectedElement: UIEventSource<any>,
|
2021-09-22 05:02:09 +02:00
|
|
|
allElements: ElementStorage,
|
|
|
|
featurePipeline: FeaturePipeline
|
2021-09-21 03:10:15 +02:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
this.hash = hash;
|
|
|
|
this.state = state
|
2021-09-22 05:02:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
// If the hash changes, set the selected element correctly
|
|
|
|
function setSelectedElementFromHash(h){
|
2021-01-27 01:14:16 +01:00
|
|
|
if (h === undefined || h === "") {
|
2021-09-22 05:02:09 +02:00
|
|
|
// Hash has been cleared - we clear the selected element
|
2021-09-21 03:10:15 +02:00
|
|
|
state.selectedElement.setData(undefined);
|
|
|
|
}else{
|
2021-09-22 05:02:09 +02:00
|
|
|
// we search the element to select
|
2021-09-21 03:10:15 +02:00
|
|
|
const feature = state.allElements.ContainingFeatures.get(h)
|
2021-10-01 05:24:10 +02:00
|
|
|
if(feature === undefined){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const currentlySeleced = state.selectedElement.data
|
|
|
|
if(currentlySeleced === undefined){
|
2021-09-21 03:10:15 +02:00
|
|
|
state.selectedElement.setData(feature)
|
2021-10-01 05:24:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(currentlySeleced.properties?.id === feature.properties.id){
|
|
|
|
// We already have the right feature
|
|
|
|
return;
|
2021-09-21 03:10:15 +02:00
|
|
|
}
|
2021-10-01 05:24:10 +02:00
|
|
|
state.selectedElement.setData(feature)
|
2021-07-28 00:09:17 +02:00
|
|
|
}
|
2021-09-22 05:02:09 +02:00
|
|
|
}
|
2021-07-28 00:09:17 +02:00
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
hash.addCallback(setSelectedElementFromHash)
|
2021-01-27 01:14:16 +01:00
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
|
|
|
|
// IF the selected element changes, set the hash correctly
|
2021-09-21 03:10:15 +02:00
|
|
|
state.selectedElement.addCallback(feature => {
|
2021-07-28 00:09:17 +02:00
|
|
|
if (feature === undefined) {
|
2021-10-01 02:57:41 +02:00
|
|
|
if (!SelectedFeatureHandler._no_trigger_on.has(hash.data)) {
|
2021-04-23 18:16:44 +02:00
|
|
|
hash.setData("")
|
|
|
|
}
|
2021-04-06 19:41:41 +02:00
|
|
|
}
|
2021-07-28 00:09:17 +02:00
|
|
|
|
2021-03-13 17:25:44 +01:00
|
|
|
const h = feature?.properties?.id;
|
2021-07-28 00:09:17 +02:00
|
|
|
if (h !== undefined) {
|
2021-03-13 17:25:44 +01:00
|
|
|
hash.setData(h)
|
|
|
|
}
|
2021-01-27 01:14:16 +01:00
|
|
|
})
|
2021-09-22 05:02:09 +02:00
|
|
|
|
|
|
|
state.featurePipeline.newDataLoadedSignal.addCallbackAndRunD(_ => {
|
|
|
|
// New data was loaded. In initial startup, the hash might be set (via the URL) but might not be selected yet
|
|
|
|
if(hash.data === undefined || SelectedFeatureHandler._no_trigger_on.has(hash.data)){
|
|
|
|
// This is an invalid hash anyway
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(state.selectedElement.data !== undefined){
|
|
|
|
// We already have something selected
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setSelectedElementFromHash(hash.data)
|
|
|
|
})
|
2021-01-27 01:14:16 +01:00
|
|
|
|
|
|
|
}
|
2021-07-28 00:09:17 +02:00
|
|
|
|
2021-05-06 01:33:09 +02:00
|
|
|
// If a feature is selected via the hash, zoom there
|
2021-07-28 00:09:17 +02:00
|
|
|
public zoomToSelectedFeature(location: UIEventSource<Loc>) {
|
2021-09-21 03:10:15 +02:00
|
|
|
const hash = this.hash.data;
|
2021-09-22 05:02:09 +02:00
|
|
|
if (hash === undefined || SelectedFeatureHandler._no_trigger_on.has(hash)) {
|
2021-05-06 01:33:09 +02:00
|
|
|
return; // No valid feature selected
|
|
|
|
}
|
2021-07-18 14:52:09 +02:00
|
|
|
// We should have a valid osm-ID and zoom to it... But we wrap it in try-catch to be sure
|
2021-07-28 00:09:17 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
OsmObject.DownloadObject(hash).addCallbackAndRunD(element => {
|
|
|
|
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();
|
|
|
|
})
|
|
|
|
} catch (e) {
|
2021-07-18 14:52:09 +02:00
|
|
|
console.error("Could not download OSM-object with id", hash, " - probably a weird hash")
|
|
|
|
}
|
2021-05-06 01:33:09 +02:00
|
|
|
}
|
2021-07-28 00:09:17 +02:00
|
|
|
|
2021-01-27 01:14:16 +01:00
|
|
|
}
|