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