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 getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
102 const node = getNode(id)(tree);
103 const children = node ? node.children :
105 ? getRootNodeChildrenIds(tree)
112 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
113 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
116 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
117 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
119 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
120 getNodeDescendantsIds(id, 0)(tree);
122 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
123 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
125 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
126 mapTree((node: TreeNode<T>) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
128 export const deactivateNode = <T>(tree: Tree<T>) =>
129 mapTree((node: TreeNode<T>) => node.active ? { ...node, active: false } : node)(tree);
131 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
132 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
134 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
135 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
137 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
138 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
140 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
141 const node = getNode(id)(tree);
143 ? setNode({ ...node, status })(tree)
147 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
148 const node = getNode(id)(tree);
151 setNode({ ...node, selected: !node.selected }),
152 toggleAncestorsSelection(id),
153 toggleDescendantsSelection(id))(tree)
158 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
159 const node = getNode(id)(tree);
160 return node && node.selected
162 : toggleNodeSelection(id)(tree);
165 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
166 const ids = typeof id === 'string' ? [id] : id;
167 return ids.reduce((tree, id) => selectNode(id)(tree), tree);
169 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
170 const node = getNode(id)(tree);
171 return node && node.selected
172 ? toggleNodeSelection(id)(tree)
176 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
177 const ids = typeof id === 'string' ? [id] : id;
178 return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
181 export const getSelectedNodes = <T>(tree: Tree<T>) =>
182 getNodeDescendants('')(tree)
183 .filter(node => node.selected);
185 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
190 status: TreeNodeStatus.INITIAL,
195 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
196 const node = getNode(id)(tree);
198 return getNodeDescendants(id)(tree)
199 .reduce((newTree, subNode) =>
200 setNode({ ...subNode, selected: node.selected })(newTree),
206 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
207 const ancestors = getNodeAncestorsIds(id)(tree).reverse();
208 return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
211 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
212 const node = getNode(id)(tree);
214 const parentNode = getNode(node.id)(tree);
216 const selected = parentNode.children
217 .map(id => getNode(id)(tree))
218 .every(node => node !== undefined && node.selected);
219 return setNode({ ...parentNode, selected })(tree);
221 return setNode(node)(tree);
227 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
228 ({ ...node, value: mapFn(node.value) });
230 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
233 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
236 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
237 const node = getNode(parentId)(tree);
239 const children = node.children.some(id => id === childId)
241 : [...node.children, childId];
243 const newNode = children === node.children
245 : { ...node, children };
247 return setNode(newNode)(tree);