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> {
17 initialState?: boolean;
19 status: TreeNodeStatus;
22 export enum TreeNodeStatus {
28 export enum TreePickerId {
29 PROJECTS = 'Projects',
30 SHARED_WITH_ME = 'Shared with me',
31 FAVORITES = 'Favorites',
32 PUBLIC_FAVORITES = 'Public Favorites'
35 export const createTree = <T>(): Tree<T> => ({});
37 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
39 export const appendSubtree = <T>(id: string, subtree: Tree<T>) => (tree: Tree<T>) =>
41 getNodeDescendants(''),
42 map(node => node.parent === '' ? { ...node, parent: id } : node),
43 reduce((newTree, node) => setNode(node)(newTree), tree)
44 )(subtree) as Tree<T>;
46 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
47 if (tree[node.id] && tree[node.id] === node) { return tree; }
50 if (tree[node.parent]) {
51 tree[node.parent].children = Array.from(new Set([...tree[node.parent].children, node.id]));
56 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
57 const node = getNode(id)(tree);
58 return node ? node.value : undefined;
61 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
62 const node = getNode(id)(tree);
64 ? setNode(mapNodeValue(() => value)(node))(tree)
68 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
69 const node = getNode(id)(tree);
71 ? setNode(mapNodeValue(mapFn)(node))(tree)
75 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
76 getNodeDescendantsIds('')(tree)
77 .map(id => getNode(id)(tree))
78 .filter(node => !!node)
79 .map(mapNodeValue(mapFn))
80 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
82 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
83 getNodeDescendantsIds('')(tree)
84 .map(id => getNode(id)(tree))
86 .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
88 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
89 mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
92 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
93 const node = getNode(id)(tree);
94 return node && node.parent
95 ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
99 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
100 mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
102 export const countNodes = <T>(tree: Tree<T>) =>
103 getNodeDescendantsIds('')(tree).length;
105 export const countChildren = (id: string) => <T>(tree: Tree<T>) =>
106 getNodeChildren('')(tree).length;
108 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
109 const node = getNode(id)(tree);
110 const children = node ? node.children :
112 ? getRootNodeChildrenIds(tree)
119 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
120 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
123 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
124 mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
126 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
127 getNodeDescendantsIds(id, 0)(tree);
129 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
130 ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
132 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
133 mapTree((node: TreeNode<T>) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
135 export const deactivateNode = <T>(tree: Tree<T>) =>
136 mapTree((node: TreeNode<T>) => node.active ? { ...node, active: false } : node)(tree);
138 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
139 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
141 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
142 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
144 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
145 mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
147 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
148 const node = getNode(id)(tree);
150 ? setNode({ ...node, status })(tree)
154 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
155 const node = getNode(id)(tree);
158 setNode({ ...node, selected: !node.selected }),
159 toggleAncestorsSelection(id),
160 toggleDescendantsSelection(id))(tree)
164 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
165 const node = getNode(id)(tree);
166 return node && node.selected
168 : toggleNodeSelection(id)(tree);
171 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
172 const ids = typeof id === 'string' ? [id] : id;
173 return ids.reduce((tree, id) => selectNode(id)(tree), tree);
175 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
176 const node = getNode(id)(tree);
177 return node && node.selected
178 ? toggleNodeSelection(id)(tree)
182 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
183 const ids = typeof id === 'string' ? [id] : id;
184 return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
187 export const getSelectedNodes = <T>(tree: Tree<T>) =>
188 getNodeDescendants('')(tree)
189 .filter(node => node.selected);
191 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
196 status: TreeNodeStatus.INITIAL,
201 export const getTreeDirty = (id: string) => <T>(tree: Tree<T>): boolean => {
202 const node = getNode(id)(tree);
203 const children = getNodeDescendants(id)(tree);
205 && node.initialState !== undefined
206 && node.selected !== node.initialState
208 || children.some(child =>
209 child.initialState !== undefined
210 && child.selected !== child.initialState
214 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
215 const node = getNode(id)(tree);
217 return getNodeDescendants(id)(tree)
218 .reduce((newTree, subNode) =>
219 setNode({ ...subNode, selected: node.selected })(newTree),
225 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
226 const ancestors = getNodeAncestorsIds(id)(tree).reverse();
227 return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
230 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
231 const node = getNode(id)(tree);
233 const parentNode = getNode(node.id)(tree);
235 const selected = parentNode.children
236 .map(id => getNode(id)(tree))
237 .every(node => node !== undefined && node.selected);
238 return setNode({ ...parentNode, selected })(tree);
240 return setNode(node)(tree);
245 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
246 ({ ...node, value: mapFn(node.value) });
248 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
251 .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);