Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 13862-collection...
[arvados-workbench2.git] / src / models / tree.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { pipe } from 'lodash/fp';
6 export type Tree<T> = Record<string, TreeNode<T>>;
7
8 export const TREE_ROOT_ID = '';
9
10 export interface TreeNode<T = any> {
11     children: string[];
12     value: T;
13     id: string;
14     parent: string;
15     active: boolean;
16     selected: boolean;
17     expanded: boolean;
18     status: TreeNodeStatus;
19 }
20
21 export enum TreeNodeStatus {
22     INITIAL = 'INITIAL',
23     PENDING = 'PENDING',
24     LOADED = 'LOADED',
25 }
26
27 export const createTree = <T>(): Tree<T> => ({});
28
29 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
30
31 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
32     return pipe(
33         (tree: Tree<T>) => getNode(node.id)(tree) === node
34             ? tree
35             : { ...tree, [node.id]: node },
36         addChild(node.parent, node.id)
37     )(tree);
38 };
39
40 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
41     const node = getNode(id)(tree);
42     return node ? node.value : undefined;
43 };
44
45 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
46     const node = getNode(id)(tree);
47     return node
48         ? setNode(mapNodeValue(() => value)(node))(tree)
49         : tree;
50 };
51
52 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
53     const node = getNode(id)(tree);
54     return node
55         ? setNode(mapNodeValue(mapFn)(node))(tree)
56         : tree;
57 };
58
59 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
60     getNodeDescendantsIds('')(tree)
61         .map(id => getNode(id)(tree))
62         .map(mapNodeValue(mapFn))
63         .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
64
65 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
66     getNodeDescendantsIds('')(tree)
67         .map(id => getNode(id)(tree))
68         .map(mapFn)
69         .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
70
71 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
72     mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
73
74
75 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
76     const node = getNode(id)(tree);
77     return node && node.parent
78         ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
79         : [];
80 };
81
82 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
83     mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
84
85 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
86     const node = getNode(id)(tree);
87     const children = node ? node.children :
88         id === TREE_ROOT_ID
89             ? getRootNodeChildren(tree)
90             : [];
91
92     return children
93         .concat(limit < 1
94             ? []
95             : children
96                 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
97                 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
98 };
99
100 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
101     mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
102
103 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
104     getNodeDescendantsIds(id, 0)(tree);
105
106 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
107     ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
108
109 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
110     mapTree(node => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
111
112 export const deactivateNode = <T>(tree: Tree<T>) =>
113     mapTree(node => node.active ? { ...node, active: false } : node)(tree);
114
115 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
116     mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
117
118 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
119     mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
120
121 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
122     mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
123
124 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
125     const node = getNode(id)(tree);
126     return node
127         ? setNode({ ...node, status })(tree)
128         : tree;
129 };
130
131 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
132     const node = getNode(id)(tree);
133     return node
134         ? pipe(
135             setNode({ ...node, selected: !node.selected }),
136             toggleAncestorsSelection(id),
137             toggleDescendantsSelection(id))(tree)
138         : tree;
139
140 };
141
142 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & {parent?: string}): TreeNode<T> => ({
143     children: [],
144     active: false,
145     selected: false,
146     expanded: false,
147     status: TreeNodeStatus.INITIAL,
148     parent: '',
149     ...data,
150 });
151
152 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
153     const node = getNode(id)(tree);
154     if (node) {
155         return getNodeDescendants(id)(tree)
156             .reduce((newTree, subNode) =>
157                 setNode({ ...subNode, selected: node.selected })(newTree),
158                 tree);
159     }
160     return tree;
161 };
162
163 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
164     const ancestors = getNodeAncestorsIds(id)(tree).reverse();
165     return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
166 };
167
168 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
169     const node = getNode(id)(tree);
170     if (node) {
171         const parentNode = getNode(node.id)(tree);
172         if (parentNode) {
173             const selected = parentNode.children
174                 .map(id => getNode(id)(tree))
175                 .every(node => node !== undefined && node.selected);
176             return setNode({ ...parentNode, selected })(tree);
177         }
178         return setNode(node)(tree);
179     }
180     return tree;
181 };
182
183
184 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
185     ({ ...node, value: mapFn(node.value) });
186
187 const getRootNodeChildren = <T>(tree: Tree<T>) =>
188     Object
189         .keys(tree)
190         .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
191
192 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
193     const node = getNode(parentId)(tree);
194     if (node) {
195         const children = node.children.some(id => id === childId)
196             ? node.children
197             : [...node.children, childId];
198
199         const newNode = children === node.children
200             ? node
201             : { ...node, children };
202
203         return setNode(newNode)(tree);
204     }
205     return tree;
206 };