1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import 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, FilterGroupIcon } 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';
16 import { GroupClass } from 'models/group';
18 type CssRules = 'list'
22 | 'toggableIconContainer'
31 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
40 transform: 'translate(0px)',
43 toggableIconContainer: {
44 color: theme.palette.grey["700"],
55 transition: 'all 0.1s ease',
58 transition: 'all 0.1s ease',
59 transform: 'rotate(90deg)',
62 width: theme.spacing.unit * 3,
63 height: theme.spacing.unit * 3,
64 margin: `0 ${theme.spacing.unit}px`,
66 color: theme.palette.grey["500"],
75 backgroundColor: 'rgba(0, 0, 0, 0.08)',
81 color: 'rgba(0, 0, 0, 0.54)',
84 color: theme.palette.primary.main,
88 export enum TreeItemStatus {
94 export interface TreeItem<T> {
100 initialState?: boolean;
101 indeterminate?: boolean;
103 status: TreeItemStatus;
104 items?: Array<TreeItem<T>>;
107 export interface TreeProps<T> {
108 disableRipple?: boolean;
109 currentItemUuid?: string;
110 items?: Array<TreeItem<T>>;
112 itemsMap?: Map<string, TreeItem<T>>;
113 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
114 render: (item: TreeItem<T>, level?: number) => ReactElement<{}>;
115 showSelection?: boolean | ((item: TreeItem<T>) => boolean);
116 levelIndentation?: number;
117 itemRightPadding?: number;
118 toggleItemActive: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
119 toggleItemOpen: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
120 toggleItemSelection?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
123 * When set to true use radio buttons instead of checkboxes for item selection.
124 * This does not guarantee radio group behavior (i.e item mutual exclusivity).
125 * Any item selection logic must be done in the toggleItemActive callback prop.
127 useRadioButtons?: boolean;
130 const getActionAndId = (event: any, initAction: string | undefined = undefined) => {
131 const { nativeEvent: { target } } = event;
132 let currentTarget: HTMLElement = target as HTMLElement;
133 let action: string | undefined = initAction || currentTarget.dataset.action;
134 let id: string | undefined = currentTarget.dataset.id;
136 while (action === undefined || id === undefined) {
137 currentTarget = currentTarget.parentElement as HTMLElement;
139 if (!currentTarget) {
143 action = action || currentTarget.dataset.action;
144 id = id || currentTarget.dataset.id;
150 interface FlatTreeProps {
152 levelIndentation: number;
153 onContextMenu: Function;
154 handleToggleItemOpen: Function;
155 toggleItemActive: Function;
156 getToggableIconClassNames: Function;
157 getProperArrowAnimation: Function;
158 itemsMap?: Map<string, TreeItem<any>>;
161 useRadioButtons?: boolean;
162 handleCheckboxChange: Function;
165 const FLAT_TREE_ACTIONS = {
166 toggleOpen: 'TOGGLE_OPEN',
167 contextMenu: 'CONTEXT_MENU',
168 toggleActive: 'TOGGLE_ACTIVE',
171 const ItemIcon = React.memo(({ type, kind, active, groupClass, classes }: any) => {
172 let Icon = ProjectIcon;
174 if (groupClass === GroupClass.FILTER) {
175 Icon = FilterGroupIcon;
181 Icon = DirectoryIcon;
193 case ResourceKind.COLLECTION:
194 Icon = CollectionIcon;
201 return <Icon className={classnames({ [classes.active]: active }, classes.childItemIcon)} />;
204 const FlatTree = (props: FlatTreeProps) =>
206 onContextMenu={(event) => {
207 const id = getActionAndId(event, FLAT_TREE_ACTIONS.contextMenu)[1];
208 props.onContextMenu(event, { id } as any);
210 onClick={(event) => {
211 const [action, id] = getActionAndId(event);
214 const item = props.itemsMap ? props.itemsMap[id] : { id };
217 case FLAT_TREE_ACTIONS.toggleOpen:
218 props.handleToggleItemOpen(item as any, event);
220 case FLAT_TREE_ACTIONS.toggleActive:
221 props.toggleItemActive(event, item as any);
230 (props.it.items || [])
231 .map((item: any) => <div key={item.id} data-id={item.id}
232 className={classnames(props.classes.childItem, { [props.classes.active]: item.active })}
233 style={{ paddingLeft: `${item.depth * props.levelIndentation}px` }}>
234 <i data-action={FLAT_TREE_ACTIONS.toggleOpen} className={props.classes.toggableIconContainer}>
235 <ListItemIcon className={props.getToggableIconClassNames(item.open, item.active)}>
236 {props.getProperArrowAnimation(item.status, item.items!)}
239 {props.showSelection(item) && !props.useRadioButtons &&
241 checked={item.selected}
242 className={props.classes.checkbox}
244 onClick={props.handleCheckboxChange(item)} />}
245 {props.showSelection(item) && props.useRadioButtons &&
247 checked={item.selected}
248 className={props.classes.checkbox}
250 <div data-action={FLAT_TREE_ACTIONS.toggleActive} className={props.classes.renderContainer}>
251 <span style={{ display: 'flex', alignItems: 'center' }}>
252 <ItemIcon type={item.data.type} active={item.active} kind={item.data.kind} groupClass={item.data.kind === ResourceKind.GROUP ? item.data.groupClass : ''} classes={props.classes} />
253 <span style={{ fontSize: '0.875rem' }}>
262 export const Tree = withStyles(styles)(
263 class Component<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
264 render(): ReactElement<any> {
265 const level = this.props.level ? this.props.level : 0;
266 const { classes, render, items, toggleItemActive, toggleItemOpen, disableRipple, currentItemUuid, useRadioButtons, itemsMap } = this.props;
267 const { list, listItem, loader, toggableIconContainer, renderContainer } = classes;
268 const showSelection = typeof this.props.showSelection === 'function'
269 ? this.props.showSelection
270 : () => this.props.showSelection ? true : false;
272 const { levelIndentation = 20, itemRightPadding = 20 } = this.props;
274 return <List className={list}>
275 {items && items.map((it: TreeItem<T>, idx: number) =>
276 <div key={`item/${level}/${it.id}`}>
277 <ListItem button className={listItem}
279 paddingLeft: (level + 1) * levelIndentation,
280 paddingRight: itemRightPadding,
282 disableRipple={disableRipple}
283 onClick={event => toggleItemActive(event, it)}
284 selected={showSelection(it) && it.id === currentItemUuid}
285 onContextMenu={(event) => this.props.onContextMenu(event, it)}>
286 {it.status === TreeItemStatus.PENDING ?
287 <CircularProgress size={10} className={loader} /> : null}
288 <i onClick={(e) => this.handleToggleItemOpen(it, e)}
289 className={toggableIconContainer}>
290 <ListItemIcon className={this.getToggableIconClassNames(it.open, it.active)}>
291 {this.getProperArrowAnimation(it.status, it.items!)}
294 {showSelection(it) && !useRadioButtons &&
296 checked={it.selected}
297 indeterminate={!it.selected && it.indeterminate}
298 className={classes.checkbox}
300 onClick={this.handleCheckboxChange(it)} />}
301 {showSelection(it) && useRadioButtons &&
303 checked={it.selected}
304 className={classes.checkbox}
306 <div className={renderContainer}>
311 it.open && it.items && it.items.length > 0 &&
316 showSelection={showSelection}
317 classes={this.props.classes}
318 useRadioButtons={useRadioButtons}
319 levelIndentation={levelIndentation}
320 handleCheckboxChange={this.handleCheckboxChange}
321 onContextMenu={this.props.onContextMenu}
322 handleToggleItemOpen={this.handleToggleItemOpen}
323 toggleItemActive={this.props.toggleItemActive}
324 getToggableIconClassNames={this.getToggableIconClassNames}
325 getProperArrowAnimation={this.getProperArrowAnimation}
327 <Collapse in={it.open} timeout="auto" unmountOnExit>
329 showSelection={this.props.showSelection}
332 disableRipple={disableRipple}
333 toggleItemOpen={toggleItemOpen}
334 toggleItemActive={toggleItemActive}
336 onContextMenu={this.props.onContextMenu}
337 toggleItemSelection={this.props.toggleItemSelection} />
344 getProperArrowAnimation = (status: string, items: Array<TreeItem<T>>) => {
345 return this.isSidePanelIconNotNeeded(status, items) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
348 isSidePanelIconNotNeeded = (status: string, items: Array<TreeItem<T>>) => {
349 return status === TreeItemStatus.PENDING ||
350 (status === TreeItemStatus.LOADED && !items) ||
351 (status === TreeItemStatus.LOADED && items && items.length === 0);
354 getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
355 const { iconOpen, iconClose, active, toggableIcon } = this.props.classes;
356 return classnames(toggableIcon, {
358 [iconClose]: !isOpen,
363 handleCheckboxChange = (item: TreeItem<T>) => {
364 const { toggleItemSelection } = this.props;
365 return toggleItemSelection
366 ? (event: React.MouseEvent<HTMLElement>) => {
367 event.stopPropagation();
368 toggleItemSelection(event, item);
373 handleToggleItemOpen = (item: TreeItem<T>, event: React.MouseEvent<HTMLElement>) => {
374 event.stopPropagation();
375 this.props.toggleItemOpen(event, item);