2021-01-27 01:14:16 +01:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
|
|
|
import FeatureSource from "../FeatureSource/FeatureSource";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure the hash shows the selected element and vice-versa
|
|
|
|
*/
|
|
|
|
export default class SelectedFeatureHandler {
|
|
|
|
private readonly _featureSource: FeatureSource;
|
|
|
|
private readonly _hash: UIEventSource<string>;
|
|
|
|
private readonly _selectedFeature: UIEventSource<any>;
|
|
|
|
|
2021-04-23 18:16:44 +02:00
|
|
|
private static readonly _no_trigger_on = ["welcome","copyright","layers"]
|
|
|
|
|
2021-01-27 01:14:16 +01:00
|
|
|
constructor(hash: UIEventSource<string>,
|
|
|
|
selectedFeature: UIEventSource<any>,
|
|
|
|
featureSource: FeatureSource) {
|
|
|
|
this._hash = hash;
|
|
|
|
this._selectedFeature = selectedFeature;
|
|
|
|
this._featureSource = featureSource;
|
|
|
|
const self = this;
|
|
|
|
hash.addCallback(h => {
|
|
|
|
if (h === undefined || h === "") {
|
|
|
|
selectedFeature.setData(undefined);
|
|
|
|
}else{
|
|
|
|
self.selectFeature();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
featureSource.features.addCallback(_ => self.selectFeature());
|
|
|
|
|
|
|
|
selectedFeature.addCallback(feature => {
|
2021-04-06 19:41:41 +02:00
|
|
|
if(feature === undefined){
|
2021-04-23 18:16:44 +02:00
|
|
|
if(SelectedFeatureHandler._no_trigger_on.indexOf(hash.data) < 0){
|
|
|
|
hash.setData("")
|
|
|
|
}
|
2021-04-06 19:41:41 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 17:25:44 +01:00
|
|
|
const h = feature?.properties?.id;
|
|
|
|
if(h !== undefined){
|
|
|
|
hash.setData(h)
|
|
|
|
}
|
2021-01-27 01:14:16 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
this.selectFeature();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private selectFeature(){
|
|
|
|
const features = this._featureSource?.features?.data;
|
|
|
|
if(features === undefined){
|
|
|
|
return;
|
|
|
|
}
|
2021-02-05 18:58:06 +01:00
|
|
|
if(this._selectedFeature.data?.properties?.id === this._hash.data){
|
|
|
|
// Feature already selected
|
|
|
|
return;
|
|
|
|
}
|
2021-02-20 01:45:51 +01:00
|
|
|
|
|
|
|
const hash = this._hash.data;
|
|
|
|
if(hash === undefined || hash === "" || hash === "#"){
|
|
|
|
return;
|
|
|
|
}
|
2021-01-27 01:14:16 +01:00
|
|
|
for (const feature of features) {
|
|
|
|
const id = feature.feature?.properties?.id;
|
2021-02-20 01:45:51 +01:00
|
|
|
if(id === hash){
|
2021-01-27 01:14:16 +01:00
|
|
|
this._selectedFeature.setData(feature.feature);
|
2021-02-14 19:45:02 +01:00
|
|
|
break;
|
2021-01-27 01:14:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|