pulse-coach / script.js
CSNET's picture
Project Name: ชีพจร (Pulse) - The Context-Aware Thai Health & Recovery Coach
9d40d50 verified
Raw History Blame Contribute Delete
5.79 kB
// Initialize app
document.addEventListener('DOMContentLoaded', function() {
initializeNavigation();
initializeProgressBars();
initializeScrollEffects();
initializeLinks();
initializeContextualData();
initializeFoodAI();
initializeIFTracker();
initializeMentalWellness();
});
// Navigation functionality
function initializeNavigation() {
const navLinks = document.querySelectorAll('.nav-link');
const sections = document.querySelectorAll('section[id]');
// Handle navigation active states
function updateActiveNav() {
let current = = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (window.scrollY >= sectionTop - 100) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${current}`) {
link.classList.add('active');
}
});
}
window.addEventListener('scroll', updateActiveNav);
// Smooth scroll for navigation links
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetSection = document.getElementById(targetId);
if (targetSection) {
targetSection.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
}
// Progress bar animations
function initializeProgressBars() {
const progressBars = document.querySelectorAll('.bg-slate-200 > div');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const bar = entry.target;
const width = bar.style.width;
bar.style.width = '0%';
setTimeout(() => {
bar.style.transition = 'width 1s ease-out';
bar.style.width = width;
}, 100);
}
});
});
progressBars.forEach(bar => {
observer.observe(bar);
});
}
// Scroll effects
function initializeScrollEffects() {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('fade-in');
}
});
}, observerOptions);
const elements = document.querySelectorAll('.bg-white');
elements.forEach(el => observer.observe(el));
}
// Interactive elements
function initializeLinks() {
// Quick action buttons
const quickActions = document.querySelectorAll('.bg-white.rounded-xl.p-4');
quickActions.forEach(button => {
button.addEventListener('click', function() {
const action = this.querySelector('span').textContent;
handleQuickAction(action);
});
});
// AI coach button
const aiButton = document.querySelector('.bg-thai-600');
if (aiButton) {
aiButton.addEventListener('click', function() {
showDetailedRecommendations();
});
}
}
// Handle quick actions
function handleQuickAction(action) {
switch(action) {
case 'ถ่ายอาหาร':
openFoodCamera();
break;
case 'เริ่มออกกำลัง':
startWorkout();
break;
case 'เริ่ม IF':
startIntermittentFasting();
break;
case 'บันทึกอารมณ์':
openMoodTracker();
break;
}
}
// API simulation functions
function openFoodCamera() {
// Simulate opening camera for food logging
alert('เปิดกล้องเพื่อถ่ายอาหาร...');
}
function startWorkout() {
// Simulate starting workout tracking
alert('เริ่มติดตามการออกกำลังกาย...');
}
function startIntermittentFasting() {
// Simulate starting IF timer
alert('เริ่มจับเวลาการอดอาหาร...');
}
function openMoodTracker() {
// Simulate opening mood tracker
alert('เปิดตัวติดตามอารมณ์...');
}
function showDetailedRecommendations() {
// Simulate showing detailed AI recommendations
alert('กำลังโหลดคำแนะนำเพิ่มเติม...');
}
// Real-time updates simulation
function simulateRealTimeUpdates() {
const recoveryScore = document.querySelector('.text-green-600');
const strainScore = document.querySelector('.text-orange-600');
setInterval(() => {
// Simulate slight variations in scores
if (recoveryScore && strainScore) {
const newRecovery = Math.floor(Math.random() * 5) + 75;
const newStrain = (Math.random() * 2 + 11).toFixed(1);
recoveryScore.textContent = newRecovery + '%';
strainScore.textContent = newStrain;
}
}, 30000); // Update every 30 seconds
}
// Initialize real-time updates
simulateRealTimeUpdates();
// Handle page visibility changes
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'visible') {
// Refresh data when user returns to page
location.reload();
}
});