X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/8fd2c927929859ec4905d7c50c83d9dd170cc6d9..c69c6d43021b29186717ad30bea95cf50ee8c1e6:/src/components/tree/tree.tsx diff --git a/src/components/tree/tree.tsx b/src/components/tree/tree.tsx index 25398288..fc9dbc74 100644 --- a/src/components/tree/tree.tsx +++ b/src/components/tree/tree.tsx @@ -2,16 +2,18 @@ // // SPDX-License-Identifier: AGPL-3.0 -import * as React from 'react'; -import { List, ListItem, ListItemIcon, Checkbox, Radio } from "@material-ui/core"; +import React from 'react'; +import { List, ListItem, ListItemIcon, Checkbox, Radio, Collapse } from "@material-ui/core"; import { StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core/styles'; -import { ProjectIcon } from '~/components/icon/icon'; +import { CollectionIcon, DefaultIcon, DirectoryIcon, FileIcon, ProjectIcon, FilterGroupIcon } from 'components/icon/icon'; import { ReactElement } from "react"; import CircularProgress from '@material-ui/core/CircularProgress'; import classnames from "classnames"; -import { ArvadosTheme } from '~/common/custom-theme'; +import { ArvadosTheme } from 'common/custom-theme'; import { SidePanelRightArrowIcon } from '../icon/icon'; +import { ResourceKind } from 'models/resource'; +import { GroupClass } from 'models/group'; type CssRules = 'list' | 'listItem' @@ -95,6 +97,9 @@ export interface TreeItem { open: boolean; active: boolean; selected?: boolean; + initialState?: boolean; + indeterminate?: boolean; + flatTree?: boolean; status: TreeItemStatus; items?: Array>; } @@ -104,6 +109,7 @@ export interface TreeProps { currentItemUuid?: string; items?: Array>; level?: number; + itemsMap?: Map>; onContextMenu: (event: React.MouseEvent, item: TreeItem) => void; render: (item: TreeItem, level?: number) => ReactElement<{}>; showSelection?: boolean | ((item: TreeItem) => boolean); @@ -141,12 +147,124 @@ const getActionAndId = (event: any, initAction: string | undefined = undefined) return [action, id]; }; +interface FlatTreeProps { + it: TreeItem; + levelIndentation: number; + onContextMenu: Function; + handleToggleItemOpen: Function; + toggleItemActive: Function; + getToggableIconClassNames: Function; + getProperArrowAnimation: Function; + itemsMap?: Map>; + classes: any; + showSelection: any; + useRadioButtons?: boolean; + handleCheckboxChange: Function; +} + +const FLAT_TREE_ACTIONS = { + toggleOpen: 'TOGGLE_OPEN', + contextMenu: 'CONTEXT_MENU', + toggleActive: 'TOGGLE_ACTIVE', +}; + +const ItemIcon = React.memo(({ type, kind, active, groupClass, classes }: any) => { + let Icon = ProjectIcon; + + if (groupClass === GroupClass.FILTER) { + Icon = FilterGroupIcon; + } + + if (type) { + switch (type) { + case 'directory': + Icon = DirectoryIcon; + break; + case 'file': + Icon = FileIcon; + break; + default: + Icon = DefaultIcon; + } + } + + if (kind) { + switch (kind) { + case ResourceKind.COLLECTION: + Icon = CollectionIcon; + break; + default: + break; + } + } + + return ; +}); + +const FlatTree = (props: FlatTreeProps) => +
{ + const id = getActionAndId(event, FLAT_TREE_ACTIONS.contextMenu)[1]; + props.onContextMenu(event, { id } as any); + }} + onClick={(event) => { + const [action, id] = getActionAndId(event); + + if (action && id) { + const item = props.itemsMap ? props.itemsMap[id] : { id }; + + switch (action) { + case FLAT_TREE_ACTIONS.toggleOpen: + props.handleToggleItemOpen(item as any, event); + break; + case FLAT_TREE_ACTIONS.toggleActive: + props.toggleItemActive(event, item as any); + break; + default: + break; + } + } + }} + > + { + (props.it.items || []) + .map((item: any) =>
+ + + {props.getProperArrowAnimation(item.status, item.items!)} + + + {props.showSelection(item) && !props.useRadioButtons && + } + {props.showSelection(item) && props.useRadioButtons && + } +
+ + + + {item.data.name} + + +
+
) + } +
; + export const Tree = withStyles(styles)( class Component extends React.Component & WithStyles, {}> { render(): ReactElement { const level = this.props.level ? this.props.level : 0; - const { classes, render, items, toggleItemActive, disableRipple, currentItemUuid, useRadioButtons } = this.props; - const { list, listItem, loader, toggableIconContainer, renderContainer, childItem, active, childItemIcon } = classes; + const { classes, render, items, toggleItemActive, toggleItemOpen, disableRipple, currentItemUuid, useRadioButtons, itemsMap } = this.props; + const { list, listItem, loader, toggableIconContainer, renderContainer } = classes; const showSelection = typeof this.props.showSelection === 'function' ? this.props.showSelection : () => this.props.showSelection ? true : false; @@ -176,6 +294,7 @@ export const Tree = withStyles(styles)( {showSelection(it) && !useRadioButtons && } @@ -188,51 +307,36 @@ export const Tree = withStyles(styles)( {render(it, level)} - {it.open && it.items && it.items.length > 0 && -
{ - const [action, id] = getActionAndId(event, 'CONTEXT_MENU'); - this.props.onContextMenu(event, { id } as any); - }} - onClick={(event) => { - const [action, id] = getActionAndId(event); - - if (action && id) { - switch(action) { - case 'TOGGLE_OPEN': - this.handleToggleItemOpen({ id } as any, event); - break; - case 'TOGGLE_ACTIVE': - toggleItemActive(event, { id } as any); - break; - default: - break; - } - } - }} - > - { - it.items - .slice(0, 30) - .map((item: any) =>
- - - {this.getProperArrowAnimation(item.status, item.items!)} - - -
- - - - {item.data.name} - - -
-
) - } -
} + { + it.open && it.items && it.items.length > 0 && + it.flatTree ? + : + + + + } )} ; }