| const initUi = require('./ui'); |
| const initSearch = require('./search'); |
| const initRouting = require('./routing'); |
|
|
| function detectHomePageId() { |
| |
| |
| try { |
| const config = |
| window.MeNav && typeof window.MeNav.getConfig === 'function' |
| ? window.MeNav.getConfig() |
| : null; |
| const injectedHomePageId = |
| config && config.data && config.data.homePageId ? String(config.data.homePageId).trim() : ''; |
| if (injectedHomePageId) return injectedHomePageId; |
| const nav = |
| config && config.data && Array.isArray(config.data.navigation) |
| ? config.data.navigation |
| : null; |
| const firstId = nav && nav[0] && nav[0].id ? String(nav[0].id).trim() : ''; |
| if (firstId) return firstId; |
| } catch (error) { |
| |
| } |
|
|
| |
| const firstNavItem = document.querySelector('.nav-item[data-page]'); |
| if (firstNavItem) { |
| const id = String(firstNavItem.getAttribute('data-page') || '').trim(); |
| if (id) return id; |
| } |
|
|
| |
| const firstPage = document.querySelector('.page[id]'); |
| if (firstPage && firstPage.id) return firstPage.id; |
|
|
| return 'home'; |
| } |
|
|
| document.addEventListener('DOMContentLoaded', () => { |
| const homePageId = detectHomePageId(); |
|
|
| const state = { |
| homePageId, |
| currentPageId: homePageId, |
| isInitialLoad: true, |
| isSidebarOpen: false, |
| isLightTheme: false, |
| isSidebarCollapsed: false, |
| pages: null, |
| currentSearchEngine: 'local', |
| isSearchActive: false, |
| searchIndex: { |
| initialized: false, |
| items: [], |
| }, |
| }; |
|
|
| |
| const searchInput = document.getElementById('search'); |
| const searchBox = document.querySelector('.search-box'); |
| const searchResultsPage = document.getElementById('search-results'); |
| const searchSections = searchResultsPage.querySelectorAll('.search-section'); |
|
|
| |
| const searchEngineToggle = document.querySelector('.search-engine-toggle'); |
| const searchEngineToggleIcon = searchEngineToggle |
| ? searchEngineToggle.querySelector('.search-engine-icon') |
| : null; |
| const searchEngineToggleLabel = searchEngineToggle |
| ? searchEngineToggle.querySelector('.search-engine-label') |
| : null; |
| const searchEngineDropdown = document.querySelector('.search-engine-dropdown'); |
| const searchEngineOptions = document.querySelectorAll('.search-engine-option'); |
|
|
| |
| const menuToggle = document.querySelector('.menu-toggle'); |
| const searchToggle = document.querySelector('.search-toggle'); |
| const sidebar = document.querySelector('.sidebar'); |
| const searchContainer = document.querySelector('.search-container'); |
| const overlay = document.querySelector('.overlay'); |
|
|
| |
| const sidebarToggle = document.querySelector('.sidebar-toggle'); |
| const content = document.querySelector('.content'); |
|
|
| |
| const themeToggle = document.querySelector('.theme-toggle'); |
| const themeIcon = themeToggle.querySelector('i'); |
|
|
| const dom = { |
| searchInput, |
| searchBox, |
| searchResultsPage, |
| searchSections, |
| searchEngineToggle, |
| searchEngineToggleIcon, |
| searchEngineToggleLabel, |
| searchEngineDropdown, |
| searchEngineOptions, |
| menuToggle, |
| searchToggle, |
| sidebar, |
| searchContainer, |
| overlay, |
| sidebarToggle, |
| content, |
| themeToggle, |
| themeIcon, |
| }; |
|
|
| const ui = initUi(state, dom); |
| const search = initSearch(state, dom); |
|
|
| initRouting(state, dom, { ui, search }); |
| }); |
|
|