commit beb70eea3e80cee4b54d7f679b27d98a8d9e921c
parent bb2182ba2f0be1f82e5d2b2f841b47a497a7bd40
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Wed, 7 Oct 2026 17:21:06 +0530
parent bb2182ba2f0be1f82e5d2b2f841b47a497a7bd40
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Wed, 7 Oct 2026 17:21:06 +0530
Clean up remaining TVA control hot spots
1 file changed, 169 insertions(+), 15 deletions(-)
diff --git a/tva-control.js b/tva-control.js @@ -68,14 +68,45 @@ plotIcon: null }; + function strokePath(ctx, points, close = false) { + ctx.beginPath(); + points.forEach(([x, y], i) => i ? ctx.lineTo(x, y) : ctx.moveTo(x, y)); + if (close) ctx.closePath(); + ctx.stroke(); + } + function drawIcon(ctx, type, x, y, color) { - ctx.save(); ctx.strokeStyle = color; ctx.fillStyle = color; ctx.lineWidth = 1.25; ctx.lineCap = 'round'; ctx.lineJoin = 'round'; - if (type === 'invoice') { ctx.strokeRect(x - 5, y + 1, 10, 11.5); [4, 7, 10].forEach(o => { ctx.beginPath(); ctx.moveTo(x - 3, y + o); ctx.lineTo(x + 3, y + o); ctx.stroke(); }); } - else if (type === 'person') { ctx.beginPath(); ctx.arc(x, y + 3.8, 2.8, 0, Math.PI * 2); ctx.stroke(); ctx.beginPath(); ctx.moveTo(x - 5, y + 12); ctx.quadraticCurveTo(x, y + 7.6, x + 5, y + 12); ctx.stroke(); } - else if (type === 'calendar') { ctx.strokeRect(x - 5, y + 3, 10, 10); ctx.beginPath(); ctx.moveTo(x - 5, y + 6); ctx.lineTo(x + 5, y + 6); ctx.moveTo(x - 2.8, y + 1); ctx.lineTo(x - 2.8, y + 4); ctx.moveTo(x + 2.8, y + 1); ctx.lineTo(x + 2.8, y + 4); ctx.stroke(); ctx.fillRect(x - 2.8, y + 8, 1.3, 1.3); ctx.fillRect(x + 1.5, y + 8, 1.3, 1.3); } - else if (type === 'deposit') { ctx.strokeRect(x - 5, y + 4, 10, 8); ctx.beginPath(); ctx.moveTo(x - 6, y + 4); ctx.lineTo(x, y + .5); ctx.lineTo(x + 6, y + 4); ctx.moveTo(x - 3, y + 7); ctx.lineTo(x + 3, y + 7); ctx.moveTo(x - 3, y + 10); ctx.lineTo(x + 3, y + 10); ctx.stroke(); } - else if (type === 'hourglass') { ctx.beginPath(); ctx.moveTo(x - 5, y + 1); ctx.lineTo(x + 5, y + 1); ctx.lineTo(x - 5, y + 12.5); ctx.lineTo(x + 5, y + 12.5); ctx.closePath(); ctx.stroke(); } - else { ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x - 5.5, y + 10.5); ctx.lineTo(x + 5.5, y + 10.5); ctx.closePath(); ctx.stroke(); ctx.beginPath(); ctx.moveTo(x, y + 3.5); ctx.lineTo(x, y + 7.2); ctx.stroke(); ctx.beginPath(); ctx.arc(x, y + 9.2, .6, 0, Math.PI * 2); ctx.fill(); } + ctx.save(); + ctx.strokeStyle = color; + ctx.fillStyle = color; + ctx.lineWidth = 1.25; + ctx.lineCap = 'round'; + ctx.lineJoin = 'round'; + + if (type === 'invoice') { + ctx.strokeRect(x - 5, y + 1, 10, 11.5); + [4, 7, 10].forEach(o => strokePath(ctx, [[x - 3, y + o], [x + 3, y + o]])); + } else if (type === 'person') { + ctx.beginPath(); ctx.arc(x, y + 3.8, 2.8, 0, Math.PI * 2); ctx.stroke(); + ctx.beginPath(); ctx.moveTo(x - 5, y + 12); ctx.quadraticCurveTo(x, y + 7.6, x + 5, y + 12); ctx.stroke(); + } else if (type === 'calendar') { + ctx.strokeRect(x - 5, y + 3, 10, 10); + strokePath(ctx, [[x - 5, y + 6], [x + 5, y + 6]]); + strokePath(ctx, [[x - 2.8, y + 1], [x - 2.8, y + 4]]); + strokePath(ctx, [[x + 2.8, y + 1], [x + 2.8, y + 4]]); + ctx.fillRect(x - 2.8, y + 8, 1.3, 1.3); ctx.fillRect(x + 1.5, y + 8, 1.3, 1.3); + } else if (type === 'deposit') { + ctx.strokeRect(x - 5, y + 4, 10, 8); + strokePath(ctx, [[x - 6, y + 4], [x, y + .5], [x + 6, y + 4]]); + strokePath(ctx, [[x - 3, y + 7], [x + 3, y + 7]]); + strokePath(ctx, [[x - 3, y + 10], [x + 3, y + 10]]); + } else if (type === 'hourglass') { + strokePath(ctx, [[x - 5, y + 1], [x + 5, y + 1], [x - 5, y + 12.5], [x + 5, y + 12.5]], true); + } else { + strokePath(ctx, [[x, y], [x - 5.5, y + 10.5], [x + 5.5, y + 10.5]], true); + strokePath(ctx, [[x, y + 3.5], [x, y + 7.2]]); + ctx.beginPath(); ctx.arc(x, y + 9.2, .6, 0, Math.PI * 2); ctx.fill(); + } ctx.restore(); } window.TvaControlInternals = { getTrend, isStale, mergeData, normalizeAreas }; @@ -149,7 +180,36 @@ function formatPoint(p) { const v = getValue(p), d = p.display || {}, dec = Number.isInteger(d.decimals) ? d.decimals : 0; const s = Number.isInteger(v) || dec === 0 ? String(Math.round(v)) : v.toFixed(dec); return `${d.prefix || ''}${s}${d.suffix || ''}`; } function trend(i) { const v = values(); return getTrend(previousMetricValues && previousMetricValues[i], v[i], number(state.trendThreshold, 15)); } function zoomBounds(vs) { let min = Math.min(0, ...vs), max = Math.max(0, ...vs); if (min === max) { min -= 10; max += 10; } const pad = Math.max(8, (max - min) * .24); return { min: Math.floor(min - pad), max: Math.ceil(max + pad) }; } - function gradient(ctx) { const v = values(), c0 = metricColor(ctx.p0DataIndex, v[ctx.p0DataIndex]), c1 = metricColor(ctx.p1DataIndex, v[ctx.p1DataIndex]); if (c0 === c1 || !ctx.p0 || !ctx.p1) return c1; const g = ctx.chart.ctx.createLinearGradient(ctx.p0.x, ctx.p0.y, ctx.p1.x, ctx.p1.y); const stale0 = isMetricStale(ctx.p0DataIndex), stale1 = isMetricStale(ctx.p1DataIndex); if (stale0 || stale1) { const base = staleColor(false), full = staleColor(true), width = 0.12, phase = stalePhase; [[0, base], [Math.max(0, phase - width), base], [phase, full], [Math.min(1, phase + width), base], [1, base]].sort((a, b) => a[0] - b[0]).forEach(([stop, color]) => g.addColorStop(stop, color)); return g; } g.addColorStop(0, c0); const l0 = breachedLevel(ctx.p0DataIndex, v[ctx.p0DataIndex]); const l1 = breachedLevel(ctx.p1DataIndex, v[ctx.p1DataIndex]); const dom0 = l0 && number(l0.dominance, 0); const dom1 = l1 && number(l1.dominance, 0); if (dom0) g.addColorStop(dom0, c0); else if (dom1) g.addColorStop(1 - dom1, c1); else g.addColorStop(.5, state.uiColors.neutralGradientMid); g.addColorStop(1, c1); return g; } + function gradient(ctx) { + const v = values(); + const c0 = metricColor(ctx.p0DataIndex, v[ctx.p0DataIndex]); + const c1 = metricColor(ctx.p1DataIndex, v[ctx.p1DataIndex]); + if (c0 === c1 || !ctx.p0 || !ctx.p1) return c1; + + const g = ctx.chart.ctx.createLinearGradient(ctx.p0.x, ctx.p0.y, ctx.p1.x, ctx.p1.y); + const stale0 = isMetricStale(ctx.p0DataIndex); + const stale1 = isMetricStale(ctx.p1DataIndex); + if (stale0 || stale1) { + const base = staleColor(false); + const full = staleColor(true); + const width = 0.12; + [[0, base], [Math.max(0, stalePhase - width), base], [stalePhase, full], [Math.min(1, stalePhase + width), base], [1, base]] + .sort((a, b) => a[0] - b[0]) + .forEach(([stop, color]) => g.addColorStop(stop, color)); + return g; + } + + g.addColorStop(0, c0); + const l0 = breachedLevel(ctx.p0DataIndex, v[ctx.p0DataIndex]); + const l1 = breachedLevel(ctx.p1DataIndex, v[ctx.p1DataIndex]); + const dom0 = l0 && number(l0.dominance, 0); + const dom1 = l1 && number(l1.dominance, 0); + if (dom0) g.addColorStop(dom0, c0); + else if (dom1) g.addColorStop(1 - dom1, c1); + else g.addColorStop(.5, state.uiColors.neutralGradientMid); + g.addColorStop(1, c1); + return g; + } const ambientGlow = init.ambientGlow || {}; function ambientGlowEnabled() { return ambientGlow.enabled !== false && ambientGlow.enabled !== 0; } @@ -294,7 +354,29 @@ } }; } - function syncControls() { const select = controls.querySelector('.tva-chart-mode-select'), back = controls.querySelector('.tva-chart-back'), fs = controls.querySelector('.tva-chart-fullscreen'); if (select) select.value = mode; back.hidden = mode === 'timeline'; fs.classList.toggle('is-restore', container.classList.contains('tva-chart-fullscreen-mode')); const vs = values(), active = vs.map((v, i) => isMetricStale(i) ? null : breachedLevel(i, v)).filter(Boolean).sort((a, b) => number(b.priority, 0) - number(a.priority, 0))[0], borderAlert = active && (active.borderBlink || active.blinkBorder || active.borderAlert); alertBadge.hidden = mode !== 'timeline' || !active; alertBadge.innerHTML = active ? `<span class="tva-alert-icon" aria-hidden="true"></span>${active.label || active.key.toUpperCase()}` : ''; alertBadge.style.color = active ? levelColor(active) : ''; container.classList.toggle('tva-border-alert', !!borderAlert); if (borderAlert) container.style.setProperty('--tva-border-alert-color', levelColor(active)); else container.style.removeProperty('--tva-border-alert-color'); } + function activeLevel() { + return values() + .map((v, i) => isMetricStale(i) ? null : breachedLevel(i, v)) + .filter(Boolean) + .sort((a, b) => number(b.priority, 0) - number(a.priority, 0))[0]; + } + function syncControls() { + const select = controls.querySelector('.tva-chart-mode-select'); + const back = controls.querySelector('.tva-chart-back'); + const fs = controls.querySelector('.tva-chart-fullscreen'); + const active = activeLevel(); + const borderAlert = active && (active.borderBlink || active.blinkBorder || active.borderAlert); + + if (select) select.value = mode; + back.hidden = mode === 'timeline'; + fs.classList.toggle('is-restore', container.classList.contains('tva-chart-fullscreen-mode')); + alertBadge.hidden = mode !== 'timeline' || !active; + alertBadge.innerHTML = active ? `<span class="tva-alert-icon" aria-hidden="true"></span>${active.label || active.key.toUpperCase()}` : ''; + alertBadge.style.color = active ? levelColor(active) : ''; + container.classList.toggle('tva-border-alert', !!borderAlert); + if (borderAlert) container.style.setProperty('--tva-border-alert-color', levelColor(active)); + else container.style.removeProperty('--tva-border-alert-color'); + } function render() { if (chart) chart.destroy(); chart = new Chart(canvas.getContext('2d'), mode === 'timeline' ? timelineConfig() : barConfig()); syncControls(); } let blinkId = null; let staleSweepId = null; @@ -303,23 +385,95 @@ function syncBlinkTimer() { const shouldBlink = hasCriticalBlink(); if (shouldBlink && !blinkId) { - blinkId = setInterval(() => { criticalBlink = !criticalBlink; if (chart && mode === 'timeline') chart.update('none'); }, number(init.blinkMs, 1100)); + blinkId = setInterval(() => { + criticalBlink = !criticalBlink; + if (chart && mode === 'timeline') chart.update('none'); + }, number(init.blinkMs, 1100)); } else if (!shouldBlink && blinkId) { clearInterval(blinkId); blinkId = null; criticalBlink = true; } const shouldSweep = hasStaleSweep(); if (shouldSweep && !staleSweepId) { - staleSweepId = setInterval(() => { stalePhase += staleDirection * 0.08; if (stalePhase >= 1) { stalePhase = 1; staleDirection = -1; } else if (stalePhase <= 0) { stalePhase = 0; staleDirection = 1; } if (chart && mode === 'timeline') chart.update('none'); }, number(init.staleSweepMs, 140)); + staleSweepId = setInterval(() => { + stalePhase += staleDirection * 0.08; + if (stalePhase >= 1) { stalePhase = 1; staleDirection = -1; } + else if (stalePhase <= 0) { stalePhase = 0; staleDirection = 1; } + if (chart && mode === 'timeline') chart.update('none'); + }, number(init.staleSweepMs, 140)); } else if (!shouldSweep && staleSweepId) { clearInterval(staleSweepId); staleSweepId = null; stalePhase = 0; staleDirection = 1; } } - function refresh() { if (!chart) { render(); syncBlinkTimer(); return; } if (mode !== 'timeline') { render(); syncBlinkTimer(); return; } const nextConfig = timelineConfig(); chart.data.labels = nextConfig.data.labels; nextConfig.data.datasets.forEach((dataset, index) => { if (chart.data.datasets[index]) chart.data.datasets[index].data = dataset.data; }); const metricDataset = chart.data.datasets[metricDatasetIndex]; if (metricDataset) metricDataset.data = values(); Object.assign(chart.options.scales.y, zoomBounds(values())); syncControls(); syncBlinkTimer(); chart.update('none'); } + function refresh() { + if (!chart || mode !== 'timeline') { + render(); + syncBlinkTimer(); + return; + } + + const nextConfig = timelineConfig(); + chart.data.labels = nextConfig.data.labels; + nextConfig.data.datasets.forEach((dataset, index) => { + if (chart.data.datasets[index]) chart.data.datasets[index].data = dataset.data; + }); + const metricDataset = chart.data.datasets[metricDatasetIndex]; + if (metricDataset) metricDataset.data = values(); + Object.assign(chart.options.scales.y, zoomBounds(values())); + syncControls(); + syncBlinkTimer(); + chart.update('none'); + } function switchMode(next) { mode = next; render(); syncBlinkTimer(); } render(); syncBlinkTimer(); - function publicData() { const out = { ...clone(state), updatedAt: clone(updatedAtByKey) }; delete out.uiColors; if (out.colors) { if (!(init.colors && init.colors.normal)) delete out.colors.normal; if (!(init.colors && init.colors.stale)) delete out.colors.stale; } return out; } - function touchChanged(patch) { const now = Date.now(); Object.entries(patch || {}).forEach(([raw, val]) => { const key = aliases[raw] || raw; if (metricKeys.includes(key) && val !== undefined && val !== null) updatedAtByKey[key] = now; }); if (patch && patch.updatedAt && typeof patch.updatedAt === 'object') Object.entries(patch.updatedAt).forEach(([raw, value]) => { const key = aliases[raw] || raw; const timestamp = typeof value === 'string' ? Date.parse(value) : number(value, NaN); if (metricKeys.includes(key) && Number.isFinite(timestamp)) updatedAtByKey[key] = timestamp; }); } - return { update(patch) { const before = values(); state = mergeData(state, patch, aliases); resolveUiColors(); if (values().some((v, i) => v !== before[i])) previousMetricValues = before; touchChanged(patch); refresh(); }, setData(next) { previousMetricValues = values(); state = mergeData(baseState(), next, aliases); resolveUiColors(); updatedAtByKey = Object.fromEntries(metricKeys.map(k => [k, Date.now()])); refresh(); }, refreshTheme() { resolveUiColors(); render(); syncBlinkTimer(); }, getData() { return publicData(); }, destroy() { if (blinkId) clearInterval(blinkId); if (staleSweepId) clearInterval(staleSweepId); container.classList.remove('tva-border-alert'); container.style.removeProperty('--tva-border-alert-color'); if (chart) chart.destroy(); controls.remove(); alertBadge.remove(); } }; + function publicData() { + const out = { ...clone(state), updatedAt: clone(updatedAtByKey) }; + delete out.uiColors; + if (out.colors) { + if (!(init.colors && init.colors.normal)) delete out.colors.normal; + if (!(init.colors && init.colors.stale)) delete out.colors.stale; + } + return out; + } + function touchChanged(patch) { + const now = Date.now(); + Object.entries(patch || {}).forEach(([raw, val]) => { + const key = aliases[raw] || raw; + if (metricKeys.includes(key) && val !== undefined && val !== null) updatedAtByKey[key] = now; + }); + if (patch && patch.updatedAt && typeof patch.updatedAt === 'object') Object.entries(patch.updatedAt).forEach(([raw, value]) => { + const key = aliases[raw] || raw; + const timestamp = typeof value === 'string' ? Date.parse(value) : number(value, NaN); + if (metricKeys.includes(key) && Number.isFinite(timestamp)) updatedAtByKey[key] = timestamp; + }); + } + return { + update(patch) { + const before = values(); + state = mergeData(state, patch, aliases); + resolveUiColors(); + if (values().some((v, i) => v !== before[i])) previousMetricValues = before; + touchChanged(patch); + refresh(); + }, + setData(next) { + previousMetricValues = values(); + state = mergeData(baseState(), next, aliases); + resolveUiColors(); + updatedAtByKey = Object.fromEntries(metricKeys.map(k => [k, Date.now()])); + refresh(); + }, + refreshTheme() { resolveUiColors(); render(); syncBlinkTimer(); }, + getData() { return publicData(); }, + destroy() { + if (blinkId) clearInterval(blinkId); + if (staleSweepId) clearInterval(staleSweepId); + container.classList.remove('tva-border-alert'); + container.style.removeProperty('--tva-border-alert-color'); + if (chart) chart.destroy(); + controls.remove(); + alertBadge.remove(); + } + }; }; })();
: asapien/superpulse