181 lines
6.2 KiB
JavaScript
181 lines
6.2 KiB
JavaScript
import { useEffect } from "react";
|
|
import { addContextMenuToMarker } from "../../utils/addContextMenuToMarker";
|
|
import { addItemsToMapContextMenu } from "../../components/useMapContextMenu"; // Importiere die Funktion
|
|
|
|
const useGmaMarkersLayer = (map, markers, GisStationsMeasurements, GMA, oms, isVisible) => {
|
|
const protocol = window.location.protocol; // Holt das Protokoll (z.B. http oder https)
|
|
const hostname = window.location.hostname; // Holt den Hostnamen (z.B. 10.10.0.70)
|
|
const baseUrl = `${protocol}//${hostname}/talas5/devices/`; // Basis-URL zusammenstellen
|
|
|
|
useEffect(() => {
|
|
if (!map || !isVisible) return;
|
|
|
|
// Entferne alte Marker
|
|
GMA.clearLayers();
|
|
|
|
markers.forEach((marker) => {
|
|
const relevantMeasurements = GisStationsMeasurements.filter((m) => m.Area_Name === marker.options.areaName);
|
|
|
|
let measurements = {};
|
|
let area_name = marker.options.areaName;
|
|
|
|
relevantMeasurements.forEach((m) => {
|
|
measurements[m.Na] = m.Val;
|
|
});
|
|
|
|
const lt = measurements["LT"] || "-";
|
|
const fbt = measurements["FBT"] || "-";
|
|
const gt = measurements["GT"] || "-";
|
|
const rlf = measurements["RLF"] || "-";
|
|
|
|
marker.bindTooltip(
|
|
`
|
|
<div class="p-0 rounded-lg bg-white bg-opacity-90 tooltip-content">
|
|
<div class="font-bold text-sm text-black">
|
|
<span>${area_name}</span>
|
|
</div>
|
|
<div class="font-bold text-xxs text-blue-700">
|
|
<span>LT : ${lt} °C</span>
|
|
</div>
|
|
<div class="font-bold text-xxs text-red-700">
|
|
<span>FBT : ${fbt} °C</span>
|
|
</div>
|
|
<div class="font-bold text-xxs text-yellow-500">
|
|
<span>GT : ${gt} °C</span>
|
|
</div>
|
|
<div class="font-bold text-xxs text-green-700">
|
|
<span>RLF : ${rlf} %</span>
|
|
</div>
|
|
</div>
|
|
`,
|
|
{
|
|
permanent: true,
|
|
direction: "auto",
|
|
offset: [60, 0],
|
|
interactive: true,
|
|
}
|
|
);
|
|
|
|
marker.on("tooltipopen", (e) => {
|
|
const tooltipElement = e.tooltip._contentNode;
|
|
|
|
if (tooltipElement) {
|
|
tooltipElement.addEventListener("contextmenu", (event) => {
|
|
event.preventDefault();
|
|
console.log("Rechtsklick auf Tooltip erkannt");
|
|
|
|
// Kombiniere die Kontextmenü-Items
|
|
const combinedContextMenuItems = [
|
|
{
|
|
text: "Station öffnen (Tab)",
|
|
icon: "/img/screen_new.png",
|
|
callback: () => {
|
|
if (marker.options.link) {
|
|
const fullUrl = `${baseUrl}${marker.options.link}`;
|
|
window.open(fullUrl, "_blank");
|
|
console.log("Link in neuem Tab geöffnet:", fullUrl);
|
|
} else {
|
|
console.error("Kein Link für Tooltip vorhanden.");
|
|
}
|
|
},
|
|
},
|
|
{ separator: true },
|
|
// Füge zusätzliche Items hinzu
|
|
...[
|
|
{
|
|
text: "Koordinaten anzeigen",
|
|
icon: "/img/not_listed_location.png",
|
|
callback: (e) => {
|
|
alert("Breitengrad: " + e.latlng.lat.toFixed(5) + "\nLängengrad: " + e.latlng.lng.toFixed(5));
|
|
},
|
|
},
|
|
{ separator: true },
|
|
{
|
|
text: "Reinzoomen",
|
|
icon: "img/zoom_in.png",
|
|
callback: () => map.zoomIn(),
|
|
},
|
|
{
|
|
text: "Rauszoomen",
|
|
icon: "img/zoom_out.png",
|
|
callback: () => map.zoomOut(),
|
|
},
|
|
{
|
|
text: "Hier zentrieren",
|
|
icon: "img/center_focus.png",
|
|
callback: (e) => map.panTo(e.latlng),
|
|
},
|
|
],
|
|
];
|
|
|
|
// Benutzerdefiniertes Kontextmenü anzeigen
|
|
const menu = document.createElement("div");
|
|
menu.className = "custom-context-menu";
|
|
menu.style.position = "absolute";
|
|
menu.style.left = `${event.clientX}px`;
|
|
menu.style.top = `${event.clientY}px`;
|
|
menu.style.backgroundColor = "#fff";
|
|
menu.style.border = "1px solid #ccc";
|
|
menu.style.padding = "4px";
|
|
menu.style.zIndex = "1000";
|
|
|
|
combinedContextMenuItems.forEach((item) => {
|
|
if (item.separator) {
|
|
const separator = document.createElement("hr");
|
|
menu.appendChild(separator);
|
|
} else {
|
|
const menuItem = document.createElement("div");
|
|
menuItem.style.display = "flex";
|
|
menuItem.style.alignItems = "center";
|
|
menuItem.style.cursor = "pointer";
|
|
|
|
if (item.icon) {
|
|
const icon = document.createElement("img");
|
|
icon.src = item.icon;
|
|
icon.alt = "Icon";
|
|
icon.style.width = "16px";
|
|
icon.style.height = "16px";
|
|
icon.style.marginRight = "8px";
|
|
menuItem.appendChild(icon);
|
|
}
|
|
|
|
const text = document.createElement("span");
|
|
text.textContent = item.text;
|
|
menuItem.appendChild(text);
|
|
|
|
menuItem.onclick = item.callback;
|
|
menu.appendChild(menuItem);
|
|
}
|
|
});
|
|
|
|
document.body.appendChild(menu);
|
|
|
|
const handleClickOutside = () => {
|
|
if (document.body.contains(menu)) {
|
|
document.body.removeChild(menu);
|
|
document.removeEventListener("click", handleClickOutside);
|
|
}
|
|
};
|
|
|
|
document.addEventListener("click", handleClickOutside);
|
|
});
|
|
}
|
|
});
|
|
|
|
addContextMenuToMarker(marker);
|
|
|
|
GMA.addLayer(marker);
|
|
oms.addMarker(marker);
|
|
});
|
|
|
|
map.addLayer(GMA);
|
|
|
|
return () => {
|
|
GMA.clearLayers();
|
|
map.removeLayer(GMA);
|
|
};
|
|
}, [map, markers, GisStationsMeasurements, GMA, oms, isVisible]);
|
|
};
|
|
|
|
export default useGmaMarkersLayer;
|