Files
nodeMap/utils/initializeMap.js

89 lines
2.9 KiB
JavaScript

// utils/initializeMap.js
// TODO: Add a comment
//FIXME: Add a comment
//BUG: Add a comment
import L from "leaflet";
import "leaflet-contextmenu";
import "leaflet/dist/leaflet.css";
import "leaflet-contextmenu/dist/leaflet.contextmenu.css";
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,
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,
],
minZoom: 5,
maxZoom: 15,
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 = "/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: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).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);
}
}
};