1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import 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 // eslint-disable-next-line
88 export const Row = <T, _>(itemList: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) => withStyles(styles)(
89 (props: React.PropsWithChildren<ListChildComponentProps> & WithStyles<CssRules>) => {
90 const { index, style, classes } = props;
91 const it = itemList[index];
92 const level = it.level || 0;
93 const { toggleItemActive, disableRipple, currentItemUuid, useRadioButtons } = treeProps;
94 const { listItem, loader, toggableIconContainer, renderContainer, virtualFileTree } = classes;
95 const { levelIndentation = 20, itemRightPadding = 20 } = treeProps;
97 const showSelection = typeof treeProps.showSelection === 'function'
98 ? treeProps.showSelection
99 : () => treeProps.showSelection ? true : false;
101 const handleRowContextMenu = (item: VirtualTreeItem<T>) =>
102 (event: React.MouseEvent<HTMLElement>) => {
103 treeProps.onContextMenu(event, item);
106 const handleToggleItemOpen = (item: VirtualTreeItem<T>) =>
107 (event: React.MouseEvent<HTMLElement>) => {
108 event.stopPropagation();
109 treeProps.toggleItemOpen(event, item);
112 const getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
113 const { iconOpen, iconClose, active, toggableIcon } = props.classes;
114 return classnames(toggableIcon, {
116 [iconClose]: !isOpen,
121 const isSidePanelIconNotNeeded = (status: string, itemCount: number) => {
122 return status === TreeItemStatus.PENDING ||
123 (status === TreeItemStatus.LOADED && itemCount === 0);
126 const getProperArrowAnimation = (status: string, itemCount: number) => {
127 return isSidePanelIconNotNeeded(status, itemCount) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
130 const handleCheckboxChange = (item: VirtualTreeItem<T>) => {
131 const { toggleItemSelection } = treeProps;
132 return toggleItemSelection
133 ? (event: React.MouseEvent<HTMLElement>) => {
134 event.stopPropagation();
135 toggleItemSelection(event, item);
140 return <div className={virtualFileTree} data-cy='virtual-file-tree' style={style}>
141 <ListItem button className={listItem}
143 paddingLeft: (level + 1) * levelIndentation,
144 paddingRight: itemRightPadding,
146 disableRipple={disableRipple}
147 onClick={event => toggleItemActive(event, it)}
148 selected={showSelection(it) && it.id === currentItemUuid}
149 onContextMenu={handleRowContextMenu(it)}>
150 {it.status === TreeItemStatus.PENDING ?
151 <CircularProgress size={10} className={loader} /> : null}
152 <i onClick={handleToggleItemOpen(it)}
153 className={toggableIconContainer}>
154 <ListItemIcon className={getToggableIconClassNames(it.open, it.active)}>
155 {getProperArrowAnimation(it.status, it.itemCount!)}
158 {showSelection(it) && !useRadioButtons &&
160 checked={it.selected}
161 className={classes.checkbox}
163 onClick={handleCheckboxChange(it)} />}
164 {showSelection(it) && useRadioButtons &&
166 checked={it.selected}
167 className={classes.checkbox}
169 <div className={renderContainer}>
178 // eslint-disable-next-line
179 export const VirtualList = <T, _>(height: number, width: number, items: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) =>
182 itemCount={items.length}
186 {Row(items, render, treeProps)}
189 export const VirtualTree = withStyles(styles)(
190 class Component<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
191 render(): ReactElement<any> {
192 const { items, render } = this.props;
194 {({ height, width }) => {
195 return VirtualList(height, width, items || [], render, this.props);