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, FreezeIcon } 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'
32 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
41 transform: 'translate(0px)',
44 toggableIconContainer: {
45 color: theme.palette.grey["700"],
56 transition: 'all 0.1s ease',
59 transition: 'all 0.1s ease',
60 transform: 'rotate(90deg)',
63 width: theme.spacing.unit * 3,
64 height: theme.spacing.unit * 3,
65 margin: `0 ${theme.spacing.unit}px`,
67 color: theme.palette.grey["500"],
76 backgroundColor: 'rgba(0, 0, 0, 0.08)',
82 color: 'rgba(0, 0, 0, 0.54)',
85 color: theme.palette.primary.main,
89 color: theme.palette.grey["600"],
94 export enum TreeItemStatus {
100 export interface TreeItem<T> {
106 initialState?: boolean;
107 indeterminate?: boolean;
109 status: TreeItemStatus;
110 items?: Array<TreeItem<T>>;
114 export interface TreeProps<T> {
115 disableRipple?: boolean;
116 currentItemUuid?: string;
117 items?: Array<TreeItem<T>>;
119 itemsMap?: Map<string, TreeItem<T>>;
120 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
121 render: (item: TreeItem<T>, level?: number) => ReactElement<{}>;
122 showSelection?: boolean | ((item: TreeItem<T>) => boolean);
123 levelIndentation?: number;
124 itemRightPadding?: number;
125 toggleItemActive: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
126 toggleItemOpen: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
127 toggleItemSelection?: (event: React.MouseEvent<HTMLElement>, item: TreeItem<T>) => void;
130 * When set to true use radio buttons instead of checkboxes for item selection.
131 * This does not guarantee radio group behavior (i.e item mutual exclusivity).
132 * Any item selection logic must be done in the toggleItemActive callback prop.
134 useRadioButtons?: boolean;
137 const getActionAndId = (event: any, initAction: string | undefined = undefined) => {
138 const { nativeEvent: { target } } = event;
139 let currentTarget: HTMLElement = target as HTMLElement;
140 let action: string | undefined = initAction || currentTarget.dataset.action;
141 let id: string | undefined = currentTarget.dataset.id;
143 while (action === undefined || id === undefined) {
144 currentTarget = currentTarget.parentElement as HTMLElement;
146 if (!currentTarget) {
150 action = action || currentTarget.dataset.action;
151 id = id || currentTarget.dataset.id;
157 interface FlatTreeProps {
159 levelIndentation: number;
160 onContextMenu: Function;
161 handleToggleItemOpen: Function;
162 toggleItemActive: Function;
163 getToggableIconClassNames: Function;
164 getProperArrowAnimation: Function;
165 itemsMap?: Map<string, TreeItem<any>>;
168 useRadioButtons?: boolean;
169 handleCheckboxChange: Function;
172 const FLAT_TREE_ACTIONS = {
173 toggleOpen: 'TOGGLE_OPEN',
174 contextMenu: 'CONTEXT_MENU',
175 toggleActive: 'TOGGLE_ACTIVE',
178 const ItemIcon = React.memo(({ type, kind, active, groupClass, classes }: any) => {
179 let Icon = ProjectIcon;
181 if (groupClass === GroupClass.FILTER) {
182 Icon = FilterGroupIcon;
188 Icon = DirectoryIcon;
200 case ResourceKind.COLLECTION:
201 Icon = CollectionIcon;
208 return <Icon className={classnames({ [classes.active]: active }, classes.childItemIcon)} />;
211 const FlatTree = (props: FlatTreeProps) =>
213 onContextMenu={(event) => {
214 const id = getActionAndId(event, FLAT_TREE_ACTIONS.contextMenu)[1];
215 props.onContextMenu(event, { id } as any);
217 onClick={(event) => {
218 const [action, id] = getActionAndId(event);
221 const item = props.itemsMap ? props.itemsMap[id] : { id };
224 case FLAT_TREE_ACTIONS.toggleOpen:
225 props.handleToggleItemOpen(item as any, event);
227 case FLAT_TREE_ACTIONS.toggleActive:
228 props.toggleItemActive(event, item as any);
237 (props.it.items || [])
238 .map((item: any) => <div key={item.id} data-id={item.id}
239 className={classnames(props.classes.childItem, { [props.classes.active]: item.active })}
240 style={{ paddingLeft: `${item.depth * props.levelIndentation}px` }}>
241 <i data-action={FLAT_TREE_ACTIONS.toggleOpen} className={props.classes.toggableIconContainer}>
242 <ListItemIcon className={props.getToggableIconClassNames(item.open, item.active)}>
243 {props.getProperArrowAnimation(item.status, item.items!)}
246 {props.showSelection(item) && !props.useRadioButtons &&
248 checked={item.selected}
249 className={props.classes.checkbox}
251 onClick={props.handleCheckboxChange(item)} />}
252 {props.showSelection(item) && props.useRadioButtons &&
254 checked={item.selected}
255 className={props.classes.checkbox}
257 <div data-action={FLAT_TREE_ACTIONS.toggleActive} className={props.classes.renderContainer}>
258 <span style={{ display: 'flex', alignItems: 'center' }}>
259 <ItemIcon type={item.data.type} active={item.active} kind={item.data.kind} groupClass={item.data.kind === ResourceKind.GROUP ? item.data.groupClass : ''} classes={props.classes} />
260 <span style={{ fontSize: '0.875rem' }}>
264 !!item.data.frozenByUuid ? <FreezeIcon className={props.classes.frozenIcon} /> : null
272 export const Tree = withStyles(styles)(
273 class Component<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
274 render(): ReactElement<any> {
275 const level = this.props.level ? this.props.level : 0;
276 const { classes, render, items, toggleItemActive, toggleItemOpen, disableRipple, currentItemUuid, useRadioButtons, itemsMap } = this.props;
277 const { list, listItem, loader, toggableIconContainer, renderContainer } = classes;
278 const showSelection = typeof this.props.showSelection === 'function'
279 ? this.props.showSelection
280 : () => this.props.showSelection ? true : false;
282 const { levelIndentation = 20, itemRightPadding = 20 } = this.props;
283 return <List className={list}>
284 {items && items.map((it: TreeItem<T>, idx: number) =>
285 <div key={`item/${level}/${it.id}`}>
286 <ListItem button className={listItem}
288 paddingLeft: (level + 1) * levelIndentation,
289 paddingRight: itemRightPadding,
291 disableRipple={disableRipple}
292 onClick={event => toggleItemActive(event, it)}
293 selected={showSelection(it) && it.id === currentItemUuid}
294 onContextMenu={(event) => this.props.onContextMenu(event, it)}>
295 {it.status === TreeItemStatus.PENDING ?
296 <CircularProgress size={10} className={loader} /> : null}
297 <i onClick={(e) => this.handleToggleItemOpen(it, e)}
298 className={toggableIconContainer}>
299 <ListItemIcon className={this.getToggableIconClassNames(it.open, it.active)}>
300 {this.getProperArrowAnimation(it.status, it.items!)}
303 {showSelection(it) && !useRadioButtons &&
305 checked={it.selected}
306 indeterminate={!it.selected && it.indeterminate}
307 className={classes.checkbox}
309 onClick={this.handleCheckboxChange(it)} />}
310 {showSelection(it) && useRadioButtons &&
312 checked={it.selected}
313 className={classes.checkbox}
315 <div className={renderContainer}>
320 it.open && it.items && it.items.length > 0 &&
325 showSelection={showSelection}
326 classes={this.props.classes}
327 useRadioButtons={useRadioButtons}
328 levelIndentation={levelIndentation}
329 handleCheckboxChange={this.handleCheckboxChange}
330 onContextMenu={this.props.onContextMenu}
331 handleToggleItemOpen={this.handleToggleItemOpen}
332 toggleItemActive={this.props.toggleItemActive}
333 getToggableIconClassNames={this.getToggableIconClassNames}
334 getProperArrowAnimation={this.getProperArrowAnimation}
336 <Collapse in={it.open} timeout="auto" unmountOnExit>
338 showSelection={this.props.showSelection}
341 disableRipple={disableRipple}
342 toggleItemOpen={toggleItemOpen}
343 toggleItemActive={toggleItemActive}
345 onContextMenu={this.props.onContextMenu}
346 toggleItemSelection={this.props.toggleItemSelection} />
353 getProperArrowAnimation = (status: string, items: Array<TreeItem<T>>) => {
354 return this.isSidePanelIconNotNeeded(status, items) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
357 isSidePanelIconNotNeeded = (status: string, items: Array<TreeItem<T>>) => {
358 return status === TreeItemStatus.PENDING ||
359 (status === TreeItemStatus.LOADED && !items) ||
360 (status === TreeItemStatus.LOADED && items && items.length === 0);
363 getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
364 const { iconOpen, iconClose, active, toggableIcon } = this.props.classes;
365 return classnames(toggableIcon, {
367 [iconClose]: !isOpen,
372 handleCheckboxChange = (item: TreeItem<T>) => {
373 const { toggleItemSelection } = this.props;
374 return toggleItemSelection
375 ? (event: React.MouseEvent<HTMLElement>) => {
376 event.stopPropagation();
377 toggleItemSelection(event, item);
382 handleToggleItemOpen = (item: TreeItem<T>, event: React.MouseEvent<HTMLElement>) => {
383 event.stopPropagation();
384 this.props.toggleItemOpen(event, item);