Merge branch 'master' into 16848-token-handling-improvements
[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, map, reduce } 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 enum TreePickerId {
28     PROJECTS = 'Projects',
29     SHARED_WITH_ME = 'Shared with me',
30     FAVORITES = 'Favorites',
31     PUBLIC_FAVORITES = 'Public Favorites'
32 }
33
34 export const createTree = <T>(): Tree<T> => ({});
35
36 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
37
38 export const appendSubtree = <T>(id: string, subtree: Tree<T>) => (tree: Tree<T>) =>
39     pipe(
40         getNodeDescendants(''),
41         map(node => node.parent === '' ? { ...node, parent: id } : node),
42         reduce((newTree, node) => setNode(node)(newTree), tree)
43     )(subtree) as Tree<T>;
44
45 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
46     if (tree[node.id] && tree[node.id] === node) { return tree; }
47
48     tree[node.id] = node;
49     if (tree[node.parent]) {
50         tree[node.parent].children = Array.from(new Set([...tree[node.parent].children, node.id]));
51     }
52     return tree;
53 };
54
55 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
56     const node = getNode(id)(tree);
57     return node ? node.value : undefined;
58 };
59
60 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
61     const node = getNode(id)(tree);
62     return node
63         ? setNode(mapNodeValue(() => value)(node))(tree)
64         : tree;
65 };
66
67 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
68     const node = getNode(id)(tree);
69     return node
70         ? setNode(mapNodeValue(mapFn)(node))(tree)
71         : tree;
72 };
73
74 export const mapTreeValues = <T, R>(mapFn: (value: T) => R) => (tree: Tree<T>): Tree<R> =>
75     getNodeDescendantsIds('')(tree)
76         .map(id => getNode(id)(tree))
77         .filter(node => !!node)
78         .map(mapNodeValue(mapFn))
79         .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
80
81 export const mapTree = <T, R = T>(mapFn: (node: TreeNode<T>) => TreeNode<R>) => (tree: Tree<T>): Tree<R> =>
82     getNodeDescendantsIds('')(tree)
83         .map(id => getNode(id)(tree))
84         .map(mapFn)
85         .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
86
87 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
88     mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
89
90
91 export const getNodeAncestorsIds = (id: string) => <T>(tree: Tree<T>): string[] => {
92     const node = getNode(id)(tree);
93     return node && node.parent
94         ? [...getNodeAncestorsIds(node.parent)(tree), node.parent]
95         : [];
96 };
97
98 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
99     mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
100
101 export const countNodes = <T>(tree: Tree<T>) =>
102     getNodeDescendantsIds('')(tree).length;
103
104 export const countChildren = (id: string) => <T>(tree: Tree<T>) =>
105     getNodeChildren('')(tree).length;
106
107 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
108     const node = getNode(id)(tree);
109     const children = node ? node.children :
110         id === TREE_ROOT_ID
111             ? getRootNodeChildrenIds(tree)
112             : [];
113
114     return children
115         .concat(limit < 1
116             ? []
117             : children
118                 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
119                 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
120 };
121
122 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
123     mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
124
125 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
126     getNodeDescendantsIds(id, 0)(tree);
127
128 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
129     ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
130
131 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
132     mapTree((node: TreeNode<T>) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
133
134 export const deactivateNode = <T>(tree: Tree<T>) =>
135     mapTree((node: TreeNode<T>) => node.active ? { ...node, active: false } : node)(tree);
136
137 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
138     mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
139
140 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
141     mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
142
143 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
144     mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
145
146 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
147     const node = getNode(id)(tree);
148     return node
149         ? setNode({ ...node, status })(tree)
150         : tree;
151 };
152
153 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
154     const node = getNode(id)(tree);
155     return node
156         ? pipe(
157             setNode({ ...node, selected: !node.selected }),
158             toggleAncestorsSelection(id),
159             toggleDescendantsSelection(id))(tree)
160         : tree;
161 };
162
163 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
164     const node = getNode(id)(tree);
165     return node && node.selected
166         ? tree
167         : toggleNodeSelection(id)(tree);
168 };
169
170 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
171     const ids = typeof id === 'string' ? [id] : id;
172     return ids.reduce((tree, id) => selectNode(id)(tree), tree);
173 };
174 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
175     const node = getNode(id)(tree);
176     return node && node.selected
177         ? toggleNodeSelection(id)(tree)
178         : tree;
179 };
180
181 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
182     const ids = typeof id === 'string' ? [id] : id;
183     return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
184 };
185
186 export const getSelectedNodes = <T>(tree: Tree<T>) =>
187     getNodeDescendants('')(tree)
188         .filter(node => node.selected);
189
190 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
191     children: [],
192     active: false,
193     selected: false,
194     expanded: false,
195     status: TreeNodeStatus.INITIAL,
196     parent: '',
197     ...data,
198 });
199
200 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
201     const node = getNode(id)(tree);
202     if (node) {
203         return getNodeDescendants(id)(tree)
204             .reduce((newTree, subNode) =>
205                 setNode({ ...subNode, selected: node.selected })(newTree),
206                 tree);
207     }
208     return tree;
209 };
210
211 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
212     const ancestors = getNodeAncestorsIds(id)(tree).reverse();
213     return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
214 };
215
216 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
217     const node = getNode(id)(tree);
218     if (node) {
219         const parentNode = getNode(node.id)(tree);
220         if (parentNode) {
221             const selected = parentNode.children
222                 .map(id => getNode(id)(tree))
223                 .every(node => node !== undefined && node.selected);
224             return setNode({ ...parentNode, selected })(tree);
225         }
226         return setNode(node)(tree);
227     }
228     return tree;
229 };
230
231
232 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
233     ({ ...node, value: mapFn(node.value) });
234
235 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
236     Object
237         .keys(tree)
238         .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);