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