153 lines
5.2 KiB
TypeScript
153 lines
5.2 KiB
TypeScript
import { CdkDrag, CdkDragDrop, DragDropModule } from '@angular/cdk/drag-drop';
|
|
import { Component, computed, input, signal } from '@angular/core';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatButtonToggleModule } from '@angular/material/button-toggle';
|
|
import { MatChipsModule } from '@angular/material/chips';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatSelectModule } from '@angular/material/select';
|
|
import { MatTooltipModule } from '@angular/material/tooltip';
|
|
import { SourceColumn, TargetField } from '../../api/api.types';
|
|
import { ImportFlow } from '../../domain/import-flow';
|
|
import { AutofocusDirective } from '../shared/autofocus.directive';
|
|
import { TypeIconPipe, TypeLabelPipe } from '../shared/type-icon.pipe';
|
|
|
|
type FieldFilter = 'all' | 'required' | 'unassigned';
|
|
|
|
/**
|
|
* Drag-and-drop column mapping for one flow: source columns on the left,
|
|
* destination fields (drop slots) on the right. Every slot also takes a
|
|
* pick from a select (keyboard/accessibility path) and an optional default
|
|
* value, so a field the source lacks can still be filled.
|
|
*
|
|
* Presentation only: every edit goes through `ImportFlow`, which owns the
|
|
* mapping state.
|
|
*/
|
|
@Component({
|
|
selector: 'app-column-mapper',
|
|
imports: [
|
|
AutofocusDirective,
|
|
DragDropModule,
|
|
MatButtonModule,
|
|
MatButtonToggleModule,
|
|
MatChipsModule,
|
|
MatFormFieldModule,
|
|
MatIconModule,
|
|
MatInputModule,
|
|
MatSelectModule,
|
|
MatTooltipModule,
|
|
TypeIconPipe,
|
|
TypeLabelPipe,
|
|
],
|
|
templateUrl: './column-mapper.html',
|
|
styleUrl: './column-mapper.scss',
|
|
})
|
|
export class ColumnMapper {
|
|
readonly flow = input.required<ImportFlow>();
|
|
|
|
readonly fieldFilter = signal<FieldFilter>('all');
|
|
readonly fieldSearch = signal('');
|
|
readonly onlyUnusedColumns = signal(false);
|
|
readonly editingDefault = signal<string | null>(null);
|
|
readonly lastAutoMap = signal<number | null>(null);
|
|
|
|
readonly acceptsColumns = (drag: CdkDrag) => typeof drag.data === 'string';
|
|
|
|
readonly fields = computed<TargetField[]>(() => {
|
|
const flow = this.flow();
|
|
const mapped = flow.mapping.byField();
|
|
const defaults = flow.defaults();
|
|
const term = this.fieldSearch().trim().toLowerCase();
|
|
return flow.target.fields
|
|
.filter((field) => !term || field.name.toLowerCase().includes(term))
|
|
.filter((field) => {
|
|
switch (this.fieldFilter()) {
|
|
case 'required':
|
|
return field.required;
|
|
case 'unassigned':
|
|
return !mapped.has(field.name) && !defaults[field.name];
|
|
default:
|
|
return true;
|
|
}
|
|
})
|
|
.sort((a, b) => Number(b.required) - Number(a.required));
|
|
});
|
|
|
|
readonly columns = computed<SourceColumn[]>(() => {
|
|
const flow = this.flow();
|
|
return this.onlyUnusedColumns() ? flow.unmappedColumns() : flow.sourceColumns();
|
|
});
|
|
|
|
/** Up to three distinct non-empty sample values per source column, for the tray. */
|
|
readonly samples = computed(() => {
|
|
const rows = this.flow().preview()?.sample_rows ?? [];
|
|
const samples = new Map<string, string>();
|
|
for (const column of this.flow().sourceColumns()) {
|
|
const values = new Set<string>();
|
|
for (const row of rows) {
|
|
const value = row[column.name];
|
|
if (value !== null && value !== undefined && String(value).trim() !== '') values.add(String(value));
|
|
if (values.size === 3) break;
|
|
}
|
|
samples.set(column.name, [...values].join(' · '));
|
|
}
|
|
return samples;
|
|
});
|
|
|
|
readonly summary = computed(() => {
|
|
const flow = this.flow();
|
|
const covered = flow.target.fields.filter((f) => flow.mapping.byField().has(f.name) || flow.defaults()[f.name]).length;
|
|
return {
|
|
covered,
|
|
total: flow.target.fields.length,
|
|
missing: flow.missingRequired().length,
|
|
suggestions: flow.pendingSuggestions(),
|
|
unused: flow.unmappedColumns().length,
|
|
};
|
|
});
|
|
|
|
onSearch(event: Event): void {
|
|
this.fieldSearch.set((event.target as HTMLInputElement).value);
|
|
}
|
|
|
|
onDropOnField(event: CdkDragDrop<string>): void {
|
|
this.flow().map(event.item.data as string, event.container.data);
|
|
}
|
|
|
|
onDropOnTray(event: CdkDragDrop<string>): void {
|
|
if (event.previousContainer !== event.container) this.flow().unmapSource(event.item.data as string);
|
|
}
|
|
|
|
pick(field: string, source: string | null): void {
|
|
if (source) this.flow().map(source, field);
|
|
else this.flow().unmapField(field);
|
|
}
|
|
|
|
autoMap(): void {
|
|
this.lastAutoMap.set(this.flow().autoMap());
|
|
}
|
|
|
|
confirmAll(): void {
|
|
const flow = this.flow();
|
|
flow.mapping.items().forEach((m) => m.confidence !== undefined && flow.confirmSuggestion(m.field));
|
|
}
|
|
|
|
saveDefault(field: string, value: string): void {
|
|
this.flow().setDefault(field, value.trim() || null);
|
|
this.editingDefault.set(null);
|
|
}
|
|
|
|
errorsFor(field: string): number {
|
|
return this.flow().quality().get(field)?.errors ?? 0;
|
|
}
|
|
|
|
mappedTo(column: string): string | undefined {
|
|
return this.flow().mapping.bySource().get(column)?.field;
|
|
}
|
|
|
|
confidenceLabel(confidence: number): string {
|
|
return `${Math.round(confidence * 100)}%`;
|
|
}
|
|
}
|