import { Component, input, Type } from '@angular/core'; import { CommonModule } from '@angular/common'; export interface CellComponent { component?: Type; componentInputs?: (obj: T) => { [key: string]: any }; } export interface DetailViewRow extends CellComponent { attribute: keyof T; getValue?: (obj: T) => any; format?: 'date' | 'datetime' | 'number' | 'raw' | ((value: any) => string); getTitle?: string | ((obj: T) => string); getDetailClass?: (obj: T) => string; getTitleStyleClass?: (obj: T) => string; getValueStyleClass?: (obj: T) => string; titleComponent?: Type; titleComponentInputs?: (obj: T) => { [key: string]: any }; } export interface DetailViewConfig { data: T; rows: DetailViewRow[]; styleClass?: string; } @Component({ selector: 'app-detail-view', imports: [CommonModule], templateUrl: './detail-view.html', styleUrl: './detail-view.css', }) export class DetailView { config = input.required>(); getFormattedValue(row: DetailViewRow, data: T): string { let value : any; try { value = row.getValue ? row.getValue(data) : data[row.attribute]; }catch (e) { value = ''; } if (row.component) { return ''; } if (typeof row.format === 'function') { return row.format(value); } switch (row.format) { case 'date': return new Date(value).toLocaleDateString(); case 'datetime': return new Date(value).toLocaleString(); case 'number': return Number(value).toString(); case 'raw': default: return value; } } getTitle(row: DetailViewRow, data: T): string { if (row.titleComponent) { return ''; } if (typeof row.getTitle === 'function') { return row.getTitle(data); } return row.getTitle || String(row.attribute); } }