procon aggregate
This commit is contained in:
1 parent
3bea468d23
commit
734aa81710
16 files changed
+656
-12
No files matched your search
@@ -0,0 +1,185 @@
|
||||
'use client';
|
||||
|
||||
import { useLayoutEffect, useRef, useState, type FormEvent, type KeyboardEvent } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Select } from '@/components/ui/select';
|
||||
import Link from 'next/link';
|
||||
import { api_visuAggregate_save } from '@/actions/rk2/api-visu-aggregate';
|
||||
import { VisuAggregateInput, VisuAggregateRow } from '@/types/anlagen/visu-aggregate';
|
||||
|
||||
const switches = [{ "key": "dauerlaeufer", "label": "Dauerläufer" }, { "key": "stellantrieb", "label": "Stellantrieb" }, { "key": "pulsantrieb", "label": "Pulsantrieb" }, { "key": "direktanlauf", "label": "Direktanlauf" }, { "key": "wendeschuetz", "label": "Wendeschütz" }, { "key": "sternDreieck", "label": "Stern-Dreieck" }, { "key": "fu", "label": "FU" }, { "key": "sanftanlauf", "label": "Sanftanlauf" }, { "key": "dzImpulsgeber", "label": "DZ Impulsgeber" }, { "key": "dzDauersignal", "label": "DZ Dauersignal" }, { "key": "schlupfueberwachung", "label": "Schlupfüberwachung" }, { "key": "endschalter1", "label": "Endschalter 1" }, { "key": "endschalter2", "label": "Endschalter 2" }, { "key": "endschalter3", "label": "Endschalter 3" }, { "key": "endschalter4", "label": "Endschalter 4" }] as const;
|
||||
const texts = [
|
||||
{ key: 'view', label: 'View' }, { key: 'antriebName', label: 'Antrieb Name' }, { key: 'bmk', label: 'BMK' },
|
||||
{ key: 'hmiText3', label: 'HMI Text 3' }, { key: 'hmiText2', label: 'HMI Text 2' }, { key: 'hmiText1', label: 'HMI Text 1' },
|
||||
] as const;
|
||||
|
||||
type BmkDefault = 'unit' | 'view';
|
||||
|
||||
function AggregateRow({ initial, bmkDefault }: { initial: VisuAggregateRow; bmkDefault: BmkDefault }) {
|
||||
const [row, setRow] = useState(initial);
|
||||
const [persisted, setPersisted] = useState(initial.persisted);
|
||||
const [antriebId, setAntriebId] = useState(String(initial.antriebId ?? ''));
|
||||
const [sensorId, setSensorId] = useState(String(initial.analogSensorId ?? ''));
|
||||
const [motors, setMotors] = useState(String(initial.anzahlMotoren));
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
const [failed, setFailed] = useState(false);
|
||||
const formId = 'aggregate-' + initial.componentId;
|
||||
const formRef = useRef<HTMLFormElement>(null);
|
||||
const savedFocus = useRef<{
|
||||
element: HTMLElement;
|
||||
selection: { start: number; end: number; direction: 'forward' | 'backward' | 'none' } | null;
|
||||
} | null>(null);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (saving || !savedFocus.current) return;
|
||||
const { element, selection } = savedFocus.current;
|
||||
savedFocus.current = null;
|
||||
if (!element.isConnected ||
|
||||
(document.activeElement !== document.body && document.activeElement !== element)) return;
|
||||
element.focus({ preventScroll: true });
|
||||
if (selection && element instanceof HTMLInputElement) {
|
||||
element.setSelectionRange(selection.start, selection.end, selection.direction);
|
||||
}
|
||||
}, [saving]);
|
||||
|
||||
function handleKeyDown(event: KeyboardEvent<HTMLTableRowElement>) {
|
||||
if (event.nativeEvent.isComposing) return;
|
||||
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 's') {
|
||||
event.preventDefault();
|
||||
if (!saving && !event.repeat) formRef.current?.requestSubmit();
|
||||
return;
|
||||
}
|
||||
if (!event.altKey || event.ctrlKey || event.metaKey ||
|
||||
(event.key !== 'ArrowDown' && event.key !== 'ArrowUp')) return;
|
||||
event.preventDefault();
|
||||
const target = event.target;
|
||||
if (!(target instanceof HTMLElement)) return;
|
||||
const cell = target.closest<HTMLTableCellElement>('td, th');
|
||||
if (!cell) return;
|
||||
const selector = 'input:not(:disabled), button:not(:disabled), select:not(:disabled), textarea:not(:disabled)';
|
||||
const controls = Array.from(cell.querySelectorAll<HTMLElement>(selector));
|
||||
const controlIndex = Math.max(0, controls.indexOf(target));
|
||||
let next = event.key === 'ArrowDown' ? event.currentTarget.nextElementSibling : event.currentTarget.previousElementSibling;
|
||||
while (next instanceof HTMLTableRowElement) {
|
||||
const nextCell = next.cells[cell.cellIndex];
|
||||
const nextControls = nextCell ? Array.from(nextCell.querySelectorAll<HTMLElement>(selector)) : [];
|
||||
const control = nextControls[controlIndex] ?? nextControls[0];
|
||||
if (control) {
|
||||
control.focus({ preventScroll: true });
|
||||
control.scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
||||
return;
|
||||
}
|
||||
next = event.key === 'ArrowDown' ? next.nextElementSibling : next.previousElementSibling;
|
||||
}
|
||||
}
|
||||
|
||||
function change() { setMessage('Ungespeicherte Änderungen'); setFailed(false); }
|
||||
|
||||
async function save(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
if (saving) return;
|
||||
const drive = Number(antriebId), sensor = sensorId.trim() === '' ? null : Number(sensorId), count = Number(motors);
|
||||
if (!antriebId.trim() || !Number.isSafeInteger(drive) || !motors.trim() || !Number.isSafeInteger(count) || count < 1 ||
|
||||
(sensor !== null && !Number.isSafeInteger(sensor))) {
|
||||
setMessage('Bitte gültige Ganzzahlen eingeben; mindestens ein Motor.'); setFailed(true); return;
|
||||
}
|
||||
const active = document.activeElement;
|
||||
if (active instanceof HTMLElement && event.currentTarget.closest('tr')?.contains(active)) {
|
||||
savedFocus.current = {
|
||||
element: active,
|
||||
selection: active instanceof HTMLInputElement && active.selectionStart !== null && active.selectionEnd !== null
|
||||
? { start: active.selectionStart, end: active.selectionEnd, direction: active.selectionDirection ?? 'none' }
|
||||
: null,
|
||||
};
|
||||
}
|
||||
setSaving(true); setMessage('');
|
||||
try {
|
||||
const data: VisuAggregateInput = { ...row, antriebId: drive, analogSensorId: sensor, anzahlMotoren: count, mengeId: null };
|
||||
const result = await api_visuAggregate_save(row.componentId, data);
|
||||
if (result.success) setPersisted(true);
|
||||
setFailed(!result.success);
|
||||
setMessage(result.success ? 'Gespeichert' : result.error ?? 'Speichern fehlgeschlagen');
|
||||
} finally { setSaving(false); }
|
||||
}
|
||||
|
||||
return <tr onKeyDown={handleKeyDown} className={persisted ? 'align-top bg-green-50/50' : 'align-top'}>
|
||||
<th scope="row" className={`sticky left-0 z-10 min-w-56 border p-2 text-left font-normal ${persisted ? 'bg-green-50' : 'bg-white'}`}>
|
||||
<div className="font-medium">{initial.componentLabel}</div>
|
||||
{persisted && <Link className="mt-1 inline-block text-xs underline" href={`/rk2/visu-aggregate/${initial.componentId}/grafiken`}>Grafiken bearbeiten</Link>}
|
||||
<div className={persisted ? 'mt-1 text-xs text-green-700' : 'mt-1 text-xs text-gray-500'}>
|
||||
{persisted ? 'In DB gespeichert' : 'Noch nicht gespeichert'}
|
||||
</div>
|
||||
<form ref={formRef} id={formId} onSubmit={save} className="mt-2">
|
||||
<Button type="submit" size="sm" disabled={saving}>{saving ? 'Speichert …' : 'Speichern'}</Button>
|
||||
</form>
|
||||
<p role={failed ? 'alert' : 'status'} className={failed ? 'mt-1 text-red-600' : 'mt-1 text-xs'}>{message}</p>
|
||||
</th>
|
||||
<td className="border p-2"><Input form={formId} aria-label={initial.componentLabel + ': Antrieb ID'} className="min-w-28" type="number" step="1" required disabled={saving}
|
||||
value={antriebId} onChange={e => { setAntriebId(e.target.value); change(); }} /></td>
|
||||
<td className="border p-2"><Input form={formId} aria-label={initial.componentLabel + ': AnalogSensor ID'} className="min-w-28" type="number" step="1" disabled={saving}
|
||||
value={sensorId} onChange={e => { setSensorId(e.target.value); change(); }} /></td>
|
||||
<td className="border p-2"><Input aria-label="Menge ID (vorerst leer)" value="" disabled className="min-w-24" /></td>
|
||||
{texts.slice(0, 3).map(({ key, label }) => <td key={key} className="border p-2"><Input form={formId} className="min-w-44" aria-label={initial.componentLabel + ': ' + label}
|
||||
value={row[key] ?? ''} disabled={saving} onChange={e => { setRow(current => ({ ...current, [key]: e.target.value })); change(); }} />
|
||||
<Button type="button" size="sm" variant="secondary" className="mt-1" disabled={saving}
|
||||
onClick={() => {
|
||||
setRow(current => ({
|
||||
...current, [key]: key === 'view' ? initial.defaultView
|
||||
: key === 'antriebName' ? initial.defaultView.replaceAll('_', ' ')
|
||||
: bmkDefault === 'view' ? current.view?.trim() ? current.view : initial.defaultView : initial.defaultBmk
|
||||
}));
|
||||
change();
|
||||
}}>Default</Button>
|
||||
</td>)}
|
||||
{switches.slice(0, 8).map(({ key, label }) => checkbox(key, label))}
|
||||
<td className="border p-2"><Input form={formId} aria-label={initial.componentLabel + ': Anzahl Motoren'} className="min-w-24" type="number" min="1" step="1" required disabled={saving}
|
||||
value={motors} onChange={e => { setMotors(e.target.value); change(); }} /></td>
|
||||
{switches.slice(8, 11).map(({ key, label }) => checkbox(key, label))}
|
||||
{texts.slice(3).map(({ key, label }) => initial.slave ? <td key={key} className="border p-2" aria-label={label + ': entfällt bei Slave'} /> : <td key={key} className="border p-2"><Input form={formId} className="min-w-44" aria-label={initial.componentLabel + ': ' + label}
|
||||
value={row[key] ?? ''} disabled={saving} onChange={e => { setRow(current => ({ ...current, [key]: e.target.value })); change(); }} /></td>)}
|
||||
{switches.slice(11).map(({ key, label }) => checkbox(key, label))}
|
||||
</tr>;
|
||||
|
||||
function checkbox(key: typeof switches[number]['key'], label: string) {
|
||||
if (initial.slave && (key === 'dzImpulsgeber' || key === 'dzDauersignal')) {
|
||||
return <td key={key} className="border p-2" aria-label={label + ': entfällt bei Slave'} />;
|
||||
}
|
||||
return <td key={key} className="border p-3 text-center"><input form={formId} type="checkbox" className="h-4 w-4" aria-label={initial.componentLabel + ': ' + label}
|
||||
checked={row[key]} disabled={saving} onChange={e => { setRow(current => ({ ...current, [key]: e.target.checked })); change(); }} /></td>;
|
||||
}
|
||||
}
|
||||
|
||||
export default function VisuAggregateTable({ rows }: { rows: VisuAggregateRow[] }) {
|
||||
const [bmkDefault, setBmkDefault] = useState<BmkDefault>('unit');
|
||||
const headers = ['Komponente / Aktion', 'Antrieb ID', 'AnalogSensor ID', 'Menge ID', ...texts.slice(0, 3).map(item => item.label),
|
||||
...switches.slice(0, 8).map(item => item.label), 'Anzahl Motoren', ...switches.slice(8, 11).map(item => item.label),
|
||||
...texts.slice(3).map(item => item.label), ...switches.slice(11).map(item => item.label)];
|
||||
if (!rows.length) return <p>In der ausgewählten Anlage sind keine Komponenten vom Typ „Antrieb“ vorhanden.</p>;
|
||||
return <div>
|
||||
<div className="mb-4 flex flex-wrap items-center gap-3">
|
||||
<Button asChild variant="secondary"><Link href="/rk2/visu-aggregate/grafiken">Grafikübersicht</Link></Button>
|
||||
<Button asChild variant="secondary"><a href="/rk2/visu-aggregate/export" download>Excel herunterladen</a></Button>
|
||||
<span className="text-xs text-gray-600">Exportiert gespeicherte Einträge. Änderungen vorher speichern.</span>
|
||||
<label htmlFor="bmk-default" className="text-sm font-medium">BMK-Default</label>
|
||||
<Select id="bmk-default" className="w-64" value={bmkDefault}
|
||||
onChange={event => setBmkDefault(event.target.value === 'view' ? 'view' : 'unit')}>
|
||||
<option value="unit">Unit-BMK</option>
|
||||
<option value="view">View-ID</option>
|
||||
</Select>
|
||||
<p className="text-sm text-gray-600">Wird beim Klick auf „Default“ in der BMK-Spalte übernommen.</p>
|
||||
</div>
|
||||
<p className="mb-3 text-sm text-gray-600">
|
||||
Alt + ↑ / ↓: gleiche Spalte in der vorherigen / nächsten Zeile. Strg + S (Mac: Cmd + S): aktuelle Zeile speichern.
|
||||
</p>
|
||||
<div className="max-h-[75vh] overflow-auto rounded border">
|
||||
<table className="w-full border-separate border-spacing-0 text-sm">
|
||||
<caption className="sr-only">VisuAggregate der ausgewählten Anlage – eine Zeile pro Antrieb-Komponente</caption>
|
||||
<thead className="sticky top-0 z-20 bg-gray-100"><tr>{headers.map(label => <th scope="col" key={label} className="border p-2 text-left">{label}</th>)}</tr></thead>
|
||||
<tbody>{rows.map(row => <AggregateRow key={row.componentId} initial={row} bmkDefault={bmkDefault} />)}</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user