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 getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
99 const node = getNode(id)(tree);
100 const children = node ? node.children :
102 ? getRootNodeChildrenIds(tree)
109 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
110 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
113 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
114 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
116 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
117 getNodeDescendantsIds(id, 0)(tree);
119 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
120 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
122 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
123 mapTree(node => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
125 export const deactivateNode = <T>(tree: Tree<T>) =>
126 mapTree(node => node.active ? { ...node, active: false } : node)(tree);
128 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
129 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
131 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
132 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
134 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
135 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
137 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
138 const node = getNode(id)(tree);
140 ? setNode({ ...node, status })(tree)
144 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
145 const node = getNode(id)(tree);
148 setNode({ ...node, selected: !node.selected }),
149 toggleAncestorsSelection(id),
150 toggleDescendantsSelection(id))(tree)
155 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
156 const node = getNode(id)(tree);
157 return node && node.selected
159 : toggleNodeSelection(id)(tree);
162 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
163 const ids = typeof id === 'string' ? [id] : id;
164 return ids.reduce((tree, id) => selectNode(id)(tree), tree);
166 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
167 const node = getNode(id)(tree);
168 return node && node.selected
169 ? toggleNodeSelection(id)(tree)
173 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
174 const ids = typeof id === 'string' ? [id] : id;
175 return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
178 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
183 status: TreeNodeStatus.INITIAL,
188 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
189 const node = getNode(id)(tree);
191 return getNodeDescendants(id)(tree)
192 .reduce((newTree, subNode) =>
193 setNode({ ...subNode, selected: node.selected })(newTree),
199 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
200 const ancestors = getNodeAncestorsIds(id)(tree).reverse();
201 return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
204 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
205 const node = getNode(id)(tree);
207 const parentNode = getNode(node.id)(tree);
209 const selected = parentNode.children
210 .map(id => getNode(id)(tree))
211 .every(node => node !== undefined && node.selected);
212 return setNode({ ...parentNode, selected })(tree);
214 return setNode(node)(tree);
220 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
221 ({ ...node, value: mapFn(node.value) });
223 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
226 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
229 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
230 const node = getNode(parentId)(tree);
232 const children = node.children.some(id => id === childId)
234 : [...node.children, childId];
236 const newNode = children === node.children
238 : { ...node, children };
240 return setNode(newNode)(tree);