1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { pipe } from 'lodash/fp';
6 export type Tree<T> = Record<string, TreeNode<T>>;
8 export const TREE_ROOT_ID = '';
10 export interface TreeNode<T = any> {
18 status: TreeNodeStatus;
21 export enum TreeNodeStatus {
27 export const createTree = <T>(): Tree<T> => ({});
29 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
31 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
33 (tree: Tree<T>) => getNode(node.id)(tree) === node
35 : { ...tree, [node.id]: node },
36 addChild(node.parent, node.id)
40 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
41 const node = getNode(id)(tree);
42 return node ? node.value : undefined;
45 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
46 const node = getNode(id)(tree);
48 ? setNode(mapNodeValue(() => value)(node))(tree)
52 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
53 const node = getNode(id)(tree);
55 ? setNode(mapNodeValue(mapFn)(node))(tree)
59 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
60 getNodeDescendantsIds('')(tree)
61 .map(id => getNode(id)(tree))
62 .map(mapNodeValue(mapFn))
63 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
65 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
66 getNodeDescendantsIds('')(tree)
67 .map(id => getNode(id)(tree))
69 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
71 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
72 mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
75 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
76 const node = getNode(id)(tree);
77 return node && node.parent
78 ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
82 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
83 mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
85 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
86 const node = getNode(id)(tree);
87 const children = node ? node.children :
89 ? getRootNodeChildren(tree)
96 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
97 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
100 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
101 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
103 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
104 getNodeDescendantsIds(id, 0)(tree);
106 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
107 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
109 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
110 mapTree(node => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
112 export const deactivateNode = <T>(tree: Tree<T>) =>
113 mapTree(node => node.active ? { ...node, active: false } : node)(tree);
115 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
116 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
118 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
119 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
121 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
122 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
124 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
125 const node = getNode(id)(tree);
127 ? setNode({ ...node, status })(tree)
131 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
132 const node = getNode(id)(tree);
135 setNode({ ...node, selected: !node.selected }),
136 toggleAncestorsSelection(id),
137 toggleDescendantsSelection(id))(tree)
142 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & {parent?: string}): TreeNode<T> => ({
147 status: TreeNodeStatus.INITIAL,
152 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
153 const node = getNode(id)(tree);
155 return getNodeDescendants(id)(tree)
156 .reduce((newTree, subNode) =>
157 setNode({ ...subNode, selected: node.selected })(newTree),
163 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
164 const ancestors = getNodeAncestorsIds(id)(tree).reverse();
165 return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
168 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
169 const node = getNode(id)(tree);
171 const parentNode = getNode(node.id)(tree);
173 const selected = parentNode.children
174 .map(id => getNode(id)(tree))
175 .every(node => node !== undefined && node.selected);
176 return setNode({ ...parentNode, selected })(tree);
178 return setNode(node)(tree);
184 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
185 ({ ...node, value: mapFn(node.value) });
187 const getRootNodeChildren = <T>(tree: Tree<T>) =>
190 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
192 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
193 const node = getNode(parentId)(tree);
195 const children = node.children.some(id => id === childId)
197 : [...node.children, childId];
199 const newNode = children === node.children
201 : { ...node, children };
203 return setNode(newNode)(tree);