feat(ui): render the CCTV calibration readout and its pose editor from the store

presentCctvCalibration() joins the panel view: ADJUST, SAVE CAL, RESET CAL
and the seven pose chips render from the gev-cctv store. The inline editor
is store state: starting an edit publishes its field, starting value and
finish(), and the CctvCalValue chip renders the number input. The draft is
set once on mount so a refresh while typing keeps it; each edit gets its
own input. Commit and cancel rules, the captured base pose and the camera
guards stay with CctvControls.

formField.js had no other user and is removed.
This commit is contained in:
Sameh Khamis committed 2026-10-30 21:45:24 -07:00
1 parent 5b8cb40fce
commit 6c7035f617
13 files changed
+337 -240

No files matched your search

+51 -73
View File
@@ -1,5 +1,4 @@
import { formatNumber, t, msg } from '../i18n/index.js';
import { createNumberInput } from './formField.js';
/** Shortest-wrap signed degrees, for heading offsets typed as absolute values. */
const signedNormalizeDeg = (deg) => ((((deg + 180) % 360) + 360) % 360) - 180;
@@ -111,100 +110,79 @@ export function _resetCctvCalibration() {
export function _beginCctvCalValueEdit(chip) {
if (this.destroyed) return;
this._calibrationEdit?.(false);
const field = CCTV_CAL_FIELDS[chip.dataset.calField];
const fieldId = chip.dataset.calField;
const field = CCTV_CAL_FIELDS[fieldId];
const activeCamera = this._cctvState?.activeCamera;
if (!field || !activeCamera?.basePose) return;
const cameraId = this._activeCctvCameraId();
const basePose = { ...activeCamera.basePose };
const editListeners = new AbortController();
const startValue = field.get(activeCamera);
const input = createNumberInput({
className: 'gev-field--inline cctv-cal-input',
step: field.decimals > 0 ? '0.1' : '1',
value: Number(startValue).toFixed(field.decimals),
ariaLabel: t(field.label),
});
chip.textContent = `${t(field.label)} `;
chip.appendChild(input);
input.focus();
input.select();
let finished = false;
const finish = (commit) => {
/**
* End the edit: commit converts the typed value to a calibration offset
* against the pose captured when it began; anything else just closes it.
*/
const finish = (commit, typedText = '') => {
if (finished) return;
finished = true;
editListeners.abort();
this._calibrationEdit = null;
const typed = parseFloat(input.value);
input.remove();
this.store.setCalEdit(null);
const typed = parseFloat(typedText);
if (
commit &&
!this.destroyed &&
Number.isFinite(typed) &&
this._cctvState?.enabled &&
this.actions.isEnabled() &&
cameraId === this._activeCctvCameraId()
cameraId &&
cameraId === this._activeCctvCameraId() &&
this.actions.setParams
) {
if (cameraId && this.actions.setParams) {
this.actions.setParams(
{
selectedCameraId: cameraId,
calibration: { cameraId, patch: field.toPatch(typed, basePose) },
},
{ origin: 'user' },
);
return; // re-render restores the chip text from fresh state
}
this.actions.setParams(
{
selectedCameraId: cameraId,
calibration: { cameraId, patch: field.toPatch(typed, basePose) },
},
{ origin: 'user' },
);
}
this._syncCctvCalReadout(
!!this._cctvState?.enabled,
this._cctvState?.activeCamera || null,
);
};
this._calibrationEdit = finish;
input.addEventListener(
'keydown',
(event) => {
if (event.key === 'Enter') finish(true);
else if (event.key === 'Escape') {
event.preventDefault();
finish(false);
}
event.stopPropagation();
},
{ signal: editListeners.signal },
);
input.addEventListener('blur', () => finish(true), {
signal: editListeners.signal,
});
input.addEventListener('click', (event) => event.stopPropagation(), {
signal: editListeners.signal,
// The readout renders a number input in this chip; Enter or blur commits
// and Escape cancels through `finish`.
this.store.setCalEdit({
field: fieldId,
label: `${t(field.label)} `,
ariaLabel: t(field.label),
step: field.decimals > 0 ? '0.1' : '1',
value: Number(field.get(activeCamera)).toFixed(field.decimals),
finish,
});
}
export function _syncCctvCalReadout(enabled, activeCamera) {
/**
* The calibration block for a camera state: ADJUST, the pose chips and
* whether calibration is possible. Chips show the effective pose.
* @param {object|null} state Camera layer UI state.
* @param {boolean} enabled Whether the layer is on.
*/
export function presentCctvCalibration(state, enabled) {
const activeCamera = state?.activeCamera || null;
const canCalibrate = !!enabled && !!activeCamera;
if (this._cctvAdjustBtn) {
const adjustOn = !!this._cctvState?.calibrationMode;
this._cctvAdjustBtn.classList.toggle('active', adjustOn && canCalibrate);
this._cctvAdjustBtn.textContent = adjustOn ? t('ADJUST ON') : t('ADJUST');
this._cctvAdjustBtn.disabled = !canCalibrate;
}
if (this._cctvCalReadout) {
for (const chip of this._cctvCalReadout.querySelectorAll(
'.cctv-cal-value',
)) {
if (chip.querySelector('input')) continue; // an edit is in flight — don't clobber
const field = CCTV_CAL_FIELDS[chip.dataset.calField];
if (!field) continue;
const value = canCalibrate ? field.get(activeCamera) : null;
chip.textContent = Number.isFinite(value)
? calValueText(field, value)
: `${t(field.label)} --`;
chip.disabled = !canCalibrate;
}
}
for (const el of [this._cctvCalibSaveBtn, this._cctvCalibResetBtn]) {
if (el) el.disabled = !canCalibrate;
const adjustOn = !!state?.calibrationMode;
const values = {};
for (const [id, field] of Object.entries(CCTV_CAL_FIELDS)) {
const value = canCalibrate ? field.get(activeCamera) : null;
values[id] = Number.isFinite(value)
? calValueText(field, value)
: `${t(field.label)} --`;
}
return {
disabled: !canCalibrate,
adjust: {
active: adjustOn && canCalibrate,
text: adjustOn ? t('ADJUST ON') : t('ADJUST'),
},
values,
};
}