mapcomplete/Logic/StrayClickHandler.ts

64 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-06-29 03:12:44 +02:00
import {Basemap} from "./Basemap";
import L from "leaflet";
import {UIEventSource} from "../UI/UIEventSource";
import {UIElement} from "../UI/UIElement";
/**
* The stray-click-hanlders adds a marker to the map if no feature was clicked.
* Shows the given uiToShow-element in the messagebox
*/
export class StrayClickHandler {
private _basemap: Basemap;
private _lastMarker;
2020-07-21 00:07:04 +02:00
private _fullScreenMessage: UIEventSource<UIElement>;
2020-06-29 03:12:44 +02:00
private _uiToShow: (() => UIElement);
constructor(
basemap: Basemap,
selectElement: UIEventSource<{ feature: any }>,
2020-07-21 00:07:04 +02:00
fullScreenMessage: UIEventSource<UIElement>,
2020-06-29 03:12:44 +02:00
uiToShow: (() => UIElement)) {
this._basemap = basemap;
2020-07-21 00:07:04 +02:00
this._fullScreenMessage = fullScreenMessage;
2020-06-29 03:12:44 +02:00
this._uiToShow = uiToShow;
const self = this;
const map = basemap.map;
basemap.LastClickLocation.addCallback(function (lastClick) {
2020-07-01 02:12:33 +02:00
selectElement.setData(undefined);
2020-06-29 03:12:44 +02:00
if (self._lastMarker !== undefined) {
map.removeLayer(self._lastMarker);
}
2020-07-25 18:00:08 +02:00
self._lastMarker = L.marker([lastClick.lat, lastClick.lon], {
icon: L.icon({
iconUrl: "./assets/add.svg",
iconSize: [50,50],
iconAnchor: [25,50],
popupAnchor: [0,-45]
})
});
2020-06-29 16:21:36 +02:00
const uiElement = uiToShow();
const popup = L.popup().setContent(uiElement.Render());
uiElement.Update();
uiElement.Activate();
2020-06-29 03:12:44 +02:00
self._lastMarker.addTo(map);
2020-06-29 16:21:36 +02:00
self._lastMarker.bindPopup(popup).openPopup();
2020-07-01 02:12:33 +02:00
self._lastMarker.on("click", () => {
2020-07-21 00:07:04 +02:00
fullScreenMessage.setData(self._uiToShow());
2020-07-01 02:12:33 +02:00
});
uiElement.Update();
uiElement.Activate();
2020-06-29 03:12:44 +02:00
});
selectElement.addCallback(() => {
if (self._lastMarker !== undefined) {
map.removeLayer(self._lastMarker);
this._lastMarker = undefined;
}
})
}
}