Spaces:
Running
Running
// Initialize Mermaid diagrams | |
document.addEventListener('DOMContentLoaded', () => { | |
if (window.mermaid) { | |
mermaid.initialize({ | |
startOnLoad: true, | |
theme: 'neutral', | |
themeVariables: { | |
fontSize: '16px' | |
} | |
}); | |
} | |
}); | |
// Lazy loading for images | |
document.addEventListener('DOMContentLoaded', () => { | |
const images = document.querySelectorAll('img[loading="lazy"]'); | |
if ('loading' in HTMLImageElement.prototype) { | |
images.forEach(img => { | |
img.src = img.dataset.src; | |
}); | |
} else { | |
// Fallback for browsers that don't support lazy loading | |
const script = document.createElement('script'); | |
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/lazysizes/5.3.2/lazysizes.min.js'; | |
document.body.appendChild(script); | |
} | |
}); | |
// Print optimization | |
const optimizePrint = () => { | |
const style = document.createElement('style'); | |
style.textContent = ` | |
@media print { | |
.no-print { display: none !important; } | |
a[href]:after { content: " (" attr(href) ")"; } | |
img { max-width: 500px !important; } | |
} | |
`; | |
document.head.appendChild(style); | |
}; | |
// Initialize all components | |
const initComponents = () => { | |
// Initialize Lottie animations if present | |
if (window.lottie) { | |
document.querySelectorAll('.lottie-player').forEach(player => { | |
if (!player.hasAttribute('data-initialized')) { | |
player.setAttribute('data-initialized', 'true'); | |
// Add any specific Lottie initialization here | |
} | |
}); | |
} | |
}; | |
// Export utilities | |
window.hospitalAI = { | |
optimizePrint, | |
initComponents | |
}; | |
// Main JavaScript for Hospital AI Website | |
// Language handling | |
const languageHandler = { | |
// Available languages | |
languages: ['es', 'en'], | |
// Get current language | |
getCurrentLanguage() { | |
return document.body.getAttribute('data-language') || 'es'; | |
}, | |
// Set language | |
setLanguage(lang) { | |
if (this.languages.includes(lang)) { | |
document.body.setAttribute('data-language', lang); | |
document.body.classList.toggle('lang-en', lang === 'en'); | |
localStorage.setItem('preferred-language', lang); | |
this.updateMetaTags(lang); | |
} | |
}, | |
// Update meta tags for language | |
updateMetaTags(lang) { | |
document.documentElement.lang = lang; | |
const title = document.querySelector('title'); | |
if (title) { | |
title.textContent = title.getAttribute(`data-${lang}`) || title.textContent; | |
} | |
}, | |
// Initialize language | |
init() { | |
const savedLang = localStorage.getItem('preferred-language'); | |
if (savedLang) { | |
this.setLanguage(savedLang); | |
} | |
} | |
}; | |
// Navigation handling | |
const navigationHandler = { | |
// Update active state | |
updateActiveState() { | |
const currentPath = window.location.pathname; | |
document.querySelectorAll('nav a').forEach(link => { | |
const href = link.getAttribute('href'); | |
if (href === currentPath) { | |
link.classList.add('active'); | |
} else { | |
link.classList.remove('active'); | |
} | |
}); | |
}, | |
// Update breadcrumbs | |
updateBreadcrumbs() { | |
const breadcrumb = document.querySelector('.breadcrumb'); | |
if (breadcrumb) { | |
const currentPath = window.location.pathname; | |
const pathParts = currentPath.split('/').filter(Boolean); | |
let currentLink = ''; | |
const breadcrumbHTML = pathParts.map((part, index) => { | |
currentLink += `/${part}`; | |
return ` | |
<a href="${currentLink}">${part}</a> | |
${index < pathParts.length - 1 ? '<span>/</span>' : ''} | |
`; | |
}).join(''); | |
breadcrumb.innerHTML = ` | |
<a href="/">Home</a> | |
${pathParts.length ? '<span>/</span>' : ''} | |
${breadcrumbHTML} | |
`; | |
} | |
}, | |
// Initialize navigation | |
init() { | |
this.updateActiveState(); | |
this.updateBreadcrumbs(); | |
window.addEventListener('popstate', () => { | |
this.updateActiveState(); | |
this.updateBreadcrumbs(); | |
}); | |
} | |
}; | |
// Dark mode handling | |
const darkModeHandler = { | |
// Toggle dark mode | |
toggle() { | |
document.documentElement.classList.toggle('dark'); | |
const isDark = document.documentElement.classList.contains('dark'); | |
localStorage.setItem('dark-mode', isDark ? 'dark' : 'light'); | |
}, | |
// Initialize dark mode | |
init() { | |
const savedMode = localStorage.getItem('dark-mode'); | |
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; | |
if (savedMode === 'dark' || (!savedMode && prefersDark)) { | |
document.documentElement.classList.add('dark'); | |
} | |
} | |
}; | |
// Smooth scrolling | |
const scrollHandler = { | |
init() { | |
document.querySelectorAll('a[href^="#"]').forEach(anchor => { | |
anchor.addEventListener('click', (e) => { | |
e.preventDefault(); | |
const target = document.querySelector(anchor.getAttribute('href')); | |
if (target) { | |
target.scrollIntoView({ | |
behavior: 'smooth', | |
block: 'start' | |
}); | |
} | |
}); | |
}); | |
} | |
}; | |
// Initialize everything | |
document.addEventListener('DOMContentLoaded', () => { | |
languageHandler.init(); | |
navigationHandler.init(); | |
darkModeHandler.init(); | |
scrollHandler.init(); | |
// Close doc viewer on escape | |
document.addEventListener('keydown', (e) => { | |
if (e.key === 'Escape') DocViewer.close(); | |
}); | |
}); | |
// Global language toggle function | |
function toggleLanguage() { | |
const currentLang = languageHandler.getCurrentLanguage(); | |
const newLang = currentLang === 'es' ? 'en' : 'es'; | |
languageHandler.setLanguage(newLang); | |
} | |
// Global dark mode toggle function | |
function toggleDarkMode() { | |
darkModeHandler.toggle(); | |
} | |
// Export for use in Node.js environments | |
if (typeof module !== 'undefined' && module.exports) { | |
module.exports = { | |
languageHandler, | |
navigationHandler, | |
darkModeHandler, | |
scrollHandler | |
}; | |
} | |
// Document viewer | |
const DocViewer = { | |
open(url) { | |
const viewer = document.getElementById('docViewer'); | |
const frame = document.getElementById('docFrame'); | |
frame.src = url; | |
viewer.classList.add('active'); | |
document.body.style.overflow = 'hidden'; | |
}, | |
close() { | |
const viewer = document.getElementById('docViewer'); | |
const frame = document.getElementById('docFrame'); | |
frame.src = ''; | |
viewer.classList.remove('active'); | |
document.body.style.overflow = 'auto'; | |
} | |
}; | |
// Export for global use | |
window.openDoc = DocViewer.open; | |
window.closeDoc = DocViewer.close; |