1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import * as 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';
16 import { min } from 'lodash';
18 type CssRules = 'list'
22 | 'toggableIconContainer'
30 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
42 transform: 'translate(0px)',
45 toggableIconContainer: {
46 color: theme.palette.grey["700"],
57 color: theme.palette.primary.main,
60 transition: 'all 0.1s ease',
63 transition: 'all 0.1s ease',
64 transform: 'rotate(90deg)',
67 width: theme.spacing.unit * 3,
68 height: theme.spacing.unit * 3,
69 margin: `0 ${theme.spacing.unit}px`,
71 color: theme.palette.grey["500"],
75 export interface VirtualTreeItem<T> extends TreeItem<T> {
80 // For some reason, on TSX files it isn't accepted just one generic param, so
81 // I'm using <T, _> as a workaround.
82 export const Row = <T, _>(itemList: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) => withStyles(styles)(
83 (props: React.PropsWithChildren<ListChildComponentProps> & WithStyles<CssRules>) => {
84 const { index, style, classes } = props;
85 const it = itemList[index];
86 const level = it.level || 0;
87 const { toggleItemActive, disableRipple, currentItemUuid, useRadioButtons } = treeProps;
88 const { listItem, loader, toggableIconContainer, renderContainer } = classes;
89 const { levelIndentation = 20, itemRightPadding = 20 } = treeProps;
91 const showSelection = typeof treeProps.showSelection === 'function'
92 ? treeProps.showSelection
93 : () => treeProps.showSelection ? true : false;
95 const handleRowContextMenu = (item: VirtualTreeItem<T>) =>
96 (event: React.MouseEvent<HTMLElement>) => {
97 treeProps.onContextMenu(event, item);
100 const handleToggleItemOpen = (item: VirtualTreeItem<T>) =>
101 (event: React.MouseEvent<HTMLElement>) => {
102 event.stopPropagation();
103 treeProps.toggleItemOpen(event, item);
106 const getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
107 const { iconOpen, iconClose, active, toggableIcon } = props.classes;
108 return classnames(toggableIcon, {
110 [iconClose]: !isOpen,
115 const isSidePanelIconNotNeeded = (status: string, itemCount: number) => {
116 return status === TreeItemStatus.PENDING ||
117 (status === TreeItemStatus.LOADED && itemCount === 0);
120 const getProperArrowAnimation = (status: string, itemCount: number) => {
121 return isSidePanelIconNotNeeded(status, itemCount) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
124 const handleCheckboxChange = (item: VirtualTreeItem<T>) => {
125 const { toggleItemSelection } = treeProps;
126 return toggleItemSelection
127 ? (event: React.MouseEvent<HTMLElement>) => {
128 event.stopPropagation();
129 toggleItemSelection(event, item);
134 return <div style={style}>
135 <ListItem button className={listItem}
137 paddingLeft: (level + 1) * levelIndentation,
138 paddingRight: itemRightPadding,
140 disableRipple={disableRipple}
141 onClick={event => toggleItemActive(event, it)}
142 selected={showSelection(it) && it.id === currentItemUuid}
143 onContextMenu={handleRowContextMenu(it)}>
144 {it.status === TreeItemStatus.PENDING ?
145 <CircularProgress size={10} className={loader} /> : null}
146 <i onClick={handleToggleItemOpen(it)}
147 className={toggableIconContainer}>
148 <ListItemIcon className={getToggableIconClassNames(it.open, it.active)}>
149 {getProperArrowAnimation(it.status, it.itemCount!)}
152 {showSelection(it) && !useRadioButtons &&
154 checked={it.selected}
155 className={classes.checkbox}
157 onClick={handleCheckboxChange(it)} />}
158 {showSelection(it) && useRadioButtons &&
160 checked={it.selected}
161 className={classes.checkbox}
163 <div className={renderContainer}>
172 export const VirtualList = <T, _>(height: number, width: number, items: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) =>
175 itemCount={items.length}
179 {Row(items, render, treeProps)}
182 export const VirtualTree = (maxElements: number) => withStyles(styles)(
183 class Component<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
184 render(): ReactElement<any> {
185 const { items, render } = this.props;
187 // Virtual list viewport's maximum height
188 const itemsQty = items && items.length || 0;
189 const viewportHeight = min([itemsQty, maxElements])! * itemSize;
191 return <div style={{height: viewportHeight}}><AutoSizer>
192 {({ height, width }) => {
193 return VirtualList(height, width, items || [], render, this.props);