// Dashboard functionality // Wait for the DOM to be fully loaded before running any code document.addEventListener("DOMContentLoaded", function() { // More reliable login detection - check for dashboard-specific elements const isLoggedIn = ( // Check for common dashboard elements !!document.querySelector('.dashboard-content') || !!document.querySelector('.side-navbar') || !!document.querySelector('#user-dropdown') || !!document.querySelector('.user-menu') || !!document.querySelector('.nav-link[href*="logout"]') || // Check URL patterns that would only be accessible when logged in window.location.pathname.includes('/dashboard') || window.location.pathname.includes('/profile') || window.location.pathname.includes('/app/') || // Explicit authentication flag window.IS_AUTHENTICATED === true ); if (isLoggedIn) { // Call with a slight delay to ensure page is fully loaded setTimeout(function() { checkWeakPassword(); }, 100); } // Initialize all components initializeComponents(); // Set up event handlers setupEventHandlers(); // Get device token for notifications if (typeof window.getDeviceToken === 'function' && isLoggedIn) { window.getDeviceToken(); } // Add global keyboard event handler for Escape key document.addEventListener('keydown', function(event) { if (event.key === 'Escape' || event.keyCode === 27) { closeAllModals(); } }); // Initialize responsive modals initResponsiveModals(); // Make all tables responsive makeAllTablesResponsive(); // Handle long content handleLongContent(); // Process any dynamically added tables const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.type === 'childList') { makeAllTablesResponsive(); handleLongContent(); } }); }); // Start observing the document for added nodes observer.observe(document.body, { childList: true, subtree: true }); }); // Function to initialize all dashboard components function initializeComponents() { // Initialize any Bootstrap components if (typeof bootstrap !== 'undefined') { // Initialize tooltips var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')); tooltipTriggerList.map(function(tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl); }); // Initialize popovers var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]')); popoverTriggerList.map(function(popoverTriggerEl) { return new bootstrap.Popover(popoverTriggerEl); }); } // Handle image loading errors document.querySelectorAll('img').forEach(function(img) { img.addEventListener("error", function() { this.onerror = null; this.src = "/static/img/user.png"; }); }); // Setup star rating if elements exist if (typeof window.setupStarRating === 'function') { window.setupStarRating(); } // Setup tabs if elements exist if (typeof window.setupCustomTabs === 'function') { window.setupCustomTabs(); } // Setup feedback tabs if elements exist if (typeof window.setupFeedbackTabs === 'function') { window.setupFeedbackTabs(); } // Initialize Bootstrap components if (typeof window.initializeBootstrapComponents === 'function') { window.initializeBootstrapComponents(); } // Set up form validation if (typeof window.setupRatingFormValidation === 'function') { window.setupRatingFormValidation(); } // Ensure notifications.js is loaded if (typeof window.showNotification !== 'function') { // Dynamically load the notifications script if not already available const script = document.createElement('script'); script.src = '/static/js/notifications.js'; script.async = true; document.head.appendChild(script); } // Ensure notification toast container exists if (!document.getElementById('notificationToast')) { const toastContainer = document.createElement('div'); toastContainer.innerHTML = `