UNPKG

@1771technologies/lytenyte-pro

Version:

Blazingly fast headless React data grid with 100s of features.

77 lines (76 loc) 2.61 kB
import { BRANCH, LEAF } from "../+constants.js"; import { traverse } from "./traverse.js"; import { aggregationEvaluator } from "./evaluator-aggregation.js"; import { groupEvaluator } from "./evaluator-group.js"; export function makeClientTree({ rowData, rowBranchModel, rowAggModel, rowIdLeaf, rowIdGroup, }) { const root = new Map(); const idsLeaf = new Set(); const idsBranch = new Set(); const idsAll = new Set(); const idToNode = new Map(); const idToSourceIndex = new Map(); for (let i = 0; i < rowData.length; i++) { const d = rowData[i]; const id = rowIdLeaf?.(d, i) ?? `${i}`; const path = groupEvaluator(rowBranchModel, d); idsLeaf.add(id); idsAll.add(id); let current = root; const runningPath = []; for (let i = 0; i < path.length; i++) { const pathKey = path[i] ?? `<__null__>`; runningPath.push(pathKey); const lookup = i === 0 ? root : current.children; // We need to create a branch node. if (!lookup.has(pathKey)) { const id = rowIdGroup?.(runningPath) ?? "+.+" + runningPath.join("/"); idsBranch.add(id); idsAll.add(id); const branch = { id, kind: BRANCH, depth: i, children: new Map(), data: {}, parent: current === root ? null : current, key: pathKey, leafIds: new Set(), leafData: [], }; lookup.set(pathKey, branch); idToNode.set(id, branch); } const branch = lookup.get(pathKey); branch.leafData.push(d); branch.leafIds.add(id); current = branch; } const leaf = { kind: LEAF, id, depth: path.length, data: d, parent: current === root ? null : current, }; idToNode.set(id, leaf); idToSourceIndex.set(id, i); if (rowBranchModel.length === 0) current.set(id, leaf); else current.children.set(id, leaf); } traverse(root, (node) => { if (node.kind === LEAF) return; node.data = aggregationEvaluator(rowAggModel, node.leafData); }); return { root, rowData, idsLeaf: idsLeaf, idsBranch: idsBranch, idsAll: idsAll, idToNode, idToSourceIndex, }; }