X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/33c03ae3c79936cc1a69129f07fba33fe2d28fd8..70cc7de1fd7fb72c757a8466dee7ca6bf8e55c84:/src/models/tree.ts diff --git a/src/models/tree.ts b/src/models/tree.ts index e9291388..aeb41541 100644 --- a/src/models/tree.ts +++ b/src/models/tree.ts @@ -14,6 +14,7 @@ export interface TreeNode { parent: string; active: boolean; selected: boolean; + initialState?: boolean; expanded: boolean; status: TreeNodeStatus; } @@ -137,6 +138,11 @@ export const deactivateNode = (tree: Tree) => export const expandNode = (...ids: string[]) => (tree: Tree) => mapTree((node: TreeNode) => ids.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree); +export const expandNodeAncestors = (...ids: string[]) => (tree: Tree) => { + const ancestors = ids.reduce((acc, id): string[] => ([...acc, ...getNodeAncestorsIds(id)(tree)]), [] as string[]); + return mapTree((node: TreeNode) => ancestors.some(id => id === node.id) ? { ...node, expanded: true } : node)(tree); +} + export const collapseNode = (...ids: string[]) => (tree: Tree) => mapTree((node: TreeNode) => ids.some(id => id === node.id) ? { ...node, expanded: false } : node)(tree); @@ -150,37 +156,40 @@ export const setNodeStatus = (id: string) => (status: TreeNodeStatus) => (tre : tree; }; -export const toggleNodeSelection = (id: string) => (tree: Tree) => { +export const toggleNodeSelection = (id: string, cascade: boolean) => (tree: Tree) => { const node = getNode(id)(tree); + return node - ? pipe( - setNode({ ...node, selected: !node.selected }), - toggleAncestorsSelection(id), - toggleDescendantsSelection(id))(tree) + ? cascade + ? pipe( + setNode({ ...node, selected: !node.selected }), + toggleAncestorsSelection(id), + toggleDescendantsSelection(id))(tree) + : setNode({ ...node, selected: !node.selected })(tree) : tree; }; -export const selectNode = (id: string) => (tree: Tree) => { +export const selectNode = (id: string, cascade: boolean) => (tree: Tree) => { const node = getNode(id)(tree); return node && node.selected ? tree - : toggleNodeSelection(id)(tree); + : toggleNodeSelection(id, cascade)(tree); }; -export const selectNodes = (id: string | string[]) => (tree: Tree) => { +export const selectNodes = (id: string | string[], cascade: boolean) => (tree: Tree) => { const ids = typeof id === 'string' ? [id] : id; - return ids.reduce((tree, id) => selectNode(id)(tree), tree); + return ids.reduce((tree, id) => selectNode(id, cascade)(tree), tree); }; -export const deselectNode = (id: string) => (tree: Tree) => { +export const deselectNode = (id: string, cascade: boolean) => (tree: Tree) => { const node = getNode(id)(tree); return node && node.selected - ? toggleNodeSelection(id)(tree) + ? toggleNodeSelection(id, cascade)(tree) : tree; }; -export const deselectNodes = (id: string | string[]) => (tree: Tree) => { +export const deselectNodes = (id: string | string[], cascade: boolean) => (tree: Tree) => { const ids = typeof id === 'string' ? [id] : id; - return ids.reduce((tree, id) => deselectNode(id)(tree), tree); + return ids.reduce((tree, id) => deselectNode(id, cascade)(tree), tree); }; export const getSelectedNodes = (tree: Tree) => @@ -197,6 +206,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) { @@ -228,7 +250,6 @@ const toggleParentNodeSelection = (id: string) => (tree: Tree) => { return tree; }; - const mapNodeValue = (mapFn: (value: T) => R) => (node: TreeNode): TreeNode => ({ ...node, value: mapFn(node.value) });