17426: Add "enableWhenPristine" option for dialog boxes.
[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 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     | 'virtualFileTree'
28     | 'virtualizedList';
29
30 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
31     list: {
32         padding: '3px 0px',
33     },
34     virtualFileTree: {
35         "&:last-child": {
36             paddingBottom: 20
37           }
38     },
39     virtualizedList: {
40         height: '200px',
41     },
42     listItem: {
43         padding: '3px 0px',
44     },
45     loader: {
46         position: 'absolute',
47         transform: 'translate(0px)',
48         top: '3px'
49     },
50     toggableIconContainer: {
51         color: theme.palette.grey["700"],
52         height: '14px',
53         width: '14px',
54     },
55     toggableIcon: {
56         fontSize: '14px'
57     },
58     renderContainer: {
59         flex: 1
60     },
61     active: {
62         color: theme.palette.primary.main,
63     },
64     iconClose: {
65         transition: 'all 0.1s ease',
66     },
67     iconOpen: {
68         transition: 'all 0.1s ease',
69         transform: 'rotate(90deg)',
70     },
71     checkbox: {
72         width: theme.spacing.unit * 3,
73         height: theme.spacing.unit * 3,
74         margin: `0 ${theme.spacing.unit}px`,
75         padding: 0,
76         color: theme.palette.grey["500"],
77     }
78 });
79
80 export interface VirtualTreeItem<T> extends TreeItem<T> {
81     itemCount?: number;
82     level?: number;
83 }
84
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;
95
96         const showSelection = typeof treeProps.showSelection === 'function'
97             ? treeProps.showSelection
98             : () => treeProps.showSelection ? true : false;
99
100         const handleRowContextMenu = (item: VirtualTreeItem<T>) =>
101             (event: React.MouseEvent<HTMLElement>) => {
102                 treeProps.onContextMenu(event, item);
103             };
104
105         const handleToggleItemOpen = (item: VirtualTreeItem<T>) =>
106             (event: React.MouseEvent<HTMLElement>) => {
107                 event.stopPropagation();
108                 treeProps.toggleItemOpen(event, item);
109             };
110
111         const getToggableIconClassNames = (isOpen?: boolean, isActive?: boolean) => {
112             const { iconOpen, iconClose, active, toggableIcon } = props.classes;
113             return classnames(toggableIcon, {
114                 [iconOpen]: isOpen,
115                 [iconClose]: !isOpen,
116                 [active]: isActive
117             });
118         };
119
120         const isSidePanelIconNotNeeded = (status: string, itemCount: number) => {
121             return status === TreeItemStatus.PENDING ||
122                 (status === TreeItemStatus.LOADED && itemCount === 0);
123         };
124
125         const getProperArrowAnimation = (status: string, itemCount: number) => {
126             return isSidePanelIconNotNeeded(status, itemCount) ? <span /> : <SidePanelRightArrowIcon style={{ fontSize: '14px' }} />;
127         };
128
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);
135                 }
136                 : undefined;
137         };
138
139         return <div className={virtualFileTree} data-cy='virtual-file-tree' style={style}>
140             <ListItem button className={listItem}
141                 style={{
142                     paddingLeft: (level + 1) * levelIndentation,
143                     paddingRight: itemRightPadding,
144                 }}
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!)}
155                     </ListItemIcon>
156                 </i>
157                 {showSelection(it) && !useRadioButtons &&
158                     <Checkbox
159                         checked={it.selected}
160                         className={classes.checkbox}
161                         color="primary"
162                         onClick={handleCheckboxChange(it)} />}
163                 {showSelection(it) && useRadioButtons &&
164                     <Radio
165                         checked={it.selected}
166                         className={classes.checkbox}
167                         color="primary" />}
168                 <div className={renderContainer}>
169                     {render(it, level)}
170                 </div>
171             </ListItem>
172         </div>;
173     });
174
175 const itemSize = 30;
176
177 export const VirtualList = <T, _>(height: number, width: number, items: VirtualTreeItem<T>[], render: any, treeProps: TreeProps<T>) =>
178     <FixedSizeList
179         height={height}
180         itemCount={items.length}
181         itemSize={itemSize}
182         width={width}
183     >
184         {Row(items, render, treeProps)}
185     </FixedSizeList>;
186
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;
191
192             return <AutoSizer>
193                 {({ height, width }) => {
194                     return VirtualList(height, width, items || [], render, this.props);
195                 }}
196             </AutoSizer>;
197         }
198     }
199 );