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