upskill-event-manager/assets/js/hvac-navigation-robust.js
Ben 87ee4b20d7 feat: implement robust navigation system with simplified CSS and enhanced JavaScript
- Created hvac-navigation-simple.css with simplified, reliable dropdown styles
- Added hvac-navigation-robust.js with comprehensive click handlers and fallbacks
- Removed complex CSS that could conflict with theme styles
- Added multiple initialization methods and error recovery
- Enhanced debugging with console logging for troubleshooting
- Simplified mobile responsive behavior
- Added periodic visibility checks and click handler verification

This addresses user-reported navigation issues with a more robust, simple solution.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-20 23:50:09 -03:00

128 lines
No EOL
4.8 KiB
JavaScript

/**
* HVAC Navigation - Robust & Simple
* Ensures navigation dropdowns work reliably for all users
*
* @version 1.0.0
* @since 2025-08-21
*/
jQuery(document).ready(function($) {
'use strict';
console.log('HVAC Navigation: Robust navigation loading...');
// Ensure the menu system works even if other JavaScript fails
function initRobustNavigation() {
// Find all menu toggle elements
const $menuToggles = $('.hvac-trainer-menu .menu-toggle');
console.log('HVAC Navigation: Found', $menuToggles.length, 'menu toggles');
// Add click handlers to dropdown toggles
$menuToggles.off('click.hvacRobust').on('click.hvacRobust', function(e) {
e.preventDefault();
e.stopPropagation();
const $this = $(this);
const $menuItem = $this.closest('.menu-item');
const $submenu = $menuItem.find('> .sub-menu');
console.log('HVAC Navigation: Toggle clicked for', $this.text().trim());
// Close other open menus at the same level
$this.closest('ul').find('> .menu-item.open').not($menuItem).removeClass('open');
// Toggle this menu
$menuItem.toggleClass('open');
// Log the state
console.log('HVAC Navigation: Menu is now', $menuItem.hasClass('open') ? 'open' : 'closed');
});
// Ensure hamburger menu works
const $hamburger = $('#hvac-hamburger-menu, .hvac-hamburger-menu');
const $menu = $('#hvac-trainer-menu, .hvac-trainer-menu');
if ($hamburger.length && $menu.length) {
console.log('HVAC Navigation: Setting up hamburger menu');
$hamburger.off('click.hvacRobust').on('click.hvacRobust', function(e) {
e.preventDefault();
e.stopPropagation();
console.log('HVAC Navigation: Hamburger clicked');
$hamburger.toggleClass('active');
$menu.toggleClass('active');
// Update aria attributes
const isOpen = $menu.hasClass('active');
$hamburger.attr('aria-expanded', isOpen);
console.log('HVAC Navigation: Menu is now', isOpen ? 'active' : 'inactive');
});
}
// Close menu when clicking outside (on mobile)
$(document).off('click.hvacRobustOutside').on('click.hvacRobustOutside', function(e) {
if (!$(e.target).closest('.hvac-trainer-nav').length) {
if (window.innerWidth <= 992) {
$menu.removeClass('active');
$hamburger.removeClass('active');
$hamburger.attr('aria-expanded', 'false');
}
// Close all open dropdowns
$('.hvac-trainer-menu .menu-item.open').removeClass('open');
}
});
// Handle window resize
$(window).off('resize.hvacRobust').on('resize.hvacRobust', function() {
if (window.innerWidth > 992) {
// Reset mobile menu state on desktop
$menu.removeClass('active');
$hamburger.removeClass('active');
$hamburger.attr('aria-expanded', 'false');
}
});
console.log('HVAC Navigation: Robust navigation initialized successfully');
}
// Initialize immediately
initRobustNavigation();
// Reinitialize after AJAX or dynamic content changes
$(document).on('hvac-content-updated', initRobustNavigation);
// Fallback initialization after a delay
setTimeout(function() {
// Check if dropdowns are working
const $firstToggle = $('.hvac-trainer-menu .menu-toggle').first();
if ($firstToggle.length) {
// Simulate a click to test functionality
const events = $._data($firstToggle[0], 'events');
if (!events || !events.click || events.click.length === 0) {
console.log('HVAC Navigation: No click handlers found, reinitializing...');
initRobustNavigation();
}
}
}, 1000);
// Extra safety net - ensure menu visibility
function ensureMenuVisibility() {
const $activeMenu = $('.hvac-trainer-menu.active');
if ($activeMenu.length && $activeMenu.is(':hidden')) {
console.log('HVAC Navigation: Forcing menu visibility');
$activeMenu.css({
'display': 'block',
'visibility': 'visible',
'opacity': '1'
});
}
}
// Check menu visibility periodically
setInterval(ensureMenuVisibility, 2000);
});