Create my account view
[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 }
32
33 export const createTree = <T>(): Tree<T> => ({});
34
35 export const getNode = (id: string) => <T>(tree: Tree<T>): TreeNode<T> | undefined => tree[id];
36
37 export const appendSubtree = <T>(id: string, subtree: Tree<T>) => (tree: Tree<T>) =>
38     pipe(
39         getNodeDescendants(''),
40         map(node => node.parent === '' ? { ...node, parent: id } : node),
41         reduce((newTree, node) => setNode(node)(newTree), tree)
42     )(subtree) as Tree<T>;
43
44 export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
45     return pipe(
46         (tree: Tree<T>) => getNode(node.id)(tree) === node
47             ? tree
48             : { ...tree, [node.id]: node },
49         addChild(node.parent, node.id)
50     )(tree);
51 };
52
53 export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
54     const node = getNode(id)(tree);
55     return node ? node.value : undefined;
56 };
57
58 export const setNodeValue = (id: string) => <T>(value: T) => (tree: Tree<T>) => {
59     const node = getNode(id)(tree);
60     return node
61         ? setNode(mapNodeValue(() => value)(node))(tree)
62         : tree;
63 };
64
65 export const setNodeValueWith = <T>(mapFn: (value: T) => T) => (id: string) => (tree: Tree<T>) => {
66     const node = getNode(id)(tree);
67     return node
68         ? setNode(mapNodeValue(mapFn)(node))(tree)
69         : tree;
70 };
71
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>());
77
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))
81         .map(mapFn)
82         .reduce((newTree, node) => setNode(node)(newTree), createTree<R>());
83
84 export const getNodeAncestors = (id: string) => <T>(tree: Tree<T>) =>
85     mapIdsToNodes(getNodeAncestorsIds(id)(tree))(tree);
86
87
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]
92         : [];
93 };
94
95 export const getNodeDescendants = (id: string, limit = Infinity) => <T>(tree: Tree<T>) =>
96     mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree);
97
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 :
101         id === TREE_ROOT_ID
102             ? getRootNodeChildrenIds(tree)
103             : [];
104
105     return children
106         .concat(limit < 1
107             ? []
108             : children
109                 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
110                 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
111 };
112
113 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
114     mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
115
116 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
117     getNodeDescendantsIds(id, 0)(tree);
118
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);
121
122 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
123     mapTree(node => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
124
125 export const deactivateNode = <T>(tree: Tree<T>) =>
126     mapTree(node => node.active ? { ...node, active: false } : node)(tree);
127
128 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
129     mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
130
131 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
132     mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
133
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);
136
137 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
138     const node = getNode(id)(tree);
139     return node
140         ? setNode({ ...node, status })(tree)
141         : tree;
142 };
143
144 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
145     const node = getNode(id)(tree);
146     return node
147         ? pipe(
148             setNode({ ...node, selected: !node.selected }),
149             toggleAncestorsSelection(id),
150             toggleDescendantsSelection(id))(tree)
151         : tree;
152
153 };
154
155 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
156     const node = getNode(id)(tree);
157     return node && node.selected
158         ? tree
159         : toggleNodeSelection(id)(tree);
160 };
161
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);
165 };
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)
170         : tree;
171 };
172
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);
176 };
177
178 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
179     children: [],
180     active: false,
181     selected: false,
182     expanded: false,
183     status: TreeNodeStatus.INITIAL,
184     parent: '',
185     ...data,
186 });
187
188 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
189     const node = getNode(id)(tree);
190     if (node) {
191         return getNodeDescendants(id)(tree)
192             .reduce((newTree, subNode) =>
193                 setNode({ ...subNode, selected: node.selected })(newTree),
194                 tree);
195     }
196     return tree;
197 };
198
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);
202 };
203
204 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
205     const node = getNode(id)(tree);
206     if (node) {
207         const parentNode = getNode(node.id)(tree);
208         if (parentNode) {
209             const selected = parentNode.children
210                 .map(id => getNode(id)(tree))
211                 .every(node => node !== undefined && node.selected);
212             return setNode({ ...parentNode, selected })(tree);
213         }
214         return setNode(node)(tree);
215     }
216     return tree;
217 };
218
219
220 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
221     ({ ...node, value: mapFn(node.value) });
222
223 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
224     Object
225         .keys(tree)
226         .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
227
228
229 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
230     const node = getNode(parentId)(tree);
231     if (node) {
232         const children = node.children.some(id => id === childId)
233             ? node.children
234             : [...node.children, childId];
235
236         const newNode = children === node.children
237             ? node
238             : { ...node, children };
239
240         return setNode(newNode)(tree);
241     }
242     return tree;
243 };