Path : /home/vishqocm/pcib.in/admin/assets/js/
File Upload :
Current File : /home/vishqocm/pcib.in/admin/assets/js/modal-fix.js

/**
 * Fix for Bootstrap 5 modal issues
 */
document.addEventListener('DOMContentLoaded', function() {
    // Initialize all modals to ensure they work properly
    var modalElements = document.querySelectorAll('.modal');
    modalElements.forEach(function(modalElement) {
        // Create modal instance if it doesn't exist
        try {
            var modalInstance = new bootstrap.Modal(modalElement);
            console.log('Modal initialized: ' + modalElement.id);
        } catch (e) {
            console.error('Error initializing modal: ' + modalElement.id, e);
        }
    });
    
    // Make sure modal triggers work properly
    var modalTriggers = document.querySelectorAll('[data-bs-toggle="modal"]');
    modalTriggers.forEach(function(trigger) {
        trigger.addEventListener('click', function(e) {
            var targetSelector = this.getAttribute('data-bs-target');
            if (!targetSelector) return;
            
            var modalElement = document.querySelector(targetSelector);
            if (!modalElement) {
                console.error('Modal not found: ' + targetSelector);
                return;
            }
            
            try {
                var modal = bootstrap.Modal.getInstance(modalElement);
                if (!modal) {
                    // Create if not exists
                    modal = new bootstrap.Modal(modalElement);
                }
                modal.show();
                console.log('Modal shown: ' + targetSelector);
            } catch (e) {
                console.error('Error showing modal: ' + targetSelector, e);
            }
        });
    });
    
    // Fix for view and delete modals
    var viewButtons = document.querySelectorAll('.btn-info[data-bs-toggle="modal"]');
    var deleteButtons = document.querySelectorAll('.btn-danger[data-bs-toggle="modal"]');
    
    // Add more specific event listeners
    function setupModalButton(button) {
        button.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            
            var targetSelector = this.getAttribute('data-bs-target');
            if (!targetSelector) return;
            
            // Try jQuery method first (if jQuery is loaded)
            if (typeof $ !== 'undefined') {
                try {
                    $(targetSelector).modal('show');
                    return;
                } catch (e) {
                    console.log('jQuery modal show failed, trying vanilla JS');
                }
            }
            
            // Fallback to vanilla JS
            var modalElement = document.querySelector(targetSelector);
            if (!modalElement) return;
            
            try {
                var bsModal = new bootstrap.Modal(modalElement);
                bsModal.show();
            } catch (e) {
                console.error('Failed to show modal with vanilla JS', e);
            }
        });
    }
    
    viewButtons.forEach(setupModalButton);
    deleteButtons.forEach(setupModalButton);
});