UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

1 lines • 5.02 kB
{"version":3,"file":"utils.mjs","names":[],"sources":["../../../src/base-ui/Tree/utils.ts"],"sourcesContent":["import type { TreeDataNode, TreeFlatRow } from './type';\n\nexport const hasChildren = (node: TreeDataNode) =>\n !node.isLeaf && !!node.children && node.children.length > 0;\n\nexport const flattenVisible = (\n data: TreeDataNode[],\n expanded: Set<string>,\n depth = 0,\n parentKey: string | null = null,\n trail: boolean[] = [],\n out: TreeFlatRow[] = [],\n): TreeFlatRow[] => {\n data.forEach((node, i) => {\n const isLast = i === data.length - 1;\n const expandable = hasChildren(node);\n out.push({ depth, hasChildren: expandable, index: out.length, isLast, node, parentKey, trail });\n if (expandable && expanded.has(node.key)) {\n flattenVisible(node.children!, expanded, depth + 1, node.key, [...trail, !isLast], out);\n }\n });\n return out;\n};\n\nexport const getAllKeys = (data: TreeDataNode[], out: string[] = []): string[] => {\n for (const node of data) {\n out.push(node.key);\n if (node.children) getAllKeys(node.children, out);\n }\n return out;\n};\n\nexport const findNode = (data: TreeDataNode[], key: string): TreeDataNode | undefined => {\n for (const node of data) {\n if (node.key === key) return node;\n const found = node.children && findNode(node.children, key);\n if (found) return found;\n }\n};\n\nconst isCheckable = (node: TreeDataNode) => node.checkable !== false && !node.disabled;\n\nexport const getSubtreeKeys = (node: TreeDataNode, out: string[] = []): string[] => {\n if (isCheckable(node)) out.push(node.key);\n node.children?.forEach((child) => getSubtreeKeys(child, out));\n return out;\n};\n\nexport const conductCheck = (data: TreeDataNode[], checkedKeys: Iterable<string>) => {\n const checked = new Set(checkedKeys);\n const halfChecked = new Set<string>();\n\n const down = (node: TreeDataNode, inherited: boolean) => {\n const on = inherited || checked.has(node.key);\n if (on && isCheckable(node)) checked.add(node.key);\n node.children?.forEach((child) => down(child, on && isCheckable(node)));\n };\n data.forEach((node) => down(node, false));\n\n const walk = (node: TreeDataNode): boolean => {\n const kids = (node.children ?? []).filter(isCheckable);\n if (kids.length === 0) return checked.has(node.key);\n const results = kids.map(walk);\n if (results.every(Boolean)) {\n checked.add(node.key);\n return true;\n }\n checked.delete(node.key);\n if (results.some(Boolean) || kids.some((kid) => halfChecked.has(kid.key))) {\n halfChecked.add(node.key);\n }\n return false;\n };\n\n data.forEach(walk);\n return { checked, halfChecked };\n};\n\nexport const getAncestorKeys = (\n data: TreeDataNode[],\n key: string,\n path: string[] = [],\n): string[] => {\n for (const node of data) {\n if (node.key === key) return [...path].reverse();\n if (node.children) {\n const found = getAncestorKeys(node.children, key, [...path, node.key]);\n if (found.length > 0 || node.children.some((c) => c.key === key)) return found;\n }\n }\n return [];\n};\n"],"mappings":";AAEA,MAAa,eAAe,SAC1B,CAAC,KAAK,UAAU,CAAC,CAAC,KAAK,YAAY,KAAK,SAAS,SAAS;AAE5D,MAAa,kBACX,MACA,UACA,QAAQ,GACR,YAA2B,MAC3B,QAAmB,CAAC,GACpB,MAAqB,CAAC,MACJ;CAClB,KAAK,SAAS,MAAM,MAAM;EACxB,MAAM,SAAS,MAAM,KAAK,SAAS;EACnC,MAAM,aAAa,YAAY,IAAI;EACnC,IAAI,KAAK;GAAE;GAAO,aAAa;GAAY,OAAO,IAAI;GAAQ;GAAQ;GAAM;GAAW;EAAM,CAAC;EAC9F,IAAI,cAAc,SAAS,IAAI,KAAK,GAAG,GACrC,eAAe,KAAK,UAAW,UAAU,QAAQ,GAAG,KAAK,KAAK,CAAC,GAAG,OAAO,CAAC,MAAM,GAAG,GAAG;CAE1F,CAAC;CACD,OAAO;AACT;AAEA,MAAa,cAAc,MAAsB,MAAgB,CAAC,MAAgB;CAChF,KAAK,MAAM,QAAQ,MAAM;EACvB,IAAI,KAAK,KAAK,GAAG;EACjB,IAAI,KAAK,UAAU,WAAW,KAAK,UAAU,GAAG;CAClD;CACA,OAAO;AACT;AAUA,MAAM,eAAe,SAAuB,KAAK,cAAc,SAAS,CAAC,KAAK;AAE9E,MAAa,kBAAkB,MAAoB,MAAgB,CAAC,MA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