2025-01-09 14:42:38 +01:00
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
2025-01-14 17:34:30 +01:00
|
|
|
const menuItems = document.querySelectorAll('.nav-item.dropdown');
|
|
|
|
const subMenuItems = document.querySelectorAll('.dropdown-submenu');
|
2025-01-09 14:42:38 +01:00
|
|
|
|
2025-01-14 19:12:43 +01:00
|
|
|
function addMenuEventListeners(items, isTopLevel) {
|
|
|
|
items.forEach(item => {
|
|
|
|
let timeout;
|
|
|
|
|
2025-01-14 19:25:07 +01:00
|
|
|
function onMouseEnter() {
|
2025-01-14 19:12:43 +01:00
|
|
|
clearTimeout(timeout);
|
|
|
|
openMenu(item, isTopLevel);
|
2025-01-14 19:25:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseLeave() {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
closeMenu(item);
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Öffnen beim Hovern
|
|
|
|
item.addEventListener('mouseenter', onMouseEnter);
|
2025-01-14 19:12:43 +01:00
|
|
|
|
|
|
|
// Verzögertes Schließen beim Verlassen
|
2025-01-14 19:25:07 +01:00
|
|
|
item.addEventListener('mouseleave', onMouseLeave);
|
2025-01-14 19:12:43 +01:00
|
|
|
|
|
|
|
// Öffnen und Position anpassen beim Klicken
|
|
|
|
item.addEventListener('click', (e) => {
|
|
|
|
e.stopPropagation(); // Verhindert das Schließen von Menüs bei Klick
|
|
|
|
if (item.classList.contains('open')) {
|
|
|
|
closeMenu(item);
|
|
|
|
} else {
|
|
|
|
openMenu(item, isTopLevel);
|
|
|
|
}
|
|
|
|
});
|
2025-01-14 17:34:30 +01:00
|
|
|
});
|
2025-01-14 19:12:43 +01:00
|
|
|
}
|
2025-01-14 17:34:30 +01:00
|
|
|
|
2025-01-14 21:05:26 +01:00
|
|
|
function addAllMenuEventListeners() {
|
2025-01-14 20:01:48 +01:00
|
|
|
const updatedMenuItems = document.querySelectorAll('.nav-item.dropdown');
|
|
|
|
const updatedSubMenuItems = document.querySelectorAll('.dropdown-submenu');
|
|
|
|
addMenuEventListeners(updatedMenuItems, true);
|
|
|
|
addMenuEventListeners(updatedSubMenuItems, false);
|
|
|
|
}
|
|
|
|
|
2025-01-14 21:05:26 +01:00
|
|
|
addAllMenuEventListeners();
|
2025-01-14 17:08:59 +01:00
|
|
|
|
|
|
|
// Globale Klick-Listener, um Menüs zu schließen, wenn außerhalb geklickt wird
|
|
|
|
document.addEventListener('click', () => {
|
2025-01-14 17:34:30 +01:00
|
|
|
[...menuItems, ...subMenuItems].forEach(item => closeMenu(item));
|
2025-01-14 17:08:59 +01:00
|
|
|
});
|
|
|
|
|
2025-01-14 17:34:30 +01:00
|
|
|
function openMenu(item, isTopLevel) {
|
2025-01-14 17:08:59 +01:00
|
|
|
item.classList.add('open');
|
|
|
|
const submenu = item.querySelector('.dropdown-menu');
|
|
|
|
if (submenu) {
|
|
|
|
submenu.style.display = 'block';
|
|
|
|
submenu.style.opacity = '1';
|
|
|
|
submenu.style.visibility = 'visible';
|
2025-01-14 21:05:26 +01:00
|
|
|
adjustMenuPosition(submenu, item, isTopLevel);
|
2025-01-14 17:08:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeMenu(item) {
|
|
|
|
item.classList.remove('open');
|
|
|
|
const submenu = item.querySelector('.dropdown-menu');
|
|
|
|
if (submenu) {
|
|
|
|
submenu.style.display = 'none';
|
|
|
|
submenu.style.opacity = '0';
|
|
|
|
submenu.style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
}
|
2025-01-14 17:34:30 +01:00
|
|
|
|
2025-01-15 13:47:51 +01:00
|
|
|
function isSmallScreen() {
|
|
|
|
return window.innerWidth < 992; // Bootstrap-Breakpoint für 'lg'
|
|
|
|
}
|
|
|
|
|
2025-01-15 13:07:02 +01:00
|
|
|
function adjustMenuPosition(submenu, parent, isTopLevel) {
|
2025-01-14 17:34:30 +01:00
|
|
|
const rect = submenu.getBoundingClientRect();
|
|
|
|
const parentRect = parent.getBoundingClientRect();
|
|
|
|
|
|
|
|
const spaceAbove = parentRect.top;
|
|
|
|
const spaceBelow = window.innerHeight - parentRect.bottom;
|
|
|
|
const spaceLeft = parentRect.left;
|
|
|
|
const spaceRight = window.innerWidth - parentRect.right;
|
|
|
|
|
|
|
|
submenu.style.top = '';
|
|
|
|
submenu.style.bottom = '';
|
|
|
|
submenu.style.left = '';
|
|
|
|
submenu.style.right = '';
|
|
|
|
|
|
|
|
if (isTopLevel) {
|
2025-01-15 13:47:51 +01:00
|
|
|
if (isSmallScreen && spaceBelow < spaceAbove) {
|
|
|
|
// Für kleine Bildschirme: Menü direkt über dem Eltern-Element öffnen
|
|
|
|
submenu.style.top = 'auto';
|
|
|
|
submenu.style.bottom = `${parentRect.height}px`; // Direkt über dem Eltern-Element
|
|
|
|
}
|
2025-01-15 13:07:02 +01:00
|
|
|
// Top-Level-Menü
|
2025-01-15 13:47:51 +01:00
|
|
|
else if (spaceBelow < spaceAbove) {
|
2025-01-15 13:07:02 +01:00
|
|
|
submenu.style.bottom = `${window.innerHeight - parentRect.bottom - parentRect.height}px`;
|
|
|
|
submenu.style.top = 'auto';
|
|
|
|
} else {
|
|
|
|
submenu.style.top = `${parentRect.height}px`;
|
|
|
|
submenu.style.bottom = 'auto';
|
|
|
|
}
|
2025-01-14 17:34:30 +01:00
|
|
|
} else {
|
2025-01-15 13:07:02 +01:00
|
|
|
// Submenü
|
|
|
|
const prefersRight = spaceRight >= spaceLeft;
|
|
|
|
submenu.style.left = prefersRight ? '100%' : 'auto';
|
|
|
|
submenu.style.right = prefersRight ? 'auto' : '100%';
|
|
|
|
|
|
|
|
// Nach oben öffnen, wenn unten kein Platz ist
|
|
|
|
if (spaceBelow < spaceAbove) {
|
|
|
|
submenu.style.bottom = `0`;
|
|
|
|
submenu.style.top = `auto`;
|
|
|
|
} else {
|
|
|
|
submenu.style.top = `0`;
|
|
|
|
submenu.style.bottom = '${parentRect.height}px';
|
|
|
|
}
|
2025-01-14 17:34:30 +01:00
|
|
|
}
|
2025-01-15 13:07:02 +01:00
|
|
|
}
|
2025-01-14 17:52:31 +01:00
|
|
|
});
|