8b71692b1c8bf8211c606573f99a8876a6392aab
[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 countNodes = <T>(tree: Tree<T>) =>
99     getNodeDescendantsIds('')(tree).length;
100
101 export const getNodeDescendantsIds = (id: string, limit = Infinity) => <T>(tree: Tree<T>): string[] => {
102     const node = getNode(id)(tree);
103     const children = node ? node.children :
104         id === TREE_ROOT_ID
105             ? getRootNodeChildrenIds(tree)
106             : [];
107
108     return children
109         .concat(limit < 1
110             ? []
111             : children
112                 .map(id => getNodeDescendantsIds(id, limit - 1)(tree))
113                 .reduce((nodes, nodeChildren) => [...nodes, ...nodeChildren], []));
114 };
115
116 export const getNodeChildren = (id: string) => <T>(tree: Tree<T>) =>
117     mapIdsToNodes(getNodeChildrenIds(id)(tree))(tree);
118
119 export const getNodeChildrenIds = (id: string) => <T>(tree: Tree<T>): string[] =>
120     getNodeDescendantsIds(id, 0)(tree);
121
122 export const mapIdsToNodes = (ids: string[]) => <T>(tree: Tree<T>) =>
123     ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode<T> => node !== undefined);
124
125 export const activateNode = (id: string) => <T>(tree: Tree<T>) =>
126     mapTree((node: TreeNode<T>) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree);
127
128 export const deactivateNode = <T>(tree: Tree<T>) =>
129     mapTree((node: TreeNode<T>) => node.active ? { ...node, active: false } : node)(tree);
130
131 export const expandNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
132     mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree);
133
134 export const collapseNode = (...ids: string[]) => <T>(tree: Tree<T>) =>
135     mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree);
136
137 export const toggleNodeCollapse = (...ids: string[]) => <T>(tree: Tree<T>) =>
138     mapTree((node: TreeNode<T>) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree);
139
140 export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => <T>(tree: Tree<T>) => {
141     const node = getNode(id)(tree);
142     return node
143         ? setNode({ ...node, status })(tree)
144         : tree;
145 };
146
147 export const toggleNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
148     const node = getNode(id)(tree);
149     return node
150         ? pipe(
151             setNode({ ...node, selected: !node.selected }),
152             toggleAncestorsSelection(id),
153             toggleDescendantsSelection(id))(tree)
154         : tree;
155
156 };
157
158 export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
159     const node = getNode(id)(tree);
160     return node && node.selected
161         ? tree
162         : toggleNodeSelection(id)(tree);
163 };
164
165 export const selectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
166     const ids = typeof id === 'string' ? [id] : id;
167     return ids.reduce((tree, id) => selectNode(id)(tree), tree);
168 };
169 export const deselectNode = (id: string) => <T>(tree: Tree<T>) => {
170     const node = getNode(id)(tree);
171     return node && node.selected
172         ? toggleNodeSelection(id)(tree)
173         : tree;
174 };
175
176 export const deselectNodes = (id: string | string[]) => <T>(tree: Tree<T>) => {
177     const ids = typeof id === 'string' ? [id] : id;
178     return ids.reduce((tree, id) => deselectNode(id)(tree), tree);
179 };
180
181 export const getSelectedNodes = <T>(tree: Tree<T>) =>
182     getNodeDescendants('')(tree)
183         .filter(node => node.selected);
184
185 export const initTreeNode = <T>(data: Pick<TreeNode<T>, 'id' | 'value'> & { parent?: string }): TreeNode<T> => ({
186     children: [],
187     active: false,
188     selected: false,
189     expanded: false,
190     status: TreeNodeStatus.INITIAL,
191     parent: '',
192     ...data,
193 });
194
195 const toggleDescendantsSelection = (id: string) => <T>(tree: Tree<T>) => {
196     const node = getNode(id)(tree);
197     if (node) {
198         return getNodeDescendants(id)(tree)
199             .reduce((newTree, subNode) =>
200                 setNode({ ...subNode, selected: node.selected })(newTree),
201                 tree);
202     }
203     return tree;
204 };
205
206 const toggleAncestorsSelection = (id: string) => <T>(tree: Tree<T>) => {
207     const ancestors = getNodeAncestorsIds(id)(tree).reverse();
208     return ancestors.reduce((newTree, parent) => parent ? toggleParentNodeSelection(parent)(newTree) : newTree, tree);
209 };
210
211 const toggleParentNodeSelection = (id: string) => <T>(tree: Tree<T>) => {
212     const node = getNode(id)(tree);
213     if (node) {
214         const parentNode = getNode(node.id)(tree);
215         if (parentNode) {
216             const selected = parentNode.children
217                 .map(id => getNode(id)(tree))
218                 .every(node => node !== undefined && node.selected);
219             return setNode({ ...parentNode, selected })(tree);
220         }
221         return setNode(node)(tree);
222     }
223     return tree;
224 };
225
226
227 const mapNodeValue = <T, R>(mapFn: (value: T) => R) => (node: TreeNode<T>): TreeNode<R> =>
228     ({ ...node, value: mapFn(node.value) });
229
230 const getRootNodeChildrenIds = <T>(tree: Tree<T>) =>
231     Object
232         .keys(tree)
233         .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
234
235
236 const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
237     const node = getNode(parentId)(tree);
238     if (node) {
239         const children = node.children.some(id => id === childId)
240             ? node.children
241             : [...node.children, childId];
242
243         const newNode = children === node.children
244             ? node
245             : { ...node, children };
246
247         return setNode(newNode)(tree);
248     }
249     return tree;
250 };