1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { pipe, map, reduce } 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 enum TreePickerId {
28 PROJECTS = 'Projects',
29 SHARED_WITH_ME = 'Shared with me',
30 FAVORITES = 'Favorites'
33 export const createTree = <T>(): Tree<T> => ({});
35 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
37 export const appendSubtree = <T>(id: string, subtree: Tree<T>) => (tree: Tree<T>) =>
39 getNodeDescendants(''),
40 map(node => node.parent === '' ? { ...node, parent: id } : node),
41 reduce((newTree, node) => setNode(node)(newTree), tree)
42 )(subtree) as Tree<T>;
44 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
46 (tree: Tree<T>) => getNode(node.id)(tree) === node
48 : { ...tree, [node.id]: node },
49 addChild(node.parent, node.id)
53 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
54 const node = getNode(id)(tree);
55 return node ? node.value : undefined;
58 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
59 const node = getNode(id)(tree);
61 ? setNode(mapNodeValue(() => value)(node))(tree)
65 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
66 const node = getNode(id)(tree);
68 ? setNode(mapNodeValue(mapFn)(node))(tree)
72 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
73 getNodeDescendantsIds('')(tree)
74 .map(id => getNode(id)(tree))
75 .map(mapNodeValue(mapFn))
76 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
78 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
79 getNodeDescendantsIds('')(tree)
80 .map(id => getNode(id)(tree))
82 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
84 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
85 mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
88 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
89 const node = getNode(id)(tree);
90 return node && node.parent
91 ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
95 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
96 mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
98 export const countNodes = <T>(tree: Tree<T>) =>
99 getNodeDescendantsIds('')(tree).length;
101 export const countChildren = (id: string) => <T>(tree: Tree<T>) =>
102 getNodeChildren('')(tree).length;
104 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
105 const node = getNode(id)(tree);
106 const children = node ? node.children :
108 ? getRootNodeChildrenIds(tree)
115 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
116 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
119 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
120 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
122 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
123 getNodeDescendantsIds(id, 0)(tree);
125 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
126 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
128 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
129 mapTree((node: TreeNode<T>) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
131 export const deactivateNode = <T>(tree: Tree<T>) =>
132 mapTree((node: TreeNode<T>) => node.active ? { ...node, active: false } : node)(tree);
134 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
135 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
137 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
138 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
140 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
141 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
143 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
144 const node = getNode(id)(tree);
146 ? setNode({ ...node, status })(tree)
150 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
151 const node = getNode(id)(tree);
154 setNode({ ...node, selected: !node.selected }),
155 toggleAncestorsSelection(id),
156 toggleDescendantsSelection(id))(tree)
161 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
162 const node = getNode(id)(tree);
163 return node && node.selected
165 : toggleNodeSelection(id)(tree);
168 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
169 const ids = typeof id === 'string' ? [id] : id;
170 return ids.reduce((tree, id) => selectNode(id)(tree), tree);
172 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
173 const node = getNode(id)(tree);
174 return node && node.selected
175 ? toggleNodeSelection(id)(tree)
179 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
180 const ids = typeof id === 'string' ? [id] : id;
181 return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
184 export const getSelectedNodes = <T>(tree: Tree<T>) =>
185 getNodeDescendants('')(tree)
186 .filter(node => node.selected);
188 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
193 status: TreeNodeStatus.INITIAL,
198 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
199 const node = getNode(id)(tree);
201 return getNodeDescendants(id)(tree)
202 .reduce((newTree, subNode) =>
203 setNode({ ...subNode, selected: node.selected })(newTree),
209 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
210 const ancestors = getNodeAncestorsIds(id)(tree).reverse();
211 return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
214 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
215 const node = getNode(id)(tree);
217 const parentNode = getNode(node.id)(tree);
219 const selected = parentNode.children
220 .map(id => getNode(id)(tree))
221 .every(node => node !== undefined && node.selected);
222 return setNode({ ...parentNode, selected })(tree);
224 return setNode(node)(tree);
230 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
231 ({ ...node, value: mapFn(node.value) });
233 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
236 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
239 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
240 const node = getNode(parentId)(tree);
242 const children = node.children.some(id => id === childId)
244 : [...node.children, childId];
246 const newNode = children === node.children
248 : { ...node, children };
250 return setNode(newNode)(tree);