// Global utility functions and shared JavaScript code // Debounce function for performance optimization function debounce(func, wait) { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } // Format date to readable string function formatDate(dateString) { const options = { year: 'numeric', month: 'long', day: 'numeric' }; return new Date(dateString).toLocaleDateString('en-US', options); } // Smooth scroll to element function smoothScrollTo(elementId) { const element = document.getElementById(elementId); if (element) { element.scrollIntoView({ behavior: 'smooth', block: 'start' }); } } // Create toast notification function showToast(message, type = 'info') { const toast = document.createElement('div'); toast.className = `fixed top-4 right-4 px-6 py-3 rounded-lg shadow-lg text-white transform translate-x-full transition-transform duration-300 ${ type === 'success' ? 'bg-green-500' : type === 'error' ? 'bg-red-500' : type === 'warning' ? 'bg-yellow-500' : 'bg-blue-500' }`; toast.textContent = message; document.body.appendChild(toast); // Animate in setTimeout(() => { toast.style.transform = 'translateX(0)'; }, 10); // Remove after 3 seconds setTimeout(() => { toast.style.transform = 'translateX(100%)'; setTimeout(() => { document.body.removeChild(toast); }, 300); }, 3000); } // Check if element is in viewport function isInViewport(element) { const rect = element.getBoundingClientRect(); return ( rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth) ); } // Lazy load images function lazyLoadImages() { const images = document.querySelectorAll('img[data-src]'); const imageObserver = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if (entry.isIntersecting) { const img = entry.target; img.src = img.dataset.src; img.removeAttribute('data-src'); observer.unobserve(img); } }); }); images.forEach(img => imageObserver.observe(img)); } // Initialize tooltips for feather icons function initTooltips() { const icons = document.querySelectorAll('[data-feather]'); icons.forEach(icon => { const title = icon.getAttribute('title'); if (title) { icon.setAttribute('aria-label', title); } }); } // Theme management function initTheme() { const theme = localStorage.getItem('theme') || 'light'; document.documentElement.classList.toggle('dark', theme === 'dark'); } function toggleTheme() { const isDark = document.documentElement.classList.toggle('dark'); localStorage.setItem('theme', isDark ? 'dark' : 'light'); showToast(`Switched to ${isDark ? 'dark' : 'light'} mode`, 'info'); } // Initialize everything when DOM is loaded document.addEventListener('DOMContentLoaded', function() { // Initialize tooltips initTooltips(); // Initialize theme initTheme(); // Lazy load images lazyLoadImages(); // Add scroll animations const observerOptions = { threshold: 0.1, rootMargin: '0px 0px -50px 0px' }; const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('animate-fade-in-up'); } }); }, observerOptions); // Observe elements with animation class document.querySelectorAll('.animate-on-scroll').forEach(el => { observer.observe(el); }); // Add click handlers for smooth scrolling links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function(e) { e.preventDefault(); const targetId = this.getAttribute('href').substring(1); smoothScrollTo(targetId); }); }); // Initialize feather icons if (typeof feather !== 'undefined') { feather.replace(); } }); // Export functions for use in other modules if (typeof module !== 'undefined' && module.exports) { module.exports = { debounce, formatDate, smoothScrollTo, showToast, isInViewport, lazyLoadImages, initTooltips, initTheme, toggleTheme }; }