class VitalCircle extends HTMLElement {
constructor() {
super();
this.value = this.getAttribute('value') || 0;
this.max = this.getAttribute('max') || 100;
this.label = this.getAttribute('label') || '';
this.sublabel = this.getAttribute('sublabel') || '';
this.color = this.getAttribute('color') || 'blue';
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
}
static get observedAttributes() {
return ['value', 'max', 'label', 'sublabel', 'color'];
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) {
this[name] = newValue;
this.render();
}
}
getColorClasses() {
const colors = {
green: {
stroke: '#10b981',
fill: '#10b98120',
text: 'text-green-600',
bg: 'bg-green-50'
},
yellow: {
stroke: '#f59e0b',
fill: '#f59e0b20',
text: 'text-yellow-600',
bg: 'bg-yellow-50'
},
red: {
stroke: '#ef4444',
fill: '#ef444420',
text: 'text-red-600',
bg: 'bg-red-50'
},
blue: {
stroke: '#3b82f6',
fill: '#3b82f620',
text: 'text-blue-600',
bg: 'bg-blue-50'
},
purple: {
stroke: '#8b5cf6',
fill: '#8b5cf620',
text: 'text-purple-600',
bg: 'bg-purple-50'
}
};
return colors[this.color] || colors.blue;
}
render() {
const colors = this.getColorClasses();
const percentage = (this.value / this.max) * 100;
const circumference = 2 * Math.PI * 45;
const strokeDashoffset = circumference - (percentage / 100) * circumference;
this.shadowRoot.innerHTML = `
${this.value}
${this.label}
${this.sublabel}
`;
// Add click event
this.shadowRoot.querySelector('.vital-container').addEventListener('click', () => {
this.showDetails();
});
}
showDetails() {
// Create detail modal
const modal = document.createElement('div');
modal.className = 'modal';
modal.style.display = 'flex';
const colors = this.getColorClasses();
const percentage = Math.round((this.value / this.max) * 100);
modal.innerHTML = `
${this.value}
${this.label}
${this.sublabel}
ความคืบหน้า
${percentage}%
ปัจจุบัน
${this.value}
เป้าหมาย
${this.max}
ข้อมูลเพิ่มเติม:
${this.getDetailedInfo()}
`;
document.body.appendChild(modal);
// Close modal when clicking outside
modal.addEventListener('click', function(e) {
if (e.target === modal) {
modal.remove();
}
});
}
getDetailedInfo() {
const info = {
Recovery: {
text: 'Recovery Score คือการวัดความพร้อมของร่างกาย คำนวณจาก HRV, RHR, และคุณภาพการนอน',
tips: [
'70%+ = พร้อมฝึกหนัก',
'40-69% = ฝึกปานกลาง',
'ต่ำกว่า 40% = พักผ่อน'
]
},
Strain: {
text: 'Strain คือปริมาณของการใช้พลังงานรวมทั้งวัน (0-21)',
tips: [
'0-9 = การฟื้นตัว',
'10-13 = ฝึกปานกลาง',
'14-17 = ฝึกหนัก',
'18+ = ฝึกหนักมาก'
]
},
Sleep: {
text: 'ชั่วโมงการนอนที่แนะนำสำหรับผู้ใหญ่คือ 7-9 ชั่วโมง',
tips: [
'คุณภาพสำคัญกว่าปริมาณ',
'Deep Sleep ควรเป็น 15-20%',
'REM Sleep ควรเป็น 20-25%'
]
}
};
const details = info[this.label] || { text: '', tips: [] };
let html = `${details.text}
`;
if (details.tips.length > 0) {
html += '';
details.tips.forEach(tip => {
html += `- •${tip}
`;
});
html += '
';
}
return html;
}
}
customElements.define('vital-circle', VitalCircle);