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 .filter(node => !!node)
78 .map(mapNodeValue(mapFn))
79 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
81 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
82 getNodeDescendantsIds('')(tree)
83 .map(id => getNode(id)(tree))
85 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
87 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
88 mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
91 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
92 const node = getNode(id)(tree);
93 return node && node.parent
94 ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
98 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
99 mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
101 export const countNodes = <T>(tree: Tree<T>) =>
102 getNodeDescendantsIds('')(tree).length;
104 export const countChildren = (id: string) => <T>(tree: Tree<T>) =>
105 getNodeChildren('')(tree).length;
107 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
108 const node = getNode(id)(tree);
109 const children = node ? node.children :
111 ? getRootNodeChildrenIds(tree)
118 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
119 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
122 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
123 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
125 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
126 getNodeDescendantsIds(id, 0)(tree);
128 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
129 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
131 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
132 mapTree((node: TreeNode<T>) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
134 export const deactivateNode = <T>(tree: Tree<T>) =>
135 mapTree((node: TreeNode<T>) => node.active ? { ...node, active: false } : node)(tree);
137 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
138 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
140 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
141 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
143 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
144 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
146 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
147 const node = getNode(id)(tree);
149 ? setNode({ ...node, status })(tree)
153 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
154 const node = getNode(id)(tree);
157 setNode({ ...node, selected: !node.selected }),
158 toggleAncestorsSelection(id),
159 toggleDescendantsSelection(id))(tree)
163 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
164 const node = getNode(id)(tree);
165 return node && node.selected
167 : toggleNodeSelection(id)(tree);
170 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
171 const ids = typeof id === 'string' ? [id] : id;
172 return ids.reduce((tree, id) => selectNode(id)(tree), tree);
174 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
175 const node = getNode(id)(tree);
176 return node && node.selected
177 ? toggleNodeSelection(id)(tree)
181 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
182 const ids = typeof id === 'string' ? [id] : id;
183 return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
186 export const getSelectedNodes = <T>(tree: Tree<T>) =>
187 getNodeDescendants('')(tree)
188 .filter(node => node.selected);
190 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
195 status: TreeNodeStatus.INITIAL,
200 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
201 const node = getNode(id)(tree);
203 return getNodeDescendants(id)(tree)
204 .reduce((newTree, subNode) =>
205 setNode({ ...subNode, selected: node.selected })(newTree),
211 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
212 const ancestors = getNodeAncestorsIds(id)(tree).reverse();
213 return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
216 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
217 const node = getNode(id)(tree);
219 const parentNode = getNode(node.id)(tree);
221 const selected = parentNode.children
222 .map(id => getNode(id)(tree))
223 .every(node => node !== undefined && node.selected);
224 return setNode({ ...parentNode, selected })(tree);
226 return setNode(node)(tree);
232 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
233 ({ ...node, value: mapFn(node.value) });
235 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
238 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);