85 lines
2.5 KiB
JavaScript
85 lines
2.5 KiB
JavaScript
// Явный id определяет технический ключ колонки; accessorKey используется как fallback.
|
||
export const getColumnKey = (node) => node?.id ?? node?.accessorKey;
|
||
|
||
// Проверка, нужно ли скрыть колонку из дерева
|
||
export const shouldExcludeFromTree = (node, excludeSet) => {
|
||
if (node?.hideInTree) return true;
|
||
const key = getColumnKey(node);
|
||
return Boolean(key && excludeSet.has(key));
|
||
};
|
||
|
||
// Фильтрация дерева колонок по списку исключений
|
||
export const filterColumnTree = (nodes, excludeColumnKeys = []) => {
|
||
if (!nodes?.length) return [];
|
||
|
||
const excludeSet = new Set(excludeColumnKeys);
|
||
|
||
return nodes.flatMap((node) => {
|
||
if (shouldExcludeFromTree(node, excludeSet)) return [];
|
||
|
||
const hasChildren = Array.isArray(node.columns) && node.columns.length > 0;
|
||
if (!hasChildren) return [node];
|
||
|
||
const filteredChildren = filterColumnTree(node.columns, excludeColumnKeys);
|
||
if (filteredChildren.length === 0) return [];
|
||
|
||
return [{ ...node, columns: filteredChildren }];
|
||
});
|
||
};
|
||
|
||
// Получение accessorKey всех листовых колонок из дерева
|
||
export const getColumnTreeLeafKeys = (nodes) => {
|
||
const keys = [];
|
||
|
||
const collect = (cols) => {
|
||
for (const col of cols) {
|
||
if (col.columns?.length) {
|
||
collect(col.columns);
|
||
} else {
|
||
const key = getColumnKey(col);
|
||
if (key) keys.push(key);
|
||
}
|
||
}
|
||
};
|
||
|
||
collect(nodes);
|
||
return keys;
|
||
};
|
||
|
||
// Сбор листовых колонок
|
||
export const getLeafColumns = (targetColumn) => {
|
||
if (!targetColumn) return [];
|
||
|
||
const collectLeafColumns = (col) => {
|
||
if (!col.subHeaders?.length) return [col];
|
||
return col.subHeaders.flatMap(collectLeafColumns);
|
||
};
|
||
|
||
return collectLeafColumns(targetColumn);
|
||
};
|
||
|
||
// Сбор всех листовых колонок из иерархии
|
||
export const collectLeafColumns = (column) => {
|
||
if (column.columns?.length === 0 || !column.columns) return [column];
|
||
if (column.columns?.length > 0 && (column.columns[0]?.columns?.length === 0 || !column.columns)) {
|
||
return column.columns;
|
||
}
|
||
return column.columns?.flatMap((childCol) => collectLeafColumns(childCol)) || [];
|
||
};
|
||
|
||
// Получение всех ID листовых колонок
|
||
export const getLeafColumnIds = (columns) => {
|
||
const leafIds = [];
|
||
|
||
const collect = (col) => {
|
||
if (!col.columns?.length) {
|
||
leafIds.push(col.id);
|
||
} else {
|
||
col.columns.forEach(collect);
|
||
}
|
||
};
|
||
|
||
columns.forEach(collect);
|
||
return leafIds;
|
||
};
|