/** * ========================================================================== * MODAL CONTROLLER - XỬ LÝ ĐÓNG MỞ POPUP * ========================================================================== */ const ModalController = { // Mở popup theo Modal ID open: function(modalId) { const modal = document.getElementById(modalId); if (!modal) return; modal.classList.add('active'); document.body.classList.add('modal-open'); // Trigger onOpen callbacks nếu có if (modalId === 'pdfModal' && window.PDFViewer) { window.PDFViewer.init(); } else if (modalId === 'videoModal' && window.VideoPlayer) { window.VideoPlayer.init(); } }, // Đóng popup theo Modal ID hoặc đóng tất cả close: function(modalId) { if (modalId) { const modal = document.getElementById(modalId); if (modal) { modal.classList.remove('active'); // Nếu là video modal, dừng video đang phát if (modalId === 'videoModal' && window.VideoPlayer) { window.VideoPlayer.pause(); } } } else { document.querySelectorAll('.modal-overlay').forEach(modal => { modal.classList.remove('active'); }); if (window.VideoPlayer) { window.VideoPlayer.pause(); } } // Kiểm tra nếu không còn modal nào đang mở thì bỏ lock cuộn trang const activeModals = document.querySelectorAll('.modal-overlay.active'); if (activeModals.length === 0) { document.body.classList.remove('modal-open'); } }, // Gán sự kiện click bên ngoài và phím ESC setupEventListeners: function() { // Lắng nghe click vào nút đóng [data-close-modal] document.querySelectorAll('[data-close-modal]').forEach(button => { button.addEventListener('click', (e) => { const modal = button.closest('.modal-overlay'); if (modal) { ModalController.close(modal.id); } }); }); // Click vào vùng mờ (backdrop) bên ngoài khung modal để đóng document.querySelectorAll('.modal-overlay').forEach(modal => { modal.addEventListener('click', (e) => { if (e.target === modal) { ModalController.close(modal.id); } }); }); // Nhấn phím Escape (ESC) để đóng popup document.addEventListener('keydown', (e) => { if (e.key === 'Escape') { ModalController.close(); } }); // Nút mở Popup PDF const openPdfBtn = document.getElementById('btnOpenPdfModal'); if (openPdfBtn) { openPdfBtn.addEventListener('click', () => { ModalController.open('pdfModal'); }); } // Nút mở Popup Video const openVideoBtn = document.getElementById('btnOpenVideoModal'); if (openVideoBtn) { openVideoBtn.addEventListener('click', () => { ModalController.open('videoModal'); }); } } };