78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
import type { CnstpSeRow } from "./cnstp-se-row-types";
|
|
|
|
export interface CnstpSeDisplayRow {
|
|
row: CnstpSeRow;
|
|
depth: number;
|
|
}
|
|
|
|
export function formatCdLvNo(lvNo?: number): string {
|
|
return `${lvNo ?? 1}단계`;
|
|
}
|
|
|
|
export function canAddChildRow(): boolean {
|
|
return true;
|
|
}
|
|
|
|
export function formatTreeBranchPrefix(depth: number): string {
|
|
if (depth <= 0) return "";
|
|
const vertical = "\u2502 ";
|
|
const branch = "\u251C\u2500";
|
|
return vertical.repeat(depth - 1) + branch;
|
|
}
|
|
|
|
function isRootRow(row: CnstpSeRow): boolean {
|
|
return (row.cdLvNo ?? 1) === 1 && !row.upCnstpCd?.trim();
|
|
}
|
|
|
|
function rowOrderIndex(rows: CnstpSeRow[], row: CnstpSeRow): number {
|
|
return rows.findIndex((candidate) => candidate.rowKey === row.rowKey);
|
|
}
|
|
|
|
function pushDisplayRow(
|
|
result: CnstpSeDisplayRow[],
|
|
displayedRowKeys: Set<string>,
|
|
row: CnstpSeRow,
|
|
depth: number,
|
|
) {
|
|
if (displayedRowKeys.has(row.rowKey)) return;
|
|
displayedRowKeys.add(row.rowKey);
|
|
result.push({ row, depth });
|
|
}
|
|
|
|
export function buildCnstpDisplayRows(rows: CnstpSeRow[]): CnstpSeDisplayRow[] {
|
|
const result: CnstpSeDisplayRow[] = [];
|
|
const displayedRowKeys = new Set<string>();
|
|
|
|
const roots = rows.filter(isRootRow);
|
|
|
|
function appendSubtree(parent: CnstpSeRow, depth: number) {
|
|
const children = rows
|
|
.filter(
|
|
(row) =>
|
|
row.rowKey !== parent.rowKey &&
|
|
row.upCnstpCd != null &&
|
|
row.upCnstpCd === parent.cnstpSeCd &&
|
|
row.cnstpSeCd !== parent.cnstpSeCd,
|
|
)
|
|
.sort((a, b) => rowOrderIndex(rows, a) - rowOrderIndex(rows, b));
|
|
|
|
for (const child of children) {
|
|
pushDisplayRow(result, displayedRowKeys, child, depth);
|
|
appendSubtree(child, depth + 1);
|
|
}
|
|
}
|
|
|
|
for (const root of roots) {
|
|
pushDisplayRow(result, displayedRowKeys, root, 0);
|
|
appendSubtree(root, 1);
|
|
}
|
|
|
|
const orphans = rows.filter((row) => !displayedRowKeys.has(row.rowKey));
|
|
|
|
for (const orphan of orphans) {
|
|
pushDisplayRow(result, displayedRowKeys, orphan, Math.max(0, (orphan.cdLvNo ?? 1) - 1));
|
|
}
|
|
|
|
return result;
|
|
}
|