- Kontextmenü wird jetzt nur einmal hinzugefügt, wenn es noch nicht existiert. - Vor dem Hinzufügen wird geprüft, ob bereits Einträge existieren, um Duplikate zu vermeiden. - Kontextmenü wird entfernt, wenn außerhalb geklickt wird, um Speicherlecks zu verhindern. - Nutzung eines `Set()` für Menü-IDs, um doppelte Einträge sicher zu verhindern.
41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
// utils/contextMenuUtils.js
|
|
import { BASE_URL } from "../config/urls";
|
|
import { store } from "../redux/store"; // Redux-Store importieren
|
|
|
|
export function addContextMenuToMarker(marker) {
|
|
marker.unbindContextMenu(); // Entferne das Kontextmenü, um Duplikate zu vermeiden
|
|
|
|
const selectedDevice = store.getState().selectedDevice; // Redux-Wert abrufen
|
|
|
|
const contextMenuItems = [];
|
|
|
|
// ✅ Nur hinzufügen, wenn `selectedDevice` vorhanden ist
|
|
if (selectedDevice && marker.options?.idDevice) {
|
|
contextMenuItems.push({
|
|
text: "Station öffnen (Tab)",
|
|
icon: "/img/screen_new.png",
|
|
callback: (e) => openInNewTab(e, marker),
|
|
});
|
|
}
|
|
|
|
// Falls weitere Kontextmenü-Optionen nötig sind, können sie hier hinzugefügt werden.
|
|
|
|
marker.bindContextMenu({
|
|
contextmenu: true,
|
|
contextmenuWidth: 140,
|
|
contextmenuItems: contextMenuItems,
|
|
});
|
|
}
|
|
|
|
// Funktion zum Öffnen in einem neuen Tab
|
|
export function openInNewTab(e, marker) {
|
|
const baseUrl = BASE_URL;
|
|
console.log("baseUrl:", baseUrl);
|
|
|
|
if (marker && marker.options && marker.options.link) {
|
|
window.open(baseUrl + marker.options.link, "_blank");
|
|
} else {
|
|
console.error("Fehler: Marker hat keine gültige 'link' Eigenschaft");
|
|
}
|
|
}
|