class VitalCircle extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.render();
// Delay animation to ensure DOM is ready
setTimeout(() => {
this.animateValue();
}, 100);
}
render() {
const type = this.getAttribute('type') || 'recovery';
const value = this.getAttribute('value') || '0';
const label = this.getAttribute('label') || 'การวัด';
const color = this.getAttribute('color') || 'from-green-400 to-emerald-500';
const max = parseInt(this.getAttribute('max') || '100');
this.shadowRoot.innerHTML = `
`;
}
getColorValues(colorClass) {
const colorMap = {
'from-green-400 to-emerald-500': '#34d399, #10b981',
'from-orange-400 to-red-500': '#fb923c, #ef4444',
'from-blue-400 to-indigo-500': '#60a5fa, #6366f1',
'from-purple-500 to-pink-600': '#a855f7, #db2777',
'from-yellow-400 to-orange-500': '#fbbf24, #f97316'
};
return colorMap[colorClass] || colorMap['from-green-400 to-emerald-500'];
}
getPrimaryColor(colorClass) {
const colorMap = {
'from-green-400 to-emerald-500': '#34d399',
'from-orange-400 to-red-500': '#fb923c',
'from-blue-400 to-indigo-500': '#60a5fa',
'from-purple-500 to-pink-600': '#a855f7',
'from-yellow-400 to-orange-500': '#fbbf24'
};
return colorMap[colorClass] || '#34d399';
}
animateValue() {
try {
const valueElement = this.shadowRoot.getElementById('vitalValue');
if (!valueElement) return;
const endValue = parseFloat(this.getAttribute('value') || '0');
const duration = 1500;
const startTime = performance.now();
const animate = (currentTime) => {
try {
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / duration, 1);
const easeOutQuart = 1 - Math.pow(1 - progress, 4);
const currentValue = (endValue * easeOutQuart).toFixed(1);
valueElement.textContent = currentValue % 1 === 0 ? currentValue.split('.')[0] : currentValue;
if (progress < 1) {
requestAnimationFrame(animate);
} else {
this.addTrendIndicator();
}
} catch (innerError) {
console.warn('Animation frame error:', innerError);
}
};
requestAnimationFrame(animate);
} catch (error) {
console.error('Animate value error:', error);
}
}
addTrendIndicator() {
try {
const trendElement = this.shadowRoot.getElementById('trendIndicator');
if (!trendElement) return;
const trends = ['↑ 5%', '↓ 3%', '→ เท่าเดิม', '↑ 12%', '↓ 8%'];
const randomTrend = trends[Math.floor(Math.random() * trends.length)];
trendElement.textContent = randomTrend;
trendElement.className = 'trend-indicator';
if (randomTrend.includes('↓')) {
trendElement.classList.add('down');
}
} catch (error) {
console.error('Trend indicator error:', error);
}
}
showLoading() {
const container = this.shadowRoot.getElementById('vitalContainer');
container.classList.add('loading');
}
hideLoading() {
const container = this.shadowRoot.getElementById('vitalContainer');
container.classList.remove('loading');
}
updateValue(newValue) {
this.setAttribute('value', newValue);
this.animateValue();
}
}
customElements.define('vital-circle', VitalCircle);