class CustomNavbar extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
// Initialize mobile menu
this.initializeMobileMenu();
// Update active link based on scroll
this.initializeScrollSpy();
// Replace feather icons
setTimeout(() => {
if (window.feather) {
window.feather.replace();
}
}, 100);
}
initializeMobileMenu() {
const mobileMenuBtn = this.shadowRoot.getElementById('mobileMenuBtn');
const mobileMenu = this.shadowRoot.getElementById('mobileMenu');
if (mobileMenuBtn && mobileMenu) {
mobileMenuBtn.addEventListener('click', () => {
mobileMenu.classList.toggle('active');
const icon = mobileMenuBtn.querySelector('i');
if (mobileMenu.classList.contains('active')) {
icon.setAttribute('data-feather', 'x');
} else {
icon.setAttribute('data-feather', 'menu');
}
window.feather.replace();
});
// Close menu when clicking on a link
const mobileLinks = mobileMenu.querySelectorAll('.mobile-nav-link');
mobileLinks.forEach(link => {
link.addEventListener('click', () => {
mobileMenu.classList.remove('active');
const icon = mobileMenuBtn.querySelector('i');
icon.setAttribute('data-feather', 'menu');
window.feather.replace();
});
});
}
}
initializeScrollSpy() {
const navLinks = this.shadowRoot.querySelectorAll('.nav-link, .mobile-nav-link');
const sections = ['ndvi-map', 'field-details', 'recommendations'];
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const id = entry.target.id;
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${id}`) {
link.classList.add('active');
}
});
}
});
}, { threshold: 0.5 });
sections.forEach(section => {
const element = document.getElementById(section);
if (element) observer.observe(element);
});
}
}
customElements.define('custom-navbar', CustomNavbar);