100 lines
3.3 KiB
JavaScript
100 lines
3.3 KiB
JavaScript
// utils/initializeMap.js
|
|
import L from "leaflet";
|
|
import "leaflet-contextmenu";
|
|
import "leaflet/dist/leaflet.css";
|
|
import "leaflet-contextmenu/dist/leaflet.contextmenu.css";
|
|
import "leaflet-control-geocoder";
|
|
|
|
import * as urls from "../config/urls.js";
|
|
import * as layers from "../config/layers.js";
|
|
import { openInNewTab } from "./openInNewTab.js";
|
|
|
|
export const initializeMap = (mapRef, setMap, setOms, setMenuItemAdded, addItemsToMapContextMenu, hasRights, setPolylineEventsDisabled) => {
|
|
if (mapRef.current) {
|
|
const initMap = L.map(mapRef.current, {
|
|
center: [53.111111, 8.4625],
|
|
zoom: 12,
|
|
minZoom: 5,
|
|
maxZoom: 15,
|
|
layers: [
|
|
layers.MAP_LAYERS.TALAS,
|
|
layers.MAP_LAYERS.ECI,
|
|
layers.MAP_LAYERS.GSMModem,
|
|
layers.MAP_LAYERS.CiscoRouter,
|
|
layers.MAP_LAYERS.WAGO,
|
|
layers.MAP_LAYERS.Siemens,
|
|
layers.MAP_LAYERS.OTDR,
|
|
layers.MAP_LAYERS.WDM,
|
|
layers.MAP_LAYERS.GMA,
|
|
layers.MAP_LAYERS.TALASICL,
|
|
layers.MAP_LAYERS.Sonstige,
|
|
layers.MAP_LAYERS.ULAF,
|
|
],
|
|
|
|
zoomControl: false,
|
|
contextmenu: true,
|
|
contextmenuItems: [
|
|
{
|
|
text: "Station öffnen (Tab)",
|
|
icon: "/img/screen_new.png",
|
|
callback: (e) => {
|
|
const clickedElement = e.relatedTarget;
|
|
|
|
if (!clickedElement) {
|
|
console.error("No valid target for the context menu entry (Element is null).");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (clickedElement instanceof L.Marker || clickedElement?.options?.link) {
|
|
const link = "http://" + window.location.hostname + "/talas5/devices/" + clickedElement.options.link;
|
|
if (link) {
|
|
console.log("Opening link in a new tab:", link);
|
|
window.open(link, "_blank");
|
|
} else {
|
|
console.error("No link found in the Marker options.");
|
|
}
|
|
} else {
|
|
console.error("No valid target for the context menu entry");
|
|
}
|
|
} catch (error) {
|
|
console.error("Error processing the context menu:", error);
|
|
}
|
|
},
|
|
},
|
|
"-",
|
|
],
|
|
});
|
|
|
|
// Füge die Tile-Layer hinzu
|
|
L.tileLayer(urls.OFFLINE_TILE_LAYER, {
|
|
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
|
|
}).addTo(initMap);
|
|
|
|
// Suchfeld hinzufügen
|
|
/* const geocoder = L.Control.geocoder({
|
|
defaultMarkGeocode: false,
|
|
})
|
|
.on("markgeocode", function (e) {
|
|
const latlng = e.geocode.center;
|
|
initMap.setView(latlng, 15);
|
|
L.marker(latlng).addTo(initMap).bindPopup(e.geocode.name).openPopup();
|
|
})
|
|
.addTo(initMap); */
|
|
|
|
// Initialisiere OverlappingMarkerSpiderfier
|
|
const overlappingMarkerSpiderfier = new OverlappingMarkerSpiderfier(initMap, {
|
|
nearbyDistance: 20,
|
|
});
|
|
|
|
// Setze die Map und OMS in den State
|
|
setMap(initMap);
|
|
setOms(overlappingMarkerSpiderfier);
|
|
|
|
// Wenn Rechte geladen sind und es noch nicht hinzugefügt wurde, füge das Kontextmenü hinzu
|
|
if (hasRights && !setMenuItemAdded) {
|
|
addItemsToMapContextMenu(initMap, setMenuItemAdded, setPolylineEventsDisabled);
|
|
}
|
|
}
|
|
};
|