/**
* TenderRadar Navigation Component
* Shared navbar for all app pages (dashboard, profile, alerts)
*/
class NavBar {
constructor() {
this.navElement = null;
this.isLoggedIn = isAuthenticated();
this.userInfo = getUserInfo();
}
/**
* Initialize and inject navbar into page
*/
init() {
this.createNavBar();
this.attachEventListeners();
this.highlightActivePage();
}
/**
* Create navbar HTML structure
*/
createNavBar() {
const nav = document.createElement('nav');
nav.className = 'app-navbar';
nav.innerHTML = this.getNavBarHTML();
// Insert at the top of body
document.body.insertBefore(nav, document.body.firstChild);
this.navElement = nav;
}
/**
* Get navbar HTML based on auth state
*/
getNavBarHTML() {
if (this.isLoggedIn && this.userInfo) {
return this.getAuthenticatedNavHTML();
} else {
return this.getUnauthenticatedNavHTML();
}
}
/**
* HTML for authenticated users
*/
getAuthenticatedNavHTML() {
const userEmail = this.userInfo.email || 'User';
return `
`;
}
/**
* HTML for unauthenticated users
*/
getUnauthenticatedNavHTML() {
return `
`;
}
/**
* Attach event listeners to navbar
*/
attachEventListeners() {
if (!this.isLoggedIn) return;
// Mobile menu toggle
const mobileToggle = this.navElement.querySelector('.mobile-menu-toggle');
const navWrapper = this.navElement.querySelector('.nav-menu-wrapper');
mobileToggle.addEventListener('click', () => {
navWrapper.classList.toggle('active');
mobileToggle.classList.toggle('active');
});
// User menu dropdown
const userToggle = this.navElement.querySelector('.user-menu-toggle');
const userDropdown = this.navElement.querySelector('.user-dropdown');
userToggle.addEventListener('click', (e) => {
e.stopPropagation();
userDropdown.style.display =
userDropdown.style.display === 'none' ? 'block' : 'none';
});
// Close dropdown when clicking elsewhere
document.addEventListener('click', () => {
userDropdown.style.display = 'none';
});
// Logout button
const logoutBtn = this.navElement.querySelector('.logout-btn');
logoutBtn.addEventListener('click', (e) => {
e.preventDefault();
logout();
});
// Close mobile menu when clicking a link
const navLinks = this.navElement.querySelectorAll('.nav-link');
navLinks.forEach(link => {
link.addEventListener('click', () => {
navWrapper.classList.remove('active');
mobileToggle.classList.remove('active');
});
});
}
/**
* Highlight the current page's nav link
*/
highlightActivePage() {
const currentPath = window.location.pathname;
const navLinks = this.navElement.querySelectorAll('.nav-link');
navLinks.forEach(link => {
const href = link.getAttribute('href');
if (currentPath.includes(href.replace('.html', ''))) {
link.classList.add('active');
} else {
link.classList.remove('active');
}
});
}
}
// Auto-initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
const navbar = new NavBar();
navbar.init();
});
} else {
const navbar = new NavBar();
navbar.init();
}