// Filename: frontend/js/layout/base.js // [模块导入] import { themeManager } from '../components/themeManager.js'; import { apiFetch, apiFetchJson } from '../services/api.js'; /** * 激活当前页面的侧边栏导航项。 */ function initActiveNav() { const currentPath = window.location.pathname; const navLinks = document.querySelectorAll('.nav-link'); navLinks.forEach(link => { const linkPath = link.getAttribute('href'); if (linkPath && linkPath !== '/' && currentPath.startsWith(linkPath)) { const wrapper = link.closest('.nav-item-wrapper'); if (wrapper) { wrapper.dataset.active = 'true'; } } }); } /** * 将核心 API 函数挂载到 window 对象,以便在需要时进行全局访问或调试。 * 这充当了模块化世界和全局作用域之间的“桥梁”。 */ function bridgeApiToGlobal() { window.apiFetch = apiFetch; window.apiFetchJson = apiFetchJson; console.log('[Bridge] apiFetch and apiFetchJson are now globally available.'); } /** * 初始化所有与 base.html 布局相关的全局UI元素和事件监听器。 */ function initLayout() { console.log('[Init] Executing global layout JavaScript...'); // 1. 初始化侧边栏导航状态 initActiveNav(); // 2. 初始化主题管理器 themeManager.init(); // 3. 建立 API 函数的全局桥梁 bridgeApiToGlobal(); // 4. (预留) 在此处添加未来可能的其他布局逻辑,例如侧边栏的折叠/展开功能 // const sidebarToggle = document.getElementById('sidebar-toggle'); // if (sidebarToggle) { ... } } // 默认导出主初始化函数 export default initLayout;