1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { List, ListItem, ListItemIcon, Checkbox, Radio, Collapse } from "@material-ui/core";
7 import { StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core/styles';
8 import { CollectionIcon, DefaultIcon, DirectoryIcon, FileIcon, ProjectIcon } from '~/components/icon/icon';
9 import { ReactElement } from "react";
10 import CircularProgress from '@material-ui/core/CircularProgress';
11 import classnames from "classnames";
13 import { ArvadosTheme } from '~/common/custom-theme';
14 import { SidePanelRightArrowIcon } from '../icon/icon';
15 import { ResourceKind } from '~/models/resource';
17 type CssRules = 'list'
21 | 'toggableIconContainer'
30 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
39 transform: 'translate(0px)',
42 toggableIconContainer: {
43 color: theme.palette.grey["700"],
54 transition: 'all 0.1s ease',
57 transition: 'all 0.1s ease',
58 transform: 'rotate(90deg)',
61 width: theme.spacing.unit * 3,
62 height: theme.spacing.unit * 3,
63 margin: `0 ${theme.spacing.unit}px`,
65 color: theme.palette.grey["500"],
74 backgroundColor: 'rgba(0, 0, 0, 0.08)',
80 color: 'rgba(0, 0, 0, 0.54)',
83 color: theme.palette.primary.main,
87 export enum TreeItemStatus {
93 export interface TreeItem<T> {
100 status: TreeItemStatus;
101 items?: Array<TreeItem<T>>;
104 export interface TreeProps<T> {
105 disableRipple?: boolean;
106 currentItemUuid?: string;
107 items?: Array<TreeItem<T>>;
109 itemsMap?: Map<string, TreeItem<T>>;
110 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
111 render: (item: TreeItem<T>, level?: number) => ReactElement<{}>;
112 showSelection?: boolean | ((item: TreeItem<T>) => boolean);
113 levelIndentation?: number;
114 itemRightPadding?: number;
115 toggleItemActive: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
116 toggleItemOpen: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
117 toggleItemSelection?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
120 * When set to true use radio buttons instead of checkboxes for item selection.
121 * This does not guarantee radio group behavior (i.e item mutual exclusivity).
122 * Any item selection logic must be done in the toggleItemActive callback prop.
124 useRadioButtons?: boolean;
127 const getActionAndId = (event: any, initAction: string | undefined = undefined) => {
128 const { nativeEvent: { target } } = event;
129 let currentTarget: HTMLElement = target as HTMLElement;
130 let action: string | undefined = initAction || currentTarget.dataset.action;
131 let id: string | undefined = currentTarget.dataset.id;
133 while (action === undefined || id === undefined) {
134 currentTarget = currentTarget.parentElement as HTMLElement;
136 if (!currentTarget) {
140 action = action || currentTarget.dataset.action;
141 id = id || currentTarget.dataset.id;
147 interface FlatTreeProps {
149 levelIndentation: number;
150 onContextMenu: Function;
151 handleToggleItemOpen: Function;
152 toggleItemActive: Function;
153 getToggableIconClassNames: Function;
154 getProperArrowAnimation: Function;
155 itemsMap?: Map<string, TreeItem<any>>;
159 const FLAT_TREE_ACTIONS = {
160 toggleOpen: 'TOGGLE_OPEN',
161 contextMenu: 'CONTEXT_MENU',
162 toggleActive: 'TOGGLE_ACTIVE',
165 const ItemIcon = React.memo(({type, kind, active, classes}: any) => {
166 let Icon = ProjectIcon;
171 Icon = DirectoryIcon;
183 case ResourceKind.COLLECTION:
184 Icon = CollectionIcon;
191 return <Icon className={classnames({ [classes.active]: active }, classes.childItemIcon)} />;
194 const FlatTree = (props: FlatTreeProps) =>
196 onContextMenu={(event) => {
197 const [action, id] = getActionAndId(event, FLAT_TREE_ACTIONS.contextMenu);
198 props.onContextMenu(event, { id } as any);
200 onClick={(event) => {
201 const [action, id] = getActionAndId(event);
204 const item = props.itemsMap ? props.itemsMap[id] : { id };
207 case FLAT_TREE_ACTIONS.toggleOpen:
208 props.handleToggleItemOpen(item as any, event);
210 case FLAT_TREE_ACTIONS.toggleActive:
211 props.toggleItemActive(event, item as any);
220 (props.it.items || [])
221 .map((item: any) => <div key={item.id} data-id={item.id}
222 className={classnames(props.classes.childItem, { [props.classes.active]: item.active })}
223 style={{ paddingLeft: `${item.depth * props.levelIndentation}px` }}>
224 <i data-action={FLAT_TREE_ACTIONS.toggleOpen} className={props.classes.toggableIconContainer}>
225 <ListItemIcon className={props.getToggableIconClassNames(item.open, item.active)}>
226 {props.getProperArrowAnimation(item.status, item.items!)}
229 <div data-action={FLAT_TREE_ACTIONS.toggleActive} className={props.classes.renderContainer}>
230 <span style={{ display: 'flex', alignItems: 'center' }}>
231 <ItemIcon type={item.data.type} active={item.active} kind={item.data.kind} classes={props.classes} />
232 <span style={{ fontSize: '0.875rem' }}>
241 export const Tree = withStyles(styles)(
242 class Component<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
243 render(): ReactElement<any> {
244 const level = this.props.level ? this.props.level : 0;
245 const { classes, render, items, toggleItemActive, toggleItemOpen, disableRipple, currentItemUuid, useRadioButtons, itemsMap } = this.props;
246 const { list, listItem, loader, toggableIconContainer, renderContainer } = classes;
247 const showSelection = typeof this.props.showSelection === 'function'
248 ? this.props.showSelection
249 : () => this.props.showSelection ? true : false;
251 const { levelIndentation = 20, itemRightPadding = 20 } = this.props;
253 return <List className={list}>
254 {items && items.map((it: TreeItem<T>, idx: number) =>
255 <div key={`item/${level}/${it.id}`}>
256 <ListItem button className={listItem}
258 paddingLeft: (level + 1) * levelIndentation,
259 paddingRight: itemRightPadding,
261 disableRipple={disableRipple}
262 onClick={event => toggleItemActive(event, it)}
263 selected={showSelection(it) && it.id === currentItemUuid}
264 onContextMenu={(event) => this.props.onContextMenu(event, it)}>
265 {it.status === TreeItemStatus.PENDING ?
266 <CircularProgress size={10} className={loader} /> : null}
267 <i onClick={(e) => this.handleToggleItemOpen(it, e)}
268 className={toggableIconContainer}>
269 <ListItemIcon className={this.getToggableIconClassNames(it.open, it.active)}>
270 {this.getProperArrowAnimation(it.status, it.items!)}
273 {showSelection(it) && !useRadioButtons &&
275 checked={it.selected}
276 className={classes.checkbox}
278 onClick={this.handleCheckboxChange(it)} />}
279 {showSelection(it) && useRadioButtons &&
281 checked={it.selected}
282 className={classes.checkbox}
284 <div className={renderContainer}>
289 it.open && it.items && it.items.length > 0 &&
294 classes={this.props.classes}
295 levelIndentation={levelIndentation}
296 onContextMenu={this.props.onContextMenu}
297 handleToggleItemOpen={this.handleToggleItemOpen}
298 toggleItemActive={this.props.toggleItemActive}
299 getToggableIconClassNames={this.getToggableIconClassNames}
300 getProperArrowAnimation={this.getProperArrowAnimation}
302 <Collapse in={it.open} timeout="auto" unmountOnExit>
304 showSelection={this.props.showSelection}
307 disableRipple={disableRipple}
308 toggleItemOpen={toggleItemOpen}
309 toggleItemActive={toggleItemActive}
311 onContextMenu={this.props.onContextMenu}
312 toggleItemSelection={this.props.toggleItemSelection} />
319 getProperArrowAnimation = (status: string, items: Array<TreeItem<T>>) => {
320 return this.isSidePanelIconNotNeeded(status, items) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
323 isSidePanelIconNotNeeded = (status: string, items: Array<TreeItem<T>>) => {
324 return status === TreeItemStatus.PENDING ||
325 (status === TreeItemStatus.LOADED && !items) ||
326 (status === TreeItemStatus.LOADED && items && items.length === 0);
329 getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
330 const { iconOpen, iconClose, active, toggableIcon } = this.props.classes;
331 return classnames(toggableIcon, {
333 [iconClose]: !isOpen,
338 handleCheckboxChange = (item: TreeItem<T>) => {
339 const { toggleItemSelection } = this.props;
340 return toggleItemSelection
341 ? (event: React.MouseEvent<HTMLElement>) => {
342 event.stopPropagation();
343 toggleItemSelection(event, item);
348 handleToggleItemOpen = (item: TreeItem<T>, event: React.MouseEvent<HTMLElement>) => {
349 event.stopPropagation();
350 this.props.toggleItemOpen(event, item);