2021-09-21 02:10:42 +02:00
|
|
|
import {Utils} from "../../Utils";
|
|
|
|
import BaseUIElement from "../BaseUIElement";
|
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
|
|
|
import Loc from "../../Models/Loc";
|
|
|
|
import BaseLayer from "../../Models/BaseLayer";
|
|
|
|
import AvailableBaseLayers from "../../Logic/Actors/AvailableBaseLayers";
|
|
|
|
import * as L from "leaflet";
|
|
|
|
import {Map} from "leaflet";
|
2021-09-22 05:02:09 +02:00
|
|
|
import Minimap, {MinimapObj, MinimapOptions} from "./Minimap";
|
2021-09-28 17:30:48 +02:00
|
|
|
import {BBox} from "../../Logic/BBox";
|
2021-10-22 02:34:06 +02:00
|
|
|
import 'leaflet-polylineoffset'
|
2021-11-03 00:44:53 +01:00
|
|
|
import {SimpleMapScreenshoter} from "leaflet-simple-map-screenshoter";
|
2021-11-21 02:44:35 +01:00
|
|
|
import BackgroundMapSwitch from "../BigComponents/BackgroundMapSwitch";
|
2022-01-19 20:34:04 +01:00
|
|
|
import AvailableBaseLayersImplementation from "../../Logic/Actors/AvailableBaseLayersImplementation";
|
2021-09-21 02:10:42 +02:00
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
export default class MinimapImplementation extends BaseUIElement implements MinimapObj {
|
2021-09-21 02:10:42 +02:00
|
|
|
private static _nextId = 0;
|
|
|
|
public readonly leafletMap: UIEventSource<Map>
|
2022-01-26 21:40:38 +01:00
|
|
|
public readonly location: UIEventSource<Loc>;
|
|
|
|
public readonly bounds: UIEventSource<BBox> | undefined;
|
2021-09-21 02:10:42 +02:00
|
|
|
private readonly _id: string;
|
|
|
|
private readonly _background: UIEventSource<BaseLayer>;
|
|
|
|
private _isInited = false;
|
|
|
|
private _allowMoving: boolean;
|
|
|
|
private readonly _leafletoptions: any;
|
|
|
|
private readonly _onFullyLoaded: (leaflet: L.Map) => void
|
|
|
|
private readonly _attribution: BaseUIElement | boolean;
|
2021-11-21 02:44:35 +01:00
|
|
|
private readonly _addLayerControl: boolean;
|
2022-01-18 18:52:42 +01:00
|
|
|
private readonly _options: MinimapOptions;
|
2021-09-21 02:10:42 +02:00
|
|
|
|
2022-01-21 01:57:16 +01:00
|
|
|
private constructor(options?: MinimapOptions) {
|
2021-09-21 02:10:42 +02:00
|
|
|
super()
|
|
|
|
options = options ?? {}
|
|
|
|
this.leafletMap = options.leafletMap ?? new UIEventSource<Map>(undefined)
|
|
|
|
this._background = options?.background ?? new UIEventSource<BaseLayer>(AvailableBaseLayers.osmCarto)
|
2022-01-19 20:34:04 +01:00
|
|
|
this.location = options?.location ?? new UIEventSource<Loc>({lat: 0, lon: 0, zoom: 1})
|
|
|
|
this.bounds = options?.bounds;
|
2021-09-21 02:10:42 +02:00
|
|
|
this._id = "minimap" + MinimapImplementation._nextId;
|
|
|
|
this._allowMoving = options.allowMoving ?? true;
|
|
|
|
this._leafletoptions = options.leafletOptions ?? {}
|
|
|
|
this._onFullyLoaded = options.onFullyLoaded
|
|
|
|
this._attribution = options.attribution
|
2021-11-21 02:44:35 +01:00
|
|
|
this._addLayerControl = options.addLayerControl ?? false
|
2022-01-08 20:02:07 +01:00
|
|
|
this._options = options
|
2021-09-21 02:10:42 +02:00
|
|
|
MinimapImplementation._nextId++
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static initialize() {
|
2022-01-19 20:34:04 +01:00
|
|
|
AvailableBaseLayers.implement(new AvailableBaseLayersImplementation())
|
2021-09-21 02:10:42 +02:00
|
|
|
Minimap.createMiniMap = options => new MinimapImplementation(options)
|
|
|
|
}
|
2022-01-18 18:52:42 +01:00
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
public installBounds(factor: number | BBox, showRange?: boolean) {
|
|
|
|
this.leafletMap.addCallbackD(leaflet => {
|
|
|
|
let bounds;
|
|
|
|
if (typeof factor === "number") {
|
2022-01-25 21:55:51 +01:00
|
|
|
bounds = leaflet.getBounds().pad(factor)
|
|
|
|
leaflet.setMaxBounds(bounds)
|
2021-09-22 20:44:53 +02:00
|
|
|
} else {
|
2021-09-22 05:02:09 +02:00
|
|
|
// @ts-ignore
|
|
|
|
leaflet.setMaxBounds(factor.toLeaflet())
|
|
|
|
bounds = leaflet.getBounds()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showRange) {
|
|
|
|
const data = {
|
|
|
|
type: "FeatureCollection",
|
|
|
|
features: [{
|
|
|
|
"type": "Feature",
|
|
|
|
"geometry": {
|
|
|
|
"type": "LineString",
|
|
|
|
"coordinates": [
|
|
|
|
[
|
|
|
|
bounds.getEast(),
|
|
|
|
bounds.getNorth()
|
|
|
|
],
|
|
|
|
[
|
|
|
|
bounds.getWest(),
|
|
|
|
bounds.getNorth()
|
|
|
|
],
|
|
|
|
[
|
|
|
|
bounds.getWest(),
|
|
|
|
bounds.getSouth()
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
bounds.getEast(),
|
|
|
|
bounds.getSouth()
|
|
|
|
],
|
|
|
|
[
|
|
|
|
bounds.getEast(),
|
|
|
|
bounds.getNorth()
|
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
L.geoJSON(data, {
|
|
|
|
style: {
|
2022-01-25 21:55:51 +01:00
|
|
|
color: "#f44",
|
|
|
|
weight: 4,
|
|
|
|
opacity: 0.7
|
2021-09-22 05:02:09 +02:00
|
|
|
}
|
|
|
|
}).addTo(leaflet)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-01-18 18:52:42 +01:00
|
|
|
|
2022-01-06 18:51:52 +01:00
|
|
|
Destroy() {
|
|
|
|
super.Destroy();
|
|
|
|
console.warn("Decomissioning minimap", this._id)
|
|
|
|
const mp = this.leafletMap.data
|
|
|
|
this.leafletMap.setData(null)
|
|
|
|
mp.off()
|
|
|
|
mp.remove()
|
|
|
|
}
|
2021-09-22 05:02:09 +02:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
public async TakeScreenshot() {
|
|
|
|
const screenshotter = new SimpleMapScreenshoter();
|
|
|
|
screenshotter.addTo(this.leafletMap.data);
|
|
|
|
return await screenshotter.takeScreen('image')
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:10:42 +02:00
|
|
|
protected InnerConstructElement(): HTMLElement {
|
|
|
|
const div = document.createElement("div")
|
|
|
|
div.id = this._id;
|
|
|
|
div.style.height = "100%"
|
|
|
|
div.style.width = "100%"
|
|
|
|
div.style.minWidth = "40px"
|
|
|
|
div.style.minHeight = "40px"
|
|
|
|
div.style.position = "relative"
|
|
|
|
const wrapper = document.createElement("div")
|
|
|
|
wrapper.appendChild(div)
|
|
|
|
const self = this;
|
|
|
|
// @ts-ignore
|
|
|
|
const resizeObserver = new ResizeObserver(_ => {
|
2022-01-18 18:52:42 +01:00
|
|
|
if (wrapper.clientHeight === 0 || wrapper.clientWidth === 0) {
|
2022-01-06 18:51:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-18 18:52:42 +01:00
|
|
|
if (wrapper.offsetParent === null || window.getComputedStyle(wrapper).display === 'none') {
|
2022-01-06 18:51:52 +01:00
|
|
|
// Not visible
|
|
|
|
return;
|
|
|
|
}
|
2021-09-22 20:44:53 +02:00
|
|
|
try {
|
|
|
|
self.InitMap();
|
|
|
|
self.leafletMap?.data?.invalidateSize()
|
|
|
|
} catch (e) {
|
2021-10-04 00:18:08 +02:00
|
|
|
console.warn("Could not construct a minimap:", e)
|
2021-09-22 20:44:53 +02:00
|
|
|
}
|
2021-09-21 02:10:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
resizeObserver.observe(div);
|
2021-11-21 02:44:35 +01:00
|
|
|
|
|
|
|
if (this._addLayerControl) {
|
|
|
|
const switcher = new BackgroundMapSwitch({
|
2022-01-19 20:34:04 +01:00
|
|
|
locationControl: this.location,
|
2021-11-21 02:44:35 +01:00
|
|
|
backgroundLayer: this._background
|
|
|
|
},
|
|
|
|
this._background
|
|
|
|
).SetClass("top-0 right-0 z-above-map absolute")
|
|
|
|
wrapper.appendChild(switcher.ConstructElement())
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:10:42 +02:00
|
|
|
return wrapper;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private InitMap() {
|
|
|
|
if (this._constructedHtmlElement === undefined) {
|
|
|
|
// This element isn't initialized yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document.getElementById(this._id) === null) {
|
|
|
|
// not yet attached, we probably got some other event
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._isInited) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._isInited = true;
|
2022-01-19 20:34:04 +01:00
|
|
|
const location = this.location;
|
2021-09-21 02:10:42 +02:00
|
|
|
const self = this;
|
|
|
|
let currentLayer = this._background.data.layer()
|
2021-09-22 20:44:53 +02:00
|
|
|
let latLon = <[number, number]>[location.data?.lat ?? 0, location.data?.lon ?? 0]
|
2021-11-07 16:34:51 +01:00
|
|
|
if (isNaN(latLon[0]) || isNaN(latLon[1])) {
|
|
|
|
latLon = [0, 0]
|
2021-09-22 20:44:53 +02:00
|
|
|
}
|
2021-09-21 02:10:42 +02:00
|
|
|
const options = {
|
2021-09-22 20:44:53 +02:00
|
|
|
center: latLon,
|
2021-09-21 02:10:42 +02:00
|
|
|
zoom: location.data?.zoom ?? 2,
|
|
|
|
layers: [currentLayer],
|
|
|
|
zoomControl: false,
|
|
|
|
attributionControl: this._attribution !== undefined,
|
|
|
|
dragging: this._allowMoving,
|
|
|
|
scrollWheelZoom: this._allowMoving,
|
|
|
|
doubleClickZoom: this._allowMoving,
|
|
|
|
keyboard: this._allowMoving,
|
|
|
|
touchZoom: this._allowMoving,
|
|
|
|
// Disabling this breaks the geojson layer - don't ask me why! zoomAnimation: this._allowMoving,
|
|
|
|
fadeAnimation: this._allowMoving,
|
2022-01-08 20:02:07 +01:00
|
|
|
maxZoom: 21
|
2021-09-21 02:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Utils.Merge(this._leafletoptions, options)
|
|
|
|
|
|
|
|
const map = L.map(this._id, options);
|
|
|
|
if (self._onFullyLoaded !== undefined) {
|
|
|
|
|
|
|
|
currentLayer.on("load", () => {
|
|
|
|
console.log("Fully loaded all tiles!")
|
|
|
|
self._onFullyLoaded(map)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Users are not allowed to zoom to the 'copies' on the left and the right, stuff goes wrong then
|
|
|
|
// We give a bit of leeway for people on the edges
|
|
|
|
// Also see: https://www.reddit.com/r/openstreetmap/comments/ih4zzc/mapcomplete_a_new_easytouse_editor/g31ubyv/
|
|
|
|
|
|
|
|
map.setMaxBounds(
|
|
|
|
[[-100, -200], [100, 200]]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this._attribution !== undefined) {
|
|
|
|
if (this._attribution === true) {
|
|
|
|
map.attributionControl.setPrefix(false)
|
|
|
|
} else {
|
|
|
|
map.attributionControl.setPrefix(
|
|
|
|
"<span id='leaflet-attribution'></span>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._background.addCallbackAndRun(layer => {
|
|
|
|
const newLayer = layer.layer()
|
|
|
|
if (currentLayer !== undefined) {
|
|
|
|
map.removeLayer(currentLayer);
|
|
|
|
}
|
|
|
|
currentLayer = newLayer;
|
|
|
|
if (self._onFullyLoaded !== undefined) {
|
|
|
|
|
|
|
|
currentLayer.on("load", () => {
|
|
|
|
console.log("Fully loaded all tiles!")
|
|
|
|
self._onFullyLoaded(map)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
map.addLayer(newLayer);
|
|
|
|
if (self._attribution !== true && self._attribution !== false) {
|
|
|
|
self._attribution?.AttachTo('leaflet-attribution')
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
let isRecursing = false;
|
|
|
|
map.on("moveend", function () {
|
|
|
|
if (isRecursing) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (map.getZoom() === location.data.zoom &&
|
|
|
|
map.getCenter().lat === location.data.lat &&
|
|
|
|
map.getCenter().lng === location.data.lon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
location.data.zoom = map.getZoom();
|
|
|
|
location.data.lat = map.getCenter().lat;
|
|
|
|
location.data.lon = map.getCenter().lng;
|
|
|
|
isRecursing = true;
|
|
|
|
location.ping();
|
|
|
|
|
2022-01-19 20:34:04 +01:00
|
|
|
if (self.bounds !== undefined) {
|
|
|
|
self.bounds.setData(BBox.fromLeafletBounds(map.getBounds()))
|
2021-09-21 02:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isRecursing = false; // This is ugly, I know
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
location.addCallback(loc => {
|
|
|
|
const mapLoc = map.getCenter()
|
|
|
|
const dlat = Math.abs(loc.lat - mapLoc[0])
|
|
|
|
const dlon = Math.abs(loc.lon - mapLoc[1])
|
|
|
|
|
|
|
|
if (dlat < 0.000001 && dlon < 0.000001 && map.getZoom() === loc.zoom) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
map.setView([loc.lat, loc.lon], loc.zoom)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2022-01-19 20:34:04 +01:00
|
|
|
if (self.bounds !== undefined) {
|
|
|
|
self.bounds.setData(BBox.fromLeafletBounds(map.getBounds()))
|
2021-09-21 02:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-08 20:02:07 +01:00
|
|
|
if (this._options.lastClickLocation) {
|
|
|
|
const lastClickLocation = this._options.lastClickLocation
|
2021-09-21 02:10:42 +02:00
|
|
|
map.on("click", function (e) {
|
|
|
|
// @ts-ignore
|
|
|
|
lastClickLocation?.setData({lat: e.latlng.lat, lon: e.latlng.lng})
|
|
|
|
});
|
|
|
|
|
|
|
|
map.on("contextmenu", function (e) {
|
|
|
|
// @ts-ignore
|
|
|
|
lastClickLocation?.setData({lat: e.latlng.lat, lon: e.latlng.lng});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.leafletMap.setData(map)
|
|
|
|
}
|
|
|
|
}
|