X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/f2f7301dab111f2561332c8cc9e7c06df958ea66..refs/heads/14433_properties_inside_projects:/src/models/tree.ts diff --git a/src/models/tree.ts b/src/models/tree.ts index f0b53b46..fe52a97b 100644 --- a/src/models/tree.ts +++ b/src/models/tree.ts @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { pipe } from 'lodash/fp'; +import { pipe, map, reduce } from 'lodash/fp'; export type Tree = Record>; export const TREE_ROOT_ID = ''; @@ -24,10 +24,23 @@ export enum TreeNodeStatus { LOADED = 'LOADED', } +export enum TreePickerId { + PROJECTS = 'Projects', + SHARED_WITH_ME = 'Shared with me', + FAVORITES = 'Favorites' +} + export const createTree = (): Tree => ({}); export const getNode = (id: string) => (tree: Tree): TreeNode | undefined => tree[id]; +export const appendSubtree = (id: string, subtree: Tree) => (tree: Tree) => + pipe( + getNodeDescendants(''), + map(node => node.parent === '' ? { ...node, parent: id } : node), + reduce((newTree, node) => setNode(node)(newTree), tree) + )(subtree) as Tree; + export const setNode = (node: TreeNode) => (tree: Tree): Tree => { return pipe( (tree: Tree) => getNode(node.id)(tree) === node @@ -86,7 +99,7 @@ export const getNodeDescendantsIds = (id: string, limit = Infinity) => (tree: const node = getNode(id)(tree); const children = node ? node.children : id === TREE_ROOT_ID - ? getRootNodeChildren(tree) + ? getRootNodeChildrenIds(tree) : []; return children @@ -207,11 +220,12 @@ const toggleParentNodeSelection = (id: string) => (tree: Tree) => { const mapNodeValue = (mapFn: (value: T) => R) => (node: TreeNode): TreeNode => ({ ...node, value: mapFn(node.value) }); -const getRootNodeChildren = (tree: Tree) => +const getRootNodeChildrenIds = (tree: Tree) => Object .keys(tree) .filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID); + const addChild = (parentId: string, childId: string) => (tree: Tree): Tree => { const node = getNode(parentId)(tree); if (node) {