15610: Fixes layout issues with the collection panel file listing.
[arvados-workbench2.git] / src / components / tree / virtual-tree.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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";
11
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
17 type CssRules = 'list'
18     | 'listItem'
19     | 'active'
20     | 'loader'
21     | 'toggableIconContainer'
22     | 'iconClose'
23     | 'renderContainer'
24     | 'iconOpen'
25     | 'toggableIcon'
26     | 'checkbox'
27     | 'virtualizedList';
28
29 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30     list: {
31         padding: '3px 0px',
32     },
33     virtualizedList: {
34         height: '200px',
35     },
36     listItem: {
37         padding: '3px 0px',
38     },
39     loader: {
40         position: 'absolute',
41         transform: 'translate(0px)',
42         top: '3px'
43     },
44     toggableIconContainer: {
45         color: theme.palette.grey["700"],
46         height: '14px',
47         width: '14px',
48     },
49     toggableIcon: {
50         fontSize: '14px'
51     },
52     renderContainer: {
53         flex: 1
54     },
55     active: {
56         color: theme.palette.primary.main,
57     },
58     iconClose: {
59         transition: 'all 0.1s ease',
60     },
61     iconOpen: {
62         transition: 'all 0.1s ease',
63         transform: 'rotate(90deg)',
64     },
65     checkbox: {
66         width: theme.spacing.unit * 3,
67         height: theme.spacing.unit * 3,
68         margin: `0 ${theme.spacing.unit}px`,
69         padding: 0,
70         color: theme.palette.grey["500"],
71     }
72 });
73
74 export interface VirtualTreeItem<T> extends TreeItem<T> {
75     itemCount?: number;
76     level?: number;
77 }
78
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;
89
90         const showSelection = typeof treeProps.showSelection === 'function'
91             ? treeProps.showSelection
92             : () => treeProps.showSelection ? true : false;
93
94         const handleRowContextMenu = (item: VirtualTreeItem<T>) =>
95             (event: React.MouseEvent<HTMLElement>) => {
96                 treeProps.onContextMenu(event, item);
97             };
98
99         const handleToggleItemOpen = (item: VirtualTreeItem<T>) =>
100             (event: React.MouseEvent<HTMLElement>) => {
101                 event.stopPropagation();
102                 treeProps.toggleItemOpen(event, item);
103             };
104
105         const getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
106             const { iconOpen, iconClose, active, toggableIcon } = props.classes;
107             return classnames(toggableIcon, {
108                 [iconOpen]: isOpen,
109                 [iconClose]: !isOpen,
110                 [active]: isActive
111             });
112         };
113
114         const isSidePanelIconNotNeeded = (status: string, itemCount: number) => {
115             return status === TreeItemStatus.PENDING ||
116                 (status === TreeItemStatus.LOADED && itemCount === 0);
117         };
118
119         const getProperArrowAnimation = (status: string, itemCount: number) => {
120             return isSidePanelIconNotNeeded(status, itemCount) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
121         };
122
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);
129                 }
130                 : undefined;
131         };
132
133         return <div style={style}>
134             <ListItem button className={listItem}
135                 style={{
136                     paddingLeft: (level + 1) * levelIndentation,
137                     paddingRight: itemRightPadding,
138                 }}
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!)}
149                     </ListItemIcon>
150                 </i>
151                 {showSelection(it) && !useRadioButtons &&
152                     <Checkbox
153                         checked={it.selected}
154                         className={classes.checkbox}
155                         color="primary"
156                         onClick={handleCheckboxChange(it)} />}
157                 {showSelection(it) && useRadioButtons &&
158                     <Radio
159                         checked={it.selected}
160                         className={classes.checkbox}
161                         color="primary" />}
162                 <div className={renderContainer}>
163                     {render(it, level)}
164                 </div>
165             </ListItem>
166         </div>;
167     });
168
169 const itemSize = 30;
170
171 export const VirtualList = <T, _>(height: number, width: number, items: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) =>
172     <FixedSizeList
173         height={height}
174         itemCount={items.length}
175         itemSize={itemSize}
176         width={width}
177     >
178         {Row(items, render, treeProps)}
179     </FixedSizeList>;
180
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;
185
186             return <AutoSizer>
187                 {({ height, width }) => {
188                     return VirtualList(height, width, items || [], render, this.props);
189                 }}
190             </AutoSizer>;
191         }
192     }
193 );