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',
31 PUBLIC_FAVORITES = 'Public Favorites'
34 export const createTree = <T>(): Tree<T> => ({});
36 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
38 export const appendSubtree = <T>(id: string, subtree: Tree<T>) => (tree: Tree<T>) =>
40 getNodeDescendants(''),
41 map(node => node.parent === '' ? { ...node, parent: id } : node),
42 reduce((newTree, node) => setNode(node)(newTree), tree)
43 )(subtree) as Tree<T>;
45 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
46 if (tree[node.id] && tree[node.id] === node) { return tree; }
49 if (tree[node.parent]) {
50 tree[node.parent].children = Array.from(new Set([...tree[node.parent].children, node.id]));
55 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
56 const node = getNode(id)(tree);
57 return node ? node.value : undefined;
60 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
61 const node = getNode(id)(tree);
63 ? setNode(mapNodeValue(() => value)(node))(tree)
67 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
68 const node = getNode(id)(tree);
70 ? setNode(mapNodeValue(mapFn)(node))(tree)
74 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
75 getNodeDescendantsIds('')(tree)
76 .map(id => getNode(id)(tree))
77 .map(mapNodeValue(mapFn))
78 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
80 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
81 getNodeDescendantsIds('')(tree)
82 .map(id => getNode(id)(tree))
84 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
86 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
87 mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
90 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
91 const node = getNode(id)(tree);
92 return node && node.parent
93 ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
97 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
98 mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
100 export const countNodes = <T>(tree: Tree<T>) =>
101 getNodeDescendantsIds('')(tree).length;
103 export const countChildren = (id: string) => <T>(tree: Tree<T>) =>
104 getNodeChildren('')(tree).length;
106 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
107 const node = getNode(id)(tree);
108 const children = node ? node.children :
110 ? getRootNodeChildrenIds(tree)
117 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
118 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
121 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
122 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
124 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
125 getNodeDescendantsIds(id, 0)(tree);
127 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
128 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
130 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
131 mapTree((node: TreeNode<T>) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
133 export const deactivateNode = <T>(tree: Tree<T>) =>
134 mapTree((node: TreeNode<T>) => node.active ? { ...node, active: false } : node)(tree);
136 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
137 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
139 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
140 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
142 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
143 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
145 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
146 const node = getNode(id)(tree);
148 ? setNode({ ...node, status })(tree)
152 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
153 const node = getNode(id)(tree);
156 setNode({ ...node, selected: !node.selected }),
157 toggleAncestorsSelection(id),
158 toggleDescendantsSelection(id))(tree)
162 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
163 const node = getNode(id)(tree);
164 return node && node.selected
166 : toggleNodeSelection(id)(tree);
169 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
170 const ids = typeof id === 'string' ? [id] : id;
171 return ids.reduce((tree, id) => selectNode(id)(tree), tree);
173 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
174 const node = getNode(id)(tree);
175 return node && node.selected
176 ? toggleNodeSelection(id)(tree)
180 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
181 const ids = typeof id === 'string' ? [id] : id;
182 return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
185 export const getSelectedNodes = <T>(tree: Tree<T>) =>
186 getNodeDescendants('')(tree)
187 .filter(node => node.selected);
189 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
194 status: TreeNodeStatus.INITIAL,
199 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
200 const node = getNode(id)(tree);
202 return getNodeDescendants(id)(tree)
203 .reduce((newTree, subNode) =>
204 setNode({ ...subNode, selected: node.selected })(newTree),
210 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
211 const ancestors = getNodeAncestorsIds(id)(tree).reverse();
212 return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
215 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
216 const node = getNode(id)(tree);
218 const parentNode = getNode(node.id)(tree);
220 const selected = parentNode.children
221 .map(id => getNode(id)(tree))
222 .every(node => node !== undefined && node.selected);
223 return setNode({ ...parentNode, selected })(tree);
225 return setNode(node)(tree);
231 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
232 ({ ...node, value: mapFn(node.value) });
234 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
237 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);