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