147 lines
3.8 KiB
JavaScript
Executable File
147 lines
3.8 KiB
JavaScript
Executable File
function includeHTML()
|
|
{
|
|
var z, i, elmnt, file, xhttp;
|
|
z = document.getElementsByTagName("*");
|
|
for (i = 0; i < z.length; i++)
|
|
{
|
|
elmnt = z[i];
|
|
file = elmnt.getAttribute("include-html");
|
|
if (file)
|
|
{
|
|
xhttp = new XMLHttpRequest();
|
|
xhttp.onreadystatechange = function()
|
|
{
|
|
if (this.readyState == 4)
|
|
{
|
|
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
|
|
if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
|
|
elmnt.removeAttribute("include-html");
|
|
includeHTML();
|
|
}
|
|
}
|
|
xhttp.open("GET", file, true);
|
|
xhttp.send();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Process navigation links with BASE_PATH
|
|
processNavigationLinks();
|
|
|
|
// Initialize dark mode after content is loaded
|
|
initDarkMode();
|
|
|
|
M.AutoInit();
|
|
}
|
|
|
|
function processNavigationLinks() {
|
|
// Get BASE_PATH from config (default to empty if not set)
|
|
var basePath = (typeof SIMPLEDOC_CONFIG !== 'undefined' && SIMPLEDOC_CONFIG.BASE_PATH)
|
|
? SIMPLEDOC_CONFIG.BASE_PATH
|
|
: '';
|
|
|
|
// Find all links with data-nav-link attribute
|
|
var navLinks = document.querySelectorAll('[data-nav-link]');
|
|
|
|
for (var i = 0; i < navLinks.length; i++) {
|
|
var link = navLinks[i];
|
|
var targetPath = link.getAttribute('data-nav-link');
|
|
|
|
// Set the href with BASE_PATH prepended
|
|
link.href = "/" + basePath + targetPath;
|
|
}
|
|
}
|
|
|
|
function initDarkMode() {
|
|
// Check for saved theme preference or default to 'light'
|
|
var savedTheme = localStorage.getItem('simpledoc-theme');
|
|
|
|
// If no saved preference, check system preference
|
|
if (!savedTheme) {
|
|
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
savedTheme = 'dark';
|
|
} else {
|
|
savedTheme = 'light';
|
|
}
|
|
}
|
|
|
|
// Apply the theme
|
|
setTheme(savedTheme);
|
|
|
|
// Update icons to match current theme
|
|
updateThemeIcons(savedTheme);
|
|
|
|
// Add click listener to desktop toggle button
|
|
var themeToggle = document.getElementById('theme-toggle');
|
|
if (themeToggle) {
|
|
themeToggle.addEventListener('click', function(e) {
|
|
e.preventDefault();
|
|
toggleTheme();
|
|
});
|
|
}
|
|
|
|
// Add click listener to mobile toggle button
|
|
var themeToggleMobile = document.getElementById('theme-toggle-mobile');
|
|
if (themeToggleMobile) {
|
|
themeToggleMobile.addEventListener('click', function(e) {
|
|
e.preventDefault();
|
|
toggleTheme();
|
|
});
|
|
}
|
|
|
|
// Listen for system theme changes
|
|
if (window.matchMedia) {
|
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function(e) {
|
|
if (!localStorage.getItem('simpledoc-theme')) {
|
|
setTheme(e.matches ? 'dark' : 'light');
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function setTheme(theme) {
|
|
document.documentElement.setAttribute('theme', theme);
|
|
localStorage.setItem('simpledoc-theme', theme);
|
|
updateThemeIcons(theme);
|
|
updatePrismTheme(theme);
|
|
}
|
|
|
|
function updateThemeIcons(theme) {
|
|
// Show the opposite icon (what clicking will switch to)
|
|
var iconName = theme === 'dark' ? 'light_mode' : 'dark_mode';
|
|
|
|
var themeIcon = document.getElementById('theme-icon');
|
|
if (themeIcon) {
|
|
themeIcon.textContent = iconName;
|
|
}
|
|
|
|
var themeIconMobile = document.getElementById('theme-icon-mobile');
|
|
if (themeIconMobile) {
|
|
themeIconMobile.textContent = iconName;
|
|
}
|
|
}
|
|
|
|
function toggleTheme() {
|
|
var currentTheme = document.documentElement.getAttribute('theme');
|
|
var newTheme = currentTheme === 'dark' ? 'light' : 'dark';
|
|
setTheme(newTheme);
|
|
}
|
|
|
|
function updatePrismTheme(theme) {
|
|
// Prism Tomorrow theme works well for both light and dark modes
|
|
// You can customize this if you want different syntax highlighting themes
|
|
}
|
|
|
|
// Initialize theme on page load (before includeHTML)
|
|
(function() {
|
|
var savedTheme = localStorage.getItem('simpledoc-theme');
|
|
if (!savedTheme) {
|
|
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
savedTheme = 'dark';
|
|
} else {
|
|
savedTheme = 'light';
|
|
}
|
|
}
|
|
document.documentElement.setAttribute('theme', savedTheme);
|
|
})();
|