1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import classnames from "classnames";
7 import { StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core/styles';
8 import { ReactElement } from "react";
9 import { FixedSizeList, ListChildComponentProps } from "react-window";
10 import AutoSizer from "react-virtualized-auto-sizer";
12 import { ArvadosTheme } from '~/common/custom-theme';
13 import { TreeItem, TreeProps, TreeItemStatus } from './tree';
14 import { ListItem, Radio, Checkbox, CircularProgress, ListItemIcon } from '@material-ui/core';
15 import { SidePanelRightArrowIcon } from '../icon/icon';
17 type CssRules = 'list'
21 | 'toggableIconContainer'
30 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
47 transform: 'translate(0px)',
50 toggableIconContainer: {
51 color: theme.palette.grey["700"],
62 color: theme.palette.primary.main,
65 transition: 'all 0.1s ease',
68 transition: 'all 0.1s ease',
69 transform: 'rotate(90deg)',
72 width: theme.spacing.unit * 3,
73 height: theme.spacing.unit * 3,
74 margin: `0 ${theme.spacing.unit}px`,
76 color: theme.palette.grey["500"],
80 export interface VirtualTreeItem<T> extends TreeItem<T> {
85 // For some reason, on TSX files it isn't accepted just one generic param, so
86 // I'm using <T, _> as a workaround.
87 export const Row = <T, _>(itemList: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) => withStyles(styles)(
88 (props: React.PropsWithChildren<ListChildComponentProps> & WithStyles<CssRules>) => {
89 const { index, style, classes } = props;
90 const it = itemList[index];
91 const level = it.level || 0;
92 const { toggleItemActive, disableRipple, currentItemUuid, useRadioButtons } = treeProps;
93 const { listItem, loader, toggableIconContainer, renderContainer, virtualFileTree } = classes;
94 const { levelIndentation = 20, itemRightPadding = 20 } = treeProps;
96 const showSelection = typeof treeProps.showSelection === 'function'
97 ? treeProps.showSelection
98 : () => treeProps.showSelection ? true : false;
100 const handleRowContextMenu = (item: VirtualTreeItem<T>) =>
101 (event: React.MouseEvent<HTMLElement>) => {
102 treeProps.onContextMenu(event, item);
105 const handleToggleItemOpen = (item: VirtualTreeItem<T>) =>
106 (event: React.MouseEvent<HTMLElement>) => {
107 event.stopPropagation();
108 treeProps.toggleItemOpen(event, item);
111 const getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
112 const { iconOpen, iconClose, active, toggableIcon } = props.classes;
113 return classnames(toggableIcon, {
115 [iconClose]: !isOpen,
120 const isSidePanelIconNotNeeded = (status: string, itemCount: number) => {
121 return status === TreeItemStatus.PENDING ||
122 (status === TreeItemStatus.LOADED && itemCount === 0);
125 const getProperArrowAnimation = (status: string, itemCount: number) => {
126 return isSidePanelIconNotNeeded(status, itemCount) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
129 const handleCheckboxChange = (item: VirtualTreeItem<T>) => {
130 const { toggleItemSelection } = treeProps;
131 return toggleItemSelection
132 ? (event: React.MouseEvent<HTMLElement>) => {
133 event.stopPropagation();
134 toggleItemSelection(event, item);
139 return <div className={virtualFileTree} data-cy='virtual-file-tree' style={style}>
140 <ListItem button className={listItem}
142 paddingLeft: (level + 1) * levelIndentation,
143 paddingRight: itemRightPadding,
145 disableRipple={disableRipple}
146 onClick={event => toggleItemActive(event, it)}
147 selected={showSelection(it) && it.id === currentItemUuid}
148 onContextMenu={handleRowContextMenu(it)}>
149 {it.status === TreeItemStatus.PENDING ?
150 <CircularProgress size={10} className={loader} /> : null}
151 <i onClick={handleToggleItemOpen(it)}
152 className={toggableIconContainer}>
153 <ListItemIcon className={getToggableIconClassNames(it.open, it.active)}>
154 {getProperArrowAnimation(it.status, it.itemCount!)}
157 {showSelection(it) && !useRadioButtons &&
159 checked={it.selected}
160 className={classes.checkbox}
162 onClick={handleCheckboxChange(it)} />}
163 {showSelection(it) && useRadioButtons &&
165 checked={it.selected}
166 className={classes.checkbox}
168 <div className={renderContainer}>
177 export const VirtualList = <T, _>(height: number, width: number, items: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) =>
180 itemCount={items.length}
184 {Row(items, render, treeProps)}
187 export const VirtualTree = withStyles(styles)(
188 class Component<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
189 render(): ReactElement<any> {
190 const { items, render } = this.props;
192 {({ height, width }) => {
193 return VirtualList(height, width, items || [], render, this.props);