1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
import { formatDuration, formatValue, formatWallClock } from '../utils-format.js';
function createElement(tagName, className, textContent) {
const element = document.createElement(tagName);
if (className) {
element.className = className;
}
if (textContent) {
element.textContent = textContent;
}
return element;
}
function setToggleState(element, active) {
element.dataset.active = String(active);
}
function readControlValue(element) {
if (element.tagName === 'SELECT') {
return element.value;
}
if (element instanceof HTMLInputElement) {
if (element.type === 'checkbox') {
return element.checked;
}
if (element.type === 'number' || element.type === 'range') {
return Number(element.value);
}
return element.value;
}
return element.value;
}
function syncControlValue(element, value) {
if (!element || document.activeElement === element) {
return;
}
if (element instanceof HTMLInputElement && element.type === 'checkbox') {
element.checked = Boolean(value);
return;
}
element.value = String(value ?? '');
}
export class PanelManager {
constructor({ root, store, actions }) {
this.root = root;
this.store = store;
this.actions = actions;
this.elements = {};
}
mount() {
const shell = createElement('div', 'timeplot-shell');
const topbar = createElement('header', 'timeplot-topbar');
const viewport = createElement('section', 'timeplot-viewport');
const plotGrid = createElement('div', 'timeplot-plot-grid');
const primaryPlotPanel = createElement('section', 'timeplot-plot-panel');
const secondaryPlotPanel = createElement('section', 'timeplot-plot-panel');
const primaryCanvasHost = createElement('div', 'timeplot-canvas-host');
const secondaryCanvasHost = createElement('div', 'timeplot-canvas-host');
const sidebar = createElement('aside', 'timeplot-sidebar');
const primaryTooltip = createElement('div', 'timeplot-tooltip');
const secondaryTooltip = createElement('div', 'timeplot-tooltip');
primaryTooltip.hidden = true;
secondaryTooltip.hidden = true;
const brand = createElement('div', 'timeplot-brand');
const title = createElement('h1', 'timeplot-title', 'TimePlot');
const subtitle = createElement('div', 'timeplot-subtitle', 'Dual synchronized signal monitor');
brand.append(title, subtitle);
const toolbar = createElement('div', 'timeplot-toolbar');
toolbar.append(
this.createTransportControls(),
this.createPanelToggles(),
);
topbar.append(brand, toolbar);
primaryPlotPanel.append(primaryCanvasHost, primaryTooltip);
secondaryPlotPanel.append(secondaryCanvasHost, secondaryTooltip);
plotGrid.append(primaryPlotPanel, secondaryPlotPanel);
viewport.append(plotGrid);
shell.append(topbar, viewport, sidebar);
this.root.replaceChildren(shell);
this.elements = {
...this.elements,
shell,
topbar,
viewport,
plotGrid,
primaryPlotPanel,
secondaryPlotPanel,
primaryCanvasHost,
secondaryCanvasHost,
sidebar,
primaryTooltip,
secondaryTooltip,
title,
subtitle,
statusPanel: this.createStatusPanel(),
sourcePanel: this.createSourcePanel(),
configPanel: this.createConfigPanel(),
helpPanel: this.createHelpPanel(),
};
sidebar.append(
this.elements.statusPanel,
this.elements.sourcePanel,
this.elements.configPanel,
this.elements.helpPanel,
);
return this.elements;
}
createTransportControls() {
const wrapper = createElement('div', 'control-group');
const pauseButton = createElement('button', 'control-button', 'Pause');
const resetButton = createElement('button', 'control-button', 'Reset');
const speedLabel = createElement('span', null, 'Speed');
const speedInput = document.createElement('input');
speedInput.type = 'range';
speedInput.min = '0.1';
speedInput.max = '6';
speedInput.step = '0.1';
const speedValue = createElement('span', null, '1.0×');
pauseButton.addEventListener('click', () => this.actions.togglePause());
resetButton.addEventListener('click', () => this.actions.resetScene());
speedInput.addEventListener('input', (event) => this.actions.setSpeed(Number(event.target.value)));
wrapper.append(pauseButton, resetButton, speedLabel, speedInput, speedValue);
this.elements.pauseButton = pauseButton;
this.elements.resetButton = resetButton;
this.elements.speedInput = speedInput;
this.elements.speedValue = speedValue;
return wrapper;
}
createPanelToggles() {
const wrapper = createElement('div', 'control-group');
const panelIds = ['status', 'source', 'config', 'help'];
this.elements.panelButtons = {};
for (const panelId of panelIds) {
const button = createElement('button', 'panel-toggle', panelId);
button.addEventListener('click', () => this.actions.togglePanel(panelId));
this.elements.panelButtons[panelId] = button;
wrapper.append(button);
}
return wrapper;
}
createStatusPanel() {
const panel = createElement('section', 'panel');
panel.innerHTML = `
<h2>Status</h2>
<dl class="kv-list">
<dt>Renderer</dt><dd data-field="renderer">—</dd>
<dt>Real time</dt><dd data-field="realTime">—</dd>
<dt>Real elapsed</dt><dd data-field="realElapsed">—</dd>
<dt>Plot time</dt><dd data-field="plotTime">—</dd>
<dt>Playback</dt><dd data-field="playback">—</dd>
<dt>Points</dt><dd data-field="points">—</dd>
</dl>
`;
return panel;
}
createSourcePanel() {
const panel = createElement('section', 'panel');
panel.innerHTML = `
<h2>Data Source</h2>
<div class="panel-subsection" data-source-config="signalA">
<div class="panel-section-title">Signal A</div>
<div class="field-grid">
<label>
Source type
<select data-source-key="signalA" data-source-field="type">
<option value="synthetic-wave">Synthetic wave</option>
<option value="csv-replay">CSV replay</option>
<option value="websocket">WebSocket</option>
</select>
</label>
</div>
<div class="field-grid" data-source-mode="synthetic-wave">
<label>
Preset
<select data-source-key="signalA" data-source-field="preset">
<option value="telemetry">Telemetry</option>
<option value="chirp">Chirp</option>
<option value="burst">Burst</option>
</select>
</label>
<label>
Sample rate (Hz)
<input data-source-key="signalA" data-source-field="sampleRateHz" type="number" min="1" max="240" step="1" />
</label>
<label>
Amplitude
<input data-source-key="signalA" data-source-field="amplitude" type="number" min="0.1" max="3" step="0.1" />
</label>
<label>
Noise
<input data-source-key="signalA" data-source-field="noise" type="number" min="0" max="0.5" step="0.01" />
</label>
</div>
<div class="field-grid" data-source-mode="csv-replay">
<label>
CSV file
<input data-source-key="signalA" data-source-file="dataset" type="file" accept=".csv,text/csv" />
</label>
<label>
Replay rate
<input data-source-key="signalA" data-source-field="replayRate" type="number" min="0.1" max="8" step="0.1" />
</label>
<div class="source-meta" data-source-key="signalA" data-source-meta></div>
</div>
<div class="field-grid" data-source-mode="websocket">
<label>
WebSocket URL
<input data-source-key="signalA" data-source-field="wsUrl" type="url" placeholder="ws://localhost:8080" />
</label>
<label>
Reconnect (ms)
<input data-source-key="signalA" data-source-field="wsReconnectMs" type="number" min="250" max="30000" step="250" />
</label>
<div class="source-meta" data-source-key="signalA" data-source-ws-meta></div>
</div>
</div>
<div class="panel-subsection" data-source-config="signalB">
<div class="panel-section-title">Signal B</div>
<div class="field-grid">
<label>
Source type
<select data-source-key="signalB" data-source-field="type">
<option value="synthetic-wave">Synthetic wave</option>
<option value="csv-replay">CSV replay</option>
<option value="websocket">WebSocket</option>
</select>
</label>
</div>
<div class="field-grid" data-source-mode="synthetic-wave">
<label>
Preset
<select data-source-key="signalB" data-source-field="preset">
<option value="telemetry">Telemetry</option>
<option value="chirp">Chirp</option>
<option value="burst">Burst</option>
</select>
</label>
<label>
Sample rate (Hz)
<input data-source-key="signalB" data-source-field="sampleRateHz" type="number" min="1" max="240" step="1" />
</label>
<label>
Amplitude
<input data-source-key="signalB" data-source-field="amplitude" type="number" min="0.1" max="3" step="0.1" />
</label>
<label>
Noise
<input data-source-key="signalB" data-source-field="noise" type="number" min="0" max="0.5" step="0.01" />
</label>
</div>
<div class="field-grid" data-source-mode="csv-replay">
<label>
CSV file
<input data-source-key="signalB" data-source-file="dataset" type="file" accept=".csv,text/csv" />
</label>
<label>
Replay rate
<input data-source-key="signalB" data-source-field="replayRate" type="number" min="0.1" max="8" step="0.1" />
</label>
<div class="source-meta" data-source-key="signalB" data-source-meta></div>
</div>
<div class="field-grid" data-source-mode="websocket">
<label>
WebSocket URL
<input data-source-key="signalB" data-source-field="wsUrl" type="url" placeholder="ws://localhost:8080" />
</label>
<label>
Reconnect (ms)
<input data-source-key="signalB" data-source-field="wsReconnectMs" type="number" min="250" max="30000" step="250" />
</label>
<div class="source-meta" data-source-key="signalB" data-source-ws-meta></div>
</div>
</div>
`;
panel.querySelectorAll('[data-source-field]').forEach((input) => {
const eventName = input.tagName === 'SELECT' ? 'change' : 'input';
input.addEventListener(eventName, () => {
const sourceKey = input.getAttribute('data-source-key');
const field = input.getAttribute('data-source-field');
const value = readControlValue(input);
this.actions.updateSource(sourceKey, field, value);
});
});
panel.querySelectorAll('[data-source-file]').forEach((input) => {
input.addEventListener('change', async () => {
const sourceKey = input.getAttribute('data-source-key');
const file = input.files?.[0];
if (!file) {
return;
}
await this.actions.loadSourceFile(sourceKey, file);
input.value = '';
});
});
return panel;
}
createConfigPanel() {
const panel = createElement('section', 'panel');
panel.innerHTML = `
<h2>Config</h2>
<div class="field-grid">
<label>
Visible window (ms)
<input data-plot-field="windowDurationMs" type="number" min="2000" max="120000" step="1000" />
</label>
<label>
Max points
<input data-plot-field="maxPoints" type="number" min="200" max="4000" step="100" />
</label>
<div class="panel-row">
<span>Show grid</span>
<input data-plot-field="showGrid" type="checkbox" />
</div>
<div class="panel-row">
<span>Show points</span>
<input data-plot-field="showPoints" type="checkbox" />
</div>
</div>
<div class="panel-subsection">
<div class="panel-section-title">Graph routing</div>
<div class="field-grid">
<label>
Primary graph source
<select data-graph-id="primary" data-graph-field="sourceKey">
<option value="signalA">Signal A</option>
<option value="signalB">Signal B</option>
</select>
</label>
<label>
Primary graph transform
<select data-graph-id="primary" data-graph-field="transform">
<option value="raw">Raw</option>
<option value="delta">Delta</option>
<option value="smooth">Smooth</option>
</select>
</label>
<label>
Secondary graph source
<select data-graph-id="secondary" data-graph-field="sourceKey">
<option value="signalA">Signal A</option>
<option value="signalB">Signal B</option>
</select>
</label>
<label>
Secondary graph transform
<select data-graph-id="secondary" data-graph-field="transform">
<option value="raw">Raw</option>
<option value="delta">Delta</option>
<option value="smooth">Smooth</option>
</select>
</label>
</div>
</div>
`;
panel.querySelectorAll('[data-plot-field]').forEach((input) => {
const eventName = input instanceof HTMLInputElement && input.type === 'checkbox' ? 'change' : 'input';
input.addEventListener(eventName, () => {
const field = input.getAttribute('data-plot-field');
const value = readControlValue(input);
this.actions.updatePlot(field, value);
});
});
panel.querySelectorAll('[data-graph-field]').forEach((input) => {
input.addEventListener('change', () => {
const graphId = input.getAttribute('data-graph-id');
const field = input.getAttribute('data-graph-field');
this.actions.updateGraph(graphId, field, input.value);
});
});
return panel;
}
createHelpPanel() {
const panel = createElement('section', 'panel');
panel.innerHTML = `
<h2>Help</h2>
<ol class="help-list">
<li>Each signal can be synthetic or file-backed CSV replay.</li>
<li>Each graph can target Signal A or Signal B independently.</li>
<li>Each graph can render raw, delta, or smoothed data.</li>
<li>Hover either trace to inspect the nearest synchronized sample.</li>
<li>Use pause and speed controls to inspect timing behavior.</li>
</ol>
`;
return panel;
}
sync(state, visiblePoints) {
this.elements.title.textContent = state.app.title;
this.elements.subtitle.textContent = 'Dual synchronized signal monitor';
this.elements.pauseButton.textContent = state.time.paused ? 'Resume' : 'Pause';
setToggleState(this.elements.pauseButton, state.time.paused);
syncControlValue(this.elements.speedInput, state.time.speed);
this.elements.speedValue.textContent = `${state.time.speed.toFixed(1)}×`;
const statusFields = this.elements.statusPanel.querySelectorAll('[data-field]');
const fieldMap = Object.fromEntries(Array.from(statusFields).map((field) => [field.getAttribute('data-field'), field]));
fieldMap.renderer.textContent = state.app.renderer;
fieldMap.realTime.textContent = formatWallClock(state.time.realNowMs);
fieldMap.realElapsed.textContent = formatDuration(state.time.realElapsedMs);
fieldMap.plotTime.textContent = formatDuration(state.time.plotTimeMs);
fieldMap.playback.textContent = state.time.paused ? 'Paused' : `${state.time.speed.toFixed(1)}×`;
fieldMap.points.textContent = typeof visiblePoints === 'object'
? `${visiblePoints.primary} / ${visiblePoints.secondary}`
: `${visiblePoints}`;
this.syncSourcePanel(state);
this.syncConfigPanel(state);
this.syncPanels(state);
this.syncTooltip(state);
}
syncSourcePanel(state) {
Object.entries(state.sources).forEach(([sourceKey, sourceConfig]) => {
syncControlValue(this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-field="type"]`), sourceConfig.type);
syncControlValue(this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-field="preset"]`), sourceConfig.preset);
syncControlValue(this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-field="sampleRateHz"]`), sourceConfig.sampleRateHz);
syncControlValue(this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-field="amplitude"]`), sourceConfig.amplitude);
syncControlValue(this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-field="noise"]`), sourceConfig.noise);
const replayRateInput = this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-field="replayRate"]`);
if (replayRateInput) {
syncControlValue(replayRateInput, sourceConfig.replayRate ?? 1);
}
const sourceSection = this.elements.sourcePanel.querySelector(`[data-source-config="${sourceKey}"]`);
sourceSection.querySelectorAll('[data-source-mode]').forEach((modeSection) => {
modeSection.hidden = modeSection.getAttribute('data-source-mode') !== sourceConfig.type;
});
const meta = this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-meta]`);
if (meta) {
if (sourceConfig.type === 'csv-replay') {
meta.innerHTML = sourceConfig.loadError
? `<span class="source-meta-error">${sourceConfig.loadError}</span>`
: `${sourceConfig.dataFileName || 'No file loaded'}${sourceConfig.datasetPointCount ? ` · ${sourceConfig.datasetPointCount} pts · ${formatDuration(sourceConfig.datasetDurationMs || 0)}` : ''}`;
} else if (sourceConfig.type === 'websocket') {
meta.textContent = '';
} else {
meta.textContent = 'Generates data procedurally in-browser';
}
}
const wsUrlInput = this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-field="wsUrl"]`);
const wsReconnectInput = this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-field="wsReconnectMs"]`);
const wsMeta = this.elements.sourcePanel.querySelector(`[data-source-key="${sourceKey}"][data-source-ws-meta]`);
if (wsUrlInput) {
syncControlValue(wsUrlInput, sourceConfig.wsUrl ?? '');
}
if (wsReconnectInput) {
syncControlValue(wsReconnectInput, sourceConfig.wsReconnectMs ?? 2000);
}
if (wsMeta) {
wsMeta.innerHTML = sourceConfig.type === 'websocket'
? `status: <span class="source-meta-status source-meta-status-${sourceConfig.wsStatus || 'idle'}">${sourceConfig.wsStatus || 'idle'}</span>${sourceConfig.wsStatusDetail ? ` · ${sourceConfig.wsStatusDetail}` : ''}`
: '';
}
});
}
syncConfigPanel(state) {
syncControlValue(this.elements.configPanel.querySelector('[data-plot-field="windowDurationMs"]'), state.plot.windowDurationMs);
syncControlValue(this.elements.configPanel.querySelector('[data-plot-field="maxPoints"]'), state.plot.maxPoints);
syncControlValue(this.elements.configPanel.querySelector('[data-plot-field="showGrid"]'), state.plot.showGrid);
syncControlValue(this.elements.configPanel.querySelector('[data-plot-field="showPoints"]'), state.plot.showPoints);
syncControlValue(this.elements.configPanel.querySelector('[data-graph-id="primary"][data-graph-field="sourceKey"]'), state.graphs.primary.sourceKey);
syncControlValue(this.elements.configPanel.querySelector('[data-graph-id="primary"][data-graph-field="transform"]'), state.graphs.primary.transform);
syncControlValue(this.elements.configPanel.querySelector('[data-graph-id="secondary"][data-graph-field="sourceKey"]'), state.graphs.secondary.sourceKey);
syncControlValue(this.elements.configPanel.querySelector('[data-graph-id="secondary"][data-graph-field="transform"]'), state.graphs.secondary.transform);
}
syncPanels(state) {
const panelMap = {
status: this.elements.statusPanel,
source: this.elements.sourcePanel,
config: this.elements.configPanel,
help: this.elements.helpPanel,
};
for (const [panelId, panelState] of Object.entries(state.panels)) {
panelMap[panelId].hidden = !panelState.visible;
setToggleState(this.elements.panelButtons[panelId], panelState.visible);
}
}
syncTooltip(state) {
const tooltipState = state.plot.tooltip;
this.elements.primaryTooltip.hidden = true;
this.elements.secondaryTooltip.hidden = true;
if (!tooltipState.visible || !tooltipState.point) {
return;
}
const tooltip = tooltipState.panelId === 'secondary'
? this.elements.secondaryTooltip
: this.elements.primaryTooltip;
tooltip.hidden = false;
tooltip.style.left = `${tooltipState.x}px`;
tooltip.style.top = `${tooltipState.y}px`;
tooltip.innerHTML = `
<div class="timeplot-tooltip-title">Hovered sample</div>
<div class="timeplot-tooltip-row"><span class="muted">Panel</span><span>${tooltipState.panelLabel ?? 'Primary'}</span></div>
<div class="timeplot-tooltip-row"><span class="muted">Plot time</span><span>${formatDuration(tooltipState.point.timeMs)}</span></div>
<div class="timeplot-tooltip-row"><span class="muted">Value</span><span>${formatValue(tooltipState.point.value)}</span></div>
<div class="timeplot-tooltip-row"><span class="muted">Source</span><span>${tooltipState.point.sourceId}</span></div>
${tooltipState.linkedPoint ? `<div class="timeplot-tooltip-row"><span class="muted">Linked panel</span><span>${tooltipState.linkedPanelLabel ?? 'Linked'}</span></div>` : ''}
${tooltipState.linkedPoint ? `<div class="timeplot-tooltip-row"><span class="muted">Linked value</span><span>${formatValue(tooltipState.linkedPoint.value)}</span></div>` : ''}
`;
}
}
|