1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { pipe } 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 setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
39 (tree: Tree<T>) => getNode(node.id)(tree) === node
41 : { ...tree, [node.id]: node },
42 addChild(node.parent, node.id)
46 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
47 const node = getNode(id)(tree);
48 return node ? node.value : undefined;
51 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
52 const node = getNode(id)(tree);
54 ? setNode(mapNodeValue(() => value)(node))(tree)
58 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
59 const node = getNode(id)(tree);
61 ? setNode(mapNodeValue(mapFn)(node))(tree)
65 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
66 getNodeDescendantsIds('')(tree)
67 .map(id => getNode(id)(tree))
68 .map(mapNodeValue(mapFn))
69 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
71 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
72 getNodeDescendantsIds('')(tree)
73 .map(id => getNode(id)(tree))
75 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
77 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
78 mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
81 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
82 const node = getNode(id)(tree);
83 return node && node.parent
84 ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
88 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
89 mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
91 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
92 const node = getNode(id)(tree);
93 const children = node ? node.children :
95 ? getRootNodeChildrenIds(tree)
102 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
103 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
106 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
107 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
109 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
110 getNodeDescendantsIds(id, 0)(tree);
112 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
113 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
115 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
116 mapTree(node => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
118 export const deactivateNode = <T>(tree: Tree<T>) =>
119 mapTree(node => node.active ? { ...node, active: false } : node)(tree);
121 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
122 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
124 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
125 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
127 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
128 mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
130 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
131 const node = getNode(id)(tree);
133 ? setNode({ ...node, status })(tree)
137 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
138 const node = getNode(id)(tree);
141 setNode({ ...node, selected: !node.selected }),
142 toggleAncestorsSelection(id),
143 toggleDescendantsSelection(id))(tree)
148 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
149 const node = getNode(id)(tree);
150 return node && node.selected
152 : toggleNodeSelection(id)(tree);
155 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
156 const ids = typeof id === 'string' ? [id] : id;
157 return ids.reduce((tree, id) => selectNode(id)(tree), tree);
159 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
160 const node = getNode(id)(tree);
161 return node && node.selected
162 ? toggleNodeSelection(id)(tree)
166 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
167 const ids = typeof id === 'string' ? [id] : id;
168 return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
171 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
176 status: TreeNodeStatus.INITIAL,
181 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
182 const node = getNode(id)(tree);
184 return getNodeDescendants(id)(tree)
185 .reduce((newTree, subNode) =>
186 setNode({ ...subNode, selected: node.selected })(newTree),
192 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
193 const ancestors = getNodeAncestorsIds(id)(tree).reverse();
194 return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
197 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
198 const node = getNode(id)(tree);
200 const parentNode = getNode(node.id)(tree);
202 const selected = parentNode.children
203 .map(id => getNode(id)(tree))
204 .every(node => node !== undefined && node.selected);
205 return setNode({ ...parentNode, selected })(tree);
207 return setNode(node)(tree);
213 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
214 ({ ...node, value: mapFn(node.value) });
216 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
219 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
222 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
223 const node = getNode(parentId)(tree);
225 const children = node.children.some(id => id === childId)
227 : [...node.children, childId];
229 const newNode = children === node.children
231 : { ...node, children };
233 return setNode(newNode)(tree);