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'
29 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
41 transform: 'translate(0px)',
44 toggableIconContainer: {
45 color: theme.palette.grey["700"],
56 color: theme.palette.primary.main,
59 transition: 'all 0.1s ease',
62 transition: 'all 0.1s ease',
63 transform: 'rotate(90deg)',
66 width: theme.spacing.unit * 3,
67 height: theme.spacing.unit * 3,
68 margin: `0 ${theme.spacing.unit}px`,
70 color: theme.palette.grey["500"],
74 export interface VirtualTreeItem<T> extends TreeItem<T> {
79 // For some reason, on TSX files it isn't accepted just one generic param, so
80 // I'm using <T, _> as a workaround.
81 export const Row = <T, _>(itemList: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) => withStyles(styles)(
82 (props: React.PropsWithChildren<ListChildComponentProps> & WithStyles<CssRules>) => {
83 const { index, style, classes } = props;
84 const it = itemList[index];
85 const level = it.level || 0;
86 const { toggleItemActive, disableRipple, currentItemUuid, useRadioButtons } = treeProps;
87 const { listItem, loader, toggableIconContainer, renderContainer } = classes;
88 const { levelIndentation = 20, itemRightPadding = 20 } = treeProps;
90 const showSelection = typeof treeProps.showSelection === 'function'
91 ? treeProps.showSelection
92 : () => treeProps.showSelection ? true : false;
94 const handleRowContextMenu = (item: VirtualTreeItem<T>) =>
95 (event: React.MouseEvent<HTMLElement>) => {
96 treeProps.onContextMenu(event, item);
99 const handleToggleItemOpen = (item: VirtualTreeItem<T>) =>
100 (event: React.MouseEvent<HTMLElement>) => {
101 event.stopPropagation();
102 treeProps.toggleItemOpen(event, item);
105 const getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
106 const { iconOpen, iconClose, active, toggableIcon } = props.classes;
107 return classnames(toggableIcon, {
109 [iconClose]: !isOpen,
114 const isSidePanelIconNotNeeded = (status: string, itemCount: number) => {
115 return status === TreeItemStatus.PENDING ||
116 (status === TreeItemStatus.LOADED && itemCount === 0);
119 const getProperArrowAnimation = (status: string, itemCount: number) => {
120 return isSidePanelIconNotNeeded(status, itemCount) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
123 const handleCheckboxChange = (item: VirtualTreeItem<T>) => {
124 const { toggleItemSelection } = treeProps;
125 return toggleItemSelection
126 ? (event: React.MouseEvent<HTMLElement>) => {
127 event.stopPropagation();
128 toggleItemSelection(event, item);
133 return <div data-cy='virtual-file-tree' style={style}>
134 <ListItem button className={listItem}
136 paddingLeft: (level + 1) * levelIndentation,
137 paddingRight: itemRightPadding,
139 disableRipple={disableRipple}
140 onClick={event => toggleItemActive(event, it)}
141 selected={showSelection(it) && it.id === currentItemUuid}
142 onContextMenu={handleRowContextMenu(it)}>
143 {it.status === TreeItemStatus.PENDING ?
144 <CircularProgress size={10} className={loader} /> : null}
145 <i onClick={handleToggleItemOpen(it)}
146 className={toggableIconContainer}>
147 <ListItemIcon className={getToggableIconClassNames(it.open, it.active)}>
148 {getProperArrowAnimation(it.status, it.itemCount!)}
151 {showSelection(it) && !useRadioButtons &&
153 checked={it.selected}
154 className={classes.checkbox}
156 onClick={handleCheckboxChange(it)} />}
157 {showSelection(it) && useRadioButtons &&
159 checked={it.selected}
160 className={classes.checkbox}
162 <div className={renderContainer}>
171 export const VirtualList = <T, _>(height: number, width: number, items: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) =>
174 itemCount={items.length}
178 {Row(items, render, treeProps)}
181 export const VirtualTree = withStyles(styles)(
182 class Component<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
183 render(): ReactElement<any> {
184 const { items, render } = this.props;
186 {({ height, width }) => {
187 return VirtualList(height, width, items || [], render, this.props);