// Navigation functionality document.addEventListener('DOMContentLoaded', function() { // Navigation links const navLinks = document.querySelectorAll('.nav-link'); const sections = document.querySelectorAll('.content-section'); navLinks.forEach(link => { link.addEventListener('click', function(e) { e.preventDefault(); // Remove active class from all links and sections navLinks.forEach(l => l.classList.remove('active')); sections.forEach(s => s.classList.remove('active')); // Add active class to clicked link and corresponding section this.classList.add('active'); const sectionId = this.getAttribute('data-section'); document.getElementById(sectionId).classList.add('active'); }); }); // Tab functionality for student section const tabBtns = document.querySelectorAll('.tab-btn'); const tabContents = document.querySelectorAll('.tab-content'); tabBtns.forEach(btn => { btn.addEventListener('click', function() { const tabId = this.getAttribute('data-tab'); // Remove active class from all tabs and contents tabBtns.forEach(b => b.classList.remove('active')); tabContents.forEach(c => c.classList.remove('active')); // Add active class to clicked tab and corresponding content this.classList.add('active'); document.getElementById(tabId).classList.add('active'); }); }); // Search functionality const searchInputs = document.querySelectorAll('.search-input'); searchInputs.forEach(input => { input.addEventListener('input', function(e) { const searchTerm = e.target.value.toLowerCase(); const table = e.target.closest('.content-section').querySelector('.data-table'); if (table) { const rows = table.querySelectorAll('tbody tr'); rows.forEach(row => { const text = row.textContent.toLowerCase(); row.style.display = text.includes(searchTerm) ? '' : 'none'; }); } }); }); // Initialize tooltips initializeTooltips(); }); // Modal functions function showModal(modalId) { const modal = document.getElementById(modalId); const overlay = document.getElementById('modal-overlay'); if (modal && overlay) { overlay.classList.add('active'); modal.style.display = 'block'; document.body.style.overflow = 'hidden'; } } function closeModal() { const overlay = document.getElementById('modal-overlay'); const modals = overlay.querySelectorAll('.modal'); overlay.classList.remove('active'); modals.forEach(modal => { modal.style.display = 'none'; }); document.body.style.overflow = 'auto'; } // Show specific modals function showAddStudentModal() { showModal('add-student-modal'); } function showAddTeacherModal() { showModal('add-teacher-modal'); } function showLessonPlanModal() { showModal('lesson-plan-modal'); } function showAttendanceModal() { showMessage('Attendance module coming soon!', 'info'); } function showTimetableModal() { showMessage('Timetable module coming soon!', 'info'); } function showExamModal() { showMessage('Exam scheduler coming soon!', 'info'); } // Save functions function saveStudent() { showMessage('Student saved successfully!', 'success'); closeModal(); } function saveTeacher() { showMessage('Teacher saved successfully!', 'success'); closeModal(); } function saveLessonPlan() { showMessage('Lesson plan saved successfully!', 'success'); closeModal(); } // View and edit functions function viewStudentDetails(studentId) { showMessage(`Viewing details for student ${studentId}`, 'info'); } function editStudent(studentId) { showMessage(`Editing student ${studentId}`, 'info'); } // Message display function function showMessage(text, type = 'info') { // Remove existing messages const existingMessages = document.querySelectorAll('.message'); existingMessages.forEach(msg => msg.remove()); // Create new message const message = document.createElement('div'); message.className = `message ${type}`; message.innerHTML = ` ${text} `; // Insert at the top of the main content const main = document.querySelector('.main'); const container = main.querySelector('.container'); container.insertBefore(message, container.firstChild); // Auto remove after 3 seconds setTimeout(() => { message.remove(); }, 3000); } // Initialize tooltips function initializeTooltips() { const tooltipElements = document.querySelectorAll('[data-tooltip]'); tooltipElements.forEach(element => { element.addEventListener('mouseenter', function(e) { const tooltip = document.createElement('div'); tooltip.className = 'tooltip'; tooltip.textContent = this.getAttribute('data-tooltip'); document.body.appendChild(tooltip); const rect = this.getBoundingClientRect(); tooltip.style.top = rect.top - tooltip.offsetHeight - 5 + 'px'; tooltip.style.left = rect.left + (rect.width / 2) - (tooltip.offsetWidth / 2) + 'px'; }); element.addEventListener('mouseleave', function() { const tooltip = document.querySelector('.tooltip'); if (tooltip) { tooltip.remove(); } }); }); } // Close modal when clicking outside document.getElementById('modal-overlay').addEventListener('click', function(e) { if (e.target === this) { closeModal(); } }); // Keyboard navigation document.addEventListener('keydown', function(e) { // ESC key to close modal if (e.key === 'Escape') { closeModal(); } }); // Form validation function validateForm(form) { const inputs = form.querySelectorAll('input[required], select[required], textarea[required]'); let isValid = true; inputs.forEach(input => { if (!input.value.trim()) { isValid = false; input.style.borderColor = 'var(--danger-color)'; // Add error message let errorMsg = input.nextElementSibling; if (!errorMsg || !errorMsg.classList.contains('error-message')) { errorMsg = document.createElement('span'); errorMsg.className = 'error-message'; errorMsg.style.color = 'var(--danger-color)'; errorMsg.style.fontSize = '0.8rem'; errorMsg.textContent = 'This field is required'; input.parentNode.insertBefore(errorMsg, input.nextSibling); } } else { input.style.borderColor = ''; const errorMsg = input.nextElementSibling; if (errorMsg && errorMsg.classList.contains('error-message')) { errorMsg.remove(); } } }); return isValid; } // Export data functions function exportToCSV(tableId, filename) { const table = document.getElementById(tableId); if (!table) return; let csv = []; const rows = table.querySelectorAll('tr'); rows.forEach(row => { const cols = row.querySelectorAll('th, td'); const rowData = Array.from(cols).map(col => { return '"' + col.textContent.trim() + '"'; }); csv.push(rowData.join(',')); }); const csvContent = csv.join('\n'); const blob = new Blob([csvContent], { type: 'text/csv' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = filename; a.click(); window.URL.revokeObjectURL(url); } // Print function function printSection(sectionId) { const section = document.getElementById(sectionId); if (!section) return; const printWindow = window.open('', '_blank'); printWindow.document.write(` Print ${section.innerHTML} `); printWindow.document.close(); printWindow.print(); } // Data visualization (simple chart) function createSimpleChart(containerId, data) { const container = document.getElementById(containerId); if (!container) return; const maxValue = Math.max(...data.map(d => d.value)); data.forEach(item => { const barContainer = document.createElement('div'); barContainer.className = 'chart-bar'; const label = document.createElement('div'); label.className = 'bar-label'; label.textContent = item.label; const barWrapper = document.createElement('div'); barWrapper.className = 'bar-container'; const bar = document.createElement('div'); bar.className = 'bar'; bar.style.width = `${(item.value / maxValue) * 100}%`; const value = document.createElement('span'); value.textContent = item.value; barWrapper.appendChild(bar); barWrapper.appendChild(value); barContainer.appendChild(label); barContainer.appendChild(barWrapper); container.appendChild(barContainer); }); } // Initialize sample data function initializeSampleData() { // Sample enrollment data const enrollmentData = [ { label: 'Class 1-5', value: 350 }, { label: 'Class 6-8', value: 300 }, { label: 'Class 9-10', value: 250 }, { label: 'Class 11-12', value: 200 } ]; // This could be used to dynamically create charts // createSimpleChart('enrollment-chart', enrollmentData); } // Lazy loading for 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'); imageObserver.unobserve(img); } }); }); images.forEach(img => imageObserver.observe(img)); } // Performance monitoring function trackPerformance() { if ('performance' in window) { window.addEventListener('load', () => { const perfData = performance.getEntriesByType('navigation')[0]; console.log('Page load time:', perfData.loadEventEnd - perfData.loadEventStart, 'ms'); }); } } // Initialize everything when DOM is loaded document.addEventListener('DOMContentLoaded', function() { initializeSampleData(); lazyLoadImages(); trackPerformance(); }); // Service Worker registration for PWA capability if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/sw.js') .then(registration => { console.log('SW registered: ', registration); }) .catch(registrationError => { console.log('SW registration failed: ', registrationError); }); }); } // Dark mode toggle function toggleDarkMode() { document.body.classList.toggle('dark-mode'); localStorage.setItem('darkMode', document.body.classList.contains('dark-mode')); } // Load dark mode preference if (localStorage.getItem('darkMode') === 'true') { document.body.classList.add('dark-mode'); } // Auto-save form data function autoSaveForm(formId) { const form = document.getElementById(formId); if (!form) return; const inputs = form.querySelectorAll('input, select, textarea'); inputs.forEach(input => { // Load saved data const savedValue = localStorage.getItem(`${formId}_${input.name}`); if (savedValue) { input.value = savedValue; } // Save on change input.addEventListener('input', () => { localStorage.setItem(`${formId}_${input.name}`, input.value); }); }); } // Clear form data function clearFormData(formId) { const form = document.getElementById(formId); if (!form) return; const inputs = form.querySelectorAll('input, select, textarea'); inputs.forEach(input => { localStorage.removeItem(`${formId}_${input.name}`); input.value = ''; }); }