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> => {
47 (tree: Tree<T>) => getNode(node.id)(tree) === node
49 : { ...tree, [node.id]: node },
50 addChild(node.parent, node.id)
54 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
55 const node = getNode(id)(tree);
56 return node ? node.value : undefined;
59 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
60 const node = getNode(id)(tree);
62 ? setNode(mapNodeValue(() => value)(node))(tree)
66 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
67 const node = getNode(id)(tree);
69 ? setNode(mapNodeValue(mapFn)(node))(tree)
73 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
74 getNodeDescendantsIds('')(tree)
75 .map(id => getNode(id)(tree))
76 .map(mapNodeValue(mapFn))
77 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
79 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
80 getNodeDescendantsIds('')(tree)
81 .map(id => getNode(id)(tree))
83 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
85 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
86 mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
89 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
90 const node = getNode(id)(tree);
91 return node && node.parent
92 ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
96 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
97 mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
99 export const countNodes = <T>(tree: Tree<T>) =>
100 getNodeDescendantsIds('')(tree).length;
102 export const countChildren = (id: string) => <T>(tree: Tree<T>) =>
103 getNodeChildren('')(tree).length;
105 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
106 const node = getNode(id)(tree);
107 const children = node ? node.children :
109 ? getRootNodeChildrenIds(tree)
116 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
117 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
120 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
121 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
123 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
124 getNodeDescendantsIds(id, 0)(tree);
126 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
127 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
129 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
130 mapTree((node: TreeNode<T>) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
132 export const deactivateNode = <T>(tree: Tree<T>) =>
133 mapTree((node: TreeNode<T>) => node.active ? { ...node, active: false } : node)(tree);
135 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
136 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
138 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
139 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
141 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
142 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
144 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
145 const node = getNode(id)(tree);
147 ? setNode({ ...node, status })(tree)
151 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
152 const node = getNode(id)(tree);
155 setNode({ ...node, selected: !node.selected }),
156 toggleAncestorsSelection(id),
157 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);
240 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
241 const node = getNode(parentId)(tree);
243 const children = node.children.some(id => id === childId)
245 : [...node.children, childId];
247 const newNode = children === node.children
249 : { ...node, children };
251 return setNode(newNode)(tree);