pulse-tracker / components /vitalCircle.js
CSNET's picture
ทำแอพ HTML5 ภาษาไทย โดยมี module ดังนี้
df62a7a verified
Raw History Blame Contribute Delete
9.67 kB
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 = `
<style>
.vital-container {
text-align: center;
background: white;
border-radius: 16px;
padding: 16px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
cursor: pointer;
}
.vital-container:hover {
transform: translateY(-2px);
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}
.circle-svg {
width: 120px;
height: 120px;
transform: rotate(-90deg);
margin: 0 auto 8px;
}
.circle-background {
fill: none;
stroke: #e5e7eb;
stroke-width: 8;
}
.circle-progress {
fill: none;
stroke: ${colors.stroke};
stroke-width: 8;
stroke-linecap: round;
transition: stroke-dashoffset 0.5s ease;
}
.vital-value {
font-size: 24px;
font-weight: bold;
margin: -60px 0 8px 0;
color: #1f2937;
}
.vital-label {
font-size: 14px;
font-weight: 600;
color: #4b5563;
margin-bottom: 2px;
}
.vital-sublabel {
font-size: 12px;
color: ${colors.stroke};
}
.pulse {
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}
@keyframes pulse {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.7;
}
}
</style>
<div class="vital-container">
<svg class="circle-svg">
<circle cx="60" cy="60" r="45" class="circle-background"></circle>
<circle
cx="60" cy="60" r="45"
class="circle-progress pulse"
style="stroke-dasharray: ${circumference}; stroke-dashoffset: ${strokeDashoffset};"
></circle>
</svg>
<div class="vital-value">${this.value}</div>
<div class="vital-label">${this.label}</div>
<div class="vital-sublabel">${this.sublabel}</div>
</div>
`;
// 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 = `
<div class="modal-content">
<div class="text-center mb-4">
<div class="text-4xl font-bold ${colors.text} mb-2">${this.value}</div>
<div class="text-lg font-semibold mb-1">${this.label}</div>
<div class="text-sm text-gray-500">${this.sublabel}</div>
</div>
<div class="mb-4">
<div class="flex justify-between text-sm mb-2">
<span>ความคืบหน้า</span>
<span>${percentage}%</span>
</div>
<div class="w-full bg-gray-200 rounded-full h-3">
<div class="${colors.text.replace('text', 'bg')} h-3 rounded-full transition-all" style="width: ${percentage}%"></div>
</div>
</div>
<div class="mb-4">
<div class="flex justify-between text-sm mb-1">
<span>ปัจจุบัน</span>
<span class="font-semibold">${this.value}</span>
</div>
<div class="flex justify-between text-sm">
<span>เป้าหมาย</span>
<span class="font-semibold">${this.max}</span>
</div>
</div>
<div class="bg-gray-50 rounded-lg p-3 mb-4">
<div class="text-sm text-gray-600 mb-2">ข้อมูลเพิ่มเติม:</div>
<div class="text-sm">
${this.getDetailedInfo()}
</div>
</div>
<button class="w-full bg-indigo-600 text-white rounded-lg py-2 font-semibold hover:bg-indigo-700 transition" onclick="this.closest('.modal').remove()">
ปิด
</button>
</div>
`;
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 = `<p>${details.text}</p>`;
if (details.tips.length > 0) {
html += '<ul class="mt-2 space-y-1">';
details.tips.forEach(tip => {
html += `<li class="flex items-start"><span class="mr-2">•</span><span>${tip}</span></li>`;
});
html += '</ul>';
}
return html;
}
}
customElements.define('vital-circle', VitalCircle);