mapcomplete/Logic/Actors/StrayClickHandler.ts

84 lines
3.1 KiB
TypeScript
Raw Normal View History

import * as L from "leaflet";
2021-01-02 21:03:40 +01:00
import {UIEventSource} from "../UIEventSource";
import ScrollableFullScreen from "../../UI/Base/ScrollableFullScreen";
import AddNewMarker from "../../UI/BigComponents/AddNewMarker";
import FilteredLayer from "../../Models/FilteredLayer";
import Constants from "../../Models/Constants";
2020-06-29 03:12:44 +02:00
/**
* The stray-click-hanlders adds a marker to the map if no feature was clicked.
* Shows the given uiToShow-element in the messagebox
*/
export default class StrayClickHandler {
2020-06-29 03:12:44 +02:00
private _lastMarker;
constructor(
lastClickLocation: UIEventSource<{ lat: number, lon: number }>,
2021-01-02 21:03:40 +01:00
selectedElement: UIEventSource<string>,
filteredLayers: UIEventSource<FilteredLayer[]>,
2021-01-02 21:03:40 +01:00
leafletMap: UIEventSource<L.Map>,
uiToShow: ScrollableFullScreen) {
2020-06-29 03:12:44 +02:00
const self = this;
2021-01-02 21:03:40 +01:00
filteredLayers.data.forEach((filteredLayer) => {
filteredLayer.isDisplayed.addCallback(isEnabled => {
if (isEnabled && self._lastMarker && leafletMap.data !== undefined) {
// When a layer is activated, we remove the 'last click location' in order to force the user to reclick
// This reclick might be at a location where a feature now appeared...
leafletMap.data.removeLayer(self._lastMarker);
}
})
})
2021-01-02 21:03:40 +01:00
lastClickLocation.addCallback(function (lastClick) {
2020-06-29 03:12:44 +02:00
if (self._lastMarker !== undefined) {
2021-01-02 21:03:40 +01:00
leafletMap.data?.removeLayer(self._lastMarker);
2020-06-29 03:12:44 +02:00
}
if (lastClick === undefined) {
2021-01-22 00:40:15 +01:00
return;
}
selectedElement.setData(undefined);
const clickCoor: [number, number] = [lastClick.lat, lastClick.lon]
self._lastMarker = L.marker(clickCoor, {
icon: L.divIcon({
html: new AddNewMarker(filteredLayers).ConstructElement(),
iconSize: [50, 50],
iconAnchor: [25, 50],
popupAnchor: [0, -45]
})
2020-07-25 18:00:08 +02:00
});
const popup = L.popup({
autoPan: true,
autoPanPaddingTopLeft: [15, 15],
closeOnEscapeKey: true,
autoClose: true
}).setContent("<div id='strayclick' style='height: 65vh'></div>");
2021-01-02 21:03:40 +01:00
self._lastMarker.addTo(leafletMap.data);
self._lastMarker.bindPopup(popup);
2020-06-29 16:21:36 +02:00
2020-07-01 02:12:33 +02:00
self._lastMarker.on("click", () => {
if (leafletMap.data.getZoom() < Constants.userJourney.minZoomLevelToAddNewPoints) {
self._lastMarker.closePopup()
leafletMap.data.flyTo(clickCoor, Constants.userJourney.minZoomLevelToAddNewPoints)
return;
}
2021-06-14 02:39:23 +02:00
uiToShow.AttachTo("strayclick")
uiToShow.Activate();
2020-07-01 02:12:33 +02:00
});
2020-06-29 03:12:44 +02:00
});
2021-01-02 21:03:40 +01:00
selectedElement.addCallback(() => {
2020-06-29 03:12:44 +02:00
if (self._lastMarker !== undefined) {
2021-01-02 21:03:40 +01:00
leafletMap.data.removeLayer(self._lastMarker);
2020-06-29 03:12:44 +02:00
this._lastMarker = undefined;
}
})
}
}