2021-01-03 03:09:52 +01:00
|
|
|
import FeatureSource from "./FeatureSource";
|
|
|
|
import {UIEventSource} from "../UIEventSource";
|
|
|
|
import LayerConfig from "../../Customizations/JSON/LayerConfig";
|
|
|
|
import Loc from "../../Models/Loc";
|
|
|
|
|
|
|
|
export default class FilteringFeatureSource implements FeatureSource {
|
|
|
|
public features: UIEventSource<{ feature: any; freshness: Date }[]> = new UIEventSource<{ feature: any; freshness: Date }[]>([]);
|
|
|
|
|
|
|
|
constructor(layers: {
|
|
|
|
isDisplayed: UIEventSource<boolean>,
|
|
|
|
layerDef: LayerConfig
|
|
|
|
}[],
|
|
|
|
location: UIEventSource<Loc>,
|
|
|
|
upstream: FeatureSource) {
|
|
|
|
|
|
|
|
const self = this;
|
2021-02-14 19:45:02 +01:00
|
|
|
|
|
|
|
const layerDict = {};
|
|
|
|
for (const layer of layers) {
|
|
|
|
layerDict[layer.layerDef.id] = layer;
|
|
|
|
}
|
2021-01-04 04:06:21 +01:00
|
|
|
|
2021-02-14 19:45:02 +01:00
|
|
|
|
2021-01-03 03:09:52 +01:00
|
|
|
function update() {
|
2021-02-14 19:45:02 +01:00
|
|
|
console.log("Updating the filtering layer")
|
2021-01-03 03:09:52 +01:00
|
|
|
const features: { feature: any, freshness: Date }[] = upstream.features.data;
|
2021-02-14 19:45:02 +01:00
|
|
|
|
|
|
|
|
2021-01-03 03:09:52 +01:00
|
|
|
const newFeatures = features.filter(f => {
|
2021-01-04 04:06:21 +01:00
|
|
|
const layerId = f.feature._matching_layer_id;
|
2021-02-14 19:45:02 +01:00
|
|
|
if (layerId !== undefined) {
|
|
|
|
const layer: {
|
|
|
|
isDisplayed: UIEventSource<boolean>,
|
|
|
|
layerDef: LayerConfig
|
|
|
|
} = layerDict[layerId];
|
|
|
|
if (layer === undefined) {
|
|
|
|
console.error("No layer found with id ", layerId);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (FilteringFeatureSource.showLayer(layer, location)) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-08 04:06:10 +01:00
|
|
|
}
|
2021-02-14 19:45:02 +01:00
|
|
|
// Does it match any other layer - e.g. because of a switch?
|
2021-01-08 04:06:10 +01:00
|
|
|
for (const toCheck of layers) {
|
2021-01-08 14:23:12 +01:00
|
|
|
if (!FilteringFeatureSource.showLayer(toCheck, location)) {
|
2021-01-08 04:06:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-01-08 14:23:12 +01:00
|
|
|
if (toCheck.layerDef.overpassTags.matchesProperties(f.feature.properties)) {
|
2021-01-08 04:06:10 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-01-08 14:23:12 +01:00
|
|
|
|
2021-01-03 03:09:52 +01:00
|
|
|
});
|
|
|
|
self.features.setData(newFeatures);
|
|
|
|
}
|
2021-01-04 04:06:21 +01:00
|
|
|
|
2021-02-14 19:45:02 +01:00
|
|
|
|
2021-01-04 04:06:21 +01:00
|
|
|
upstream.features.addCallback(() => {
|
2021-01-08 14:23:12 +01:00
|
|
|
update()
|
|
|
|
});
|
2021-01-05 10:56:25 +01:00
|
|
|
location.map(l => {
|
|
|
|
// We want something that is stable for the shown layers
|
|
|
|
const displayedLayerIndexes = [];
|
|
|
|
for (let i = 0; i < layers.length; i++) {
|
2021-01-08 14:23:12 +01:00
|
|
|
if (l.zoom < layers[i].layerDef.minzoom) {
|
2021-01-05 11:17:12 +01:00
|
|
|
continue;
|
2021-01-05 10:56:25 +01:00
|
|
|
}
|
2021-01-08 14:23:12 +01:00
|
|
|
if (!layers[i].isDisplayed.data) {
|
2021-01-05 11:17:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
displayedLayerIndexes.push(i);
|
2021-01-05 10:56:25 +01:00
|
|
|
}
|
|
|
|
return displayedLayerIndexes.join(",")
|
2021-01-05 11:17:12 +01:00
|
|
|
}, layers.map(l => l.isDisplayed))
|
2021-01-05 10:56:25 +01:00
|
|
|
.addCallback(() => {
|
2021-01-08 14:23:12 +01:00
|
|
|
update();
|
|
|
|
});
|
2021-01-03 03:09:52 +01:00
|
|
|
|
2021-01-15 00:29:07 +01:00
|
|
|
update();
|
2021-01-03 03:09:52 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-08 14:23:12 +01:00
|
|
|
private static showLayer(layer: {
|
|
|
|
isDisplayed: UIEventSource<boolean>,
|
|
|
|
layerDef: LayerConfig
|
|
|
|
}, location: UIEventSource<Loc>) {
|
|
|
|
return layer.isDisplayed.data && (layer.layerDef.minzoom <= location.data.zoom)
|
|
|
|
}
|
2021-01-03 03:09:52 +01:00
|
|
|
}
|