X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/2cfb56ef5060cbad461062690fad650f15c49731..417866564ecc0b1fee98246fe1e70ae6a3e3856a:/src/models/tree.ts diff --git a/src/models/tree.ts b/src/models/tree.ts index f0b53b46f4..996f98a465 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 = ''; @@ -14,6 +14,7 @@ export interface TreeNode { parent: string; active: boolean; selected: boolean; + initialState?: boolean; expanded: boolean; status: TreeNodeStatus; } @@ -24,17 +25,32 @@ export enum TreeNodeStatus { LOADED = 'LOADED', } +export enum TreePickerId { + PROJECTS = 'Projects', + SHARED_WITH_ME = 'Shared with me', + FAVORITES = 'Favorites', + PUBLIC_FAVORITES = 'Public 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 - ? tree - : { ...tree, [node.id]: node }, - addChild(node.parent, node.id) - )(tree); + if (tree[node.id] && tree[node.id] === node) { return tree; } + + tree[node.id] = node; + if (tree[node.parent]) { + tree[node.parent].children = Array.from(new Set([...tree[node.parent].children, node.id])); + } + return tree; }; export const getNodeValue = (id: string) => (tree: Tree) => { @@ -59,6 +75,7 @@ export const setNodeValueWith = (mapFn: (value: T) => T) => (id: string) => ( export const mapTreeValues = (mapFn: (value: T) => R) => (tree: Tree): Tree => getNodeDescendantsIds('')(tree) .map(id => getNode(id)(tree)) + .filter(node => !!node) .map(mapNodeValue(mapFn)) .reduce((newTree, node) => setNode(node)(newTree), createTree()); @@ -82,11 +99,17 @@ export const getNodeAncestorsIds = (id: string) => (tree: Tree): string[] export const getNodeDescendants = (id: string, limit = Infinity) => (tree: Tree) => mapIdsToNodes(getNodeDescendantsIds(id, limit)(tree))(tree); +export const countNodes = (tree: Tree) => + getNodeDescendantsIds('')(tree).length; + +export const countChildren = (id: string) => (tree: Tree) => + getNodeChildren('')(tree).length; + export const getNodeDescendantsIds = (id: string, limit = Infinity) => (tree: Tree): string[] => { const node = getNode(id)(tree); const children = node ? node.children : id === TREE_ROOT_ID - ? getRootNodeChildren(tree) + ? getRootNodeChildrenIds(tree) : []; return children @@ -107,19 +130,19 @@ export const mapIdsToNodes = (ids: string[]) => (tree: Tree) => ids.map(id => getNode(id)(tree)).filter((node): node is TreeNode => node !== undefined); export const activateNode = (id: string) => (tree: Tree) => - mapTree(node => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree); + mapTree((node: TreeNode) => node.id === id ? { ...node, active: true } : { ...node, active: false })(tree); export const deactivateNode = (tree: Tree) => - mapTree(node => node.active ? { ...node, active: false } : node)(tree); + mapTree((node: TreeNode) => node.active ? { ...node, active: false } : node)(tree); export const expandNode = (...ids: string[]) => (tree: Tree) => - mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree); + mapTree((node: TreeNode) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree); export const collapseNode = (...ids: string[]) => (tree: Tree) => - mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree); + mapTree((node: TreeNode) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree); export const toggleNodeCollapse = (...ids: string[]) => (tree: Tree) => - mapTree(node => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree); + mapTree((node: TreeNode) => ids.some(id => id === node.id) ? { ...node, expanded: !node.expanded } : node)(tree); export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => (tree: Tree) => { const node = getNode(id)(tree); @@ -136,7 +159,6 @@ export const toggleNodeSelection = (id: string) => (tree: Tree) => { toggleAncestorsSelection(id), toggleDescendantsSelection(id))(tree) : tree; - }; export const selectNode = (id: string) => (tree: Tree) => { @@ -162,6 +184,10 @@ export const deselectNodes = (id: string | string[]) => (tree: Tree) => { return ids.reduce((tree, id) => deselectNode(id)(tree), tree); }; +export const getSelectedNodes = (tree: Tree) => + getNodeDescendants('')(tree) + .filter(node => node.selected); + export const initTreeNode = (data: Pick, 'id' | 'value'> & { parent?: string }): TreeNode => ({ children: [], active: false, @@ -172,6 +198,19 @@ export const initTreeNode = (data: Pick, 'id' | 'value'> & { pare ...data, }); +export const getTreeDirty = (id: string) => (tree: Tree): boolean => { + const node = getNode(id)(tree); + const children = getNodeDescendants(id)(tree); + return (node + && node.initialState !== undefined + && node.selected !== node.initialState + ) + || children.some(child => + child.initialState !== undefined + && child.selected !== child.initialState + ); +} + const toggleDescendantsSelection = (id: string) => (tree: Tree) => { const node = getNode(id)(tree); if (node) { @@ -203,27 +242,10 @@ const toggleParentNodeSelection = (id: string) => (tree: Tree) => { return 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) { - const children = node.children.some(id => id === childId) - ? node.children - : [...node.children, childId]; - - const newNode = children === node.children - ? node - : { ...node, children }; - - return setNode(newNode)(tree); - } - return tree; -};