/** * وظيفة تهيئة جدول المحتويات (TOC) */ function initTOC() { const tocContainer = document.getElementById('toc-list'); const content = document.querySelector('.content'); if (!tocContainer || !content) return; tocContainer.innerHTML = ''; const headings = content.querySelectorAll('h2, h3'); headings.forEach((heading, index) => { const id = `heading-${index}`; heading.setAttribute('id', id); const li = document.createElement('li'); if (heading.tagName === 'H3') { li.style.marginRight = '20px'; li.style.listStyleType = 'circle'; } const a = document.createElement('a'); a.href = `#${id}`; a.textContent = heading.textContent || ''; a.className = 'toc-link'; li.appendChild(a); tocContainer.appendChild(li); }); } /** * وظيفة إدارة القائمة الجانبية للموبايل */ function initMobileSidebar() { const toggle = document.querySelector('.mobile-toggle'); const sidebar = document.querySelector('.sidebar'); const closeBtn = document.querySelector('.close-sidebar'); if (toggle && sidebar) { toggle.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); sidebar.classList.add('active'); document.body.style.overflow = 'hidden'; }); } if (closeBtn && sidebar) { closeBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); sidebar.classList.remove('active'); document.body.style.overflow = 'auto'; }); } // إغلاق القائمة عند النقر خارجها document.addEventListener('click', (e) => { const target = e.target as HTMLElement; if (sidebar && sidebar.classList.contains('active')) { if (!sidebar.contains(target) && !toggle?.contains(target)) { sidebar.classList.remove('active'); document.body.style.overflow = 'auto'; } } }); } /** * وظيفة إدارة البوب آب (Modal) الخاص بالتعريف بالمستشار */ function initAboutModal() { const modal = document.getElementById('about-modal'); const openBtn = document.getElementById('open-about-modal'); const closeBtn = document.querySelector('.close-modal'); if (!modal || !openBtn) return; openBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); modal.style.display = 'flex'; document.body.style.overflow = 'hidden'; }); if (closeBtn) { closeBtn.addEventListener('click', (e) => { e.preventDefault(); modal.style.display = 'none'; document.body.style.overflow = 'auto'; }); } // إغلاق المودال عند النقر على الخلفية modal.addEventListener('click', (e) => { if (e.target === modal) { modal.style.display = 'none'; document.body.style.overflow = 'auto'; } }); } /** * تشغيل كافة الوظائف عند جاهزية الصفحة */ function startApp() { initTOC(); initMobileSidebar(); initAboutModal(); } // التأكد من تشغيل الكود سواء تم تحميل الصفحة أو كان السكريبت يعمل كموديول if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', startApp); } else { startApp(); }