Merge branch '18594-Collection-Advanced-Menu-is-trying-to-fetch-User-record-with...
[arvados.git] / src / components / data-table / data-table.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles, IconButton } from '@material-ui/core';
7 import classnames from 'classnames';
8 import { DataColumn, SortDirection } from './data-column';
9 import { DataTableDefaultView } from '../data-table-default-view/data-table-default-view';
10 import { DataTableFilters } from '../data-table-filters/data-table-filters-tree';
11 import { DataTableFiltersPopover } from '../data-table-filters/data-table-filters-popover';
12 import { countNodes } from 'models/tree';
13 import { PendingIcon } from 'components/icon/icon';
14 import { SvgIconProps } from '@material-ui/core/SvgIcon';
15 import ArrowDownwardIcon from '@material-ui/icons/ArrowDownward';
16
17 export type DataColumns<T> = Array<DataColumn<T>>;
18
19 export enum DataTableFetchMode {
20     PAGINATED,
21     INFINITE
22 }
23
24 export interface DataTableDataProps<T> {
25     items: T[];
26     columns: DataColumns<T>;
27     onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
28     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
29     onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
30     onSortToggle: (column: DataColumn<T>) => void;
31     onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
32     extractKey?: (item: T) => React.Key;
33     working?: boolean;
34     defaultView?: React.ReactNode;
35     currentItemUuid?: string;
36     currentRoute?: string;
37 }
38
39 type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell' | 'arrow' | 'arrowButton' | 'tableCellWorkflows' | 'loader';
40
41 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
42     root: {
43         width: '100%',
44     },
45     content: {
46         display: 'inline-block',
47         width: '100%',
48     },
49     tableBody: {
50         background: theme.palette.background.paper
51     },
52     loader: {
53         left: '50%',
54         marginLeft: '-84px',
55         position: 'absolute'
56     },
57     noItemsInfo: {
58         textAlign: "center",
59         padding: theme.spacing.unit
60     },
61     tableCell: {
62         wordWrap: 'break-word',
63         paddingRight: '24px'
64     },
65     tableCellWorkflows: {
66         '&:nth-last-child(2)': {
67             padding: '0px',
68             maxWidth: '48px'
69         },
70         '&:last-child': {
71             padding: '0px',
72             paddingRight: '24px',
73             width: '48px'
74         }
75     },
76     arrow: {
77         margin: 0
78     },
79     arrowButton: {
80         color: theme.palette.text.primary
81     }
82 });
83
84 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
85
86 export const DataTable = withStyles(styles)(
87     class Component<T> extends React.Component<DataTableProps<T>> {
88         render() {
89             const { items, classes } = this.props;
90             return <div className={classes.root}>
91                 <div className={classes.content}>
92                     <Table>
93                         <TableHead>
94                             <TableRow>
95                                 {this.mapVisibleColumns(this.renderHeadCell)}
96                             </TableRow>
97                         </TableHead>
98                         <TableBody className={classes.tableBody}>
99                             { this.props.working !== undefined && !this.props.working && items.map(this.renderBodyRow) }
100                         </TableBody>
101                     </Table>
102                     { this.props.working &&
103                         <div className={classes.loader}>
104                             <DataTableDefaultView
105                                 icon={PendingIcon}
106                                 messages={['Loading data, please wait.']} />
107                         </div> }
108                     {items.length === 0 && this.props.working !== undefined && !this.props.working && this.renderNoItemsPlaceholder()}
109                 </div>
110             </div>;
111         }
112
113         renderNoItemsPlaceholder = () => {
114             return this.props.defaultView
115                 ? this.props.defaultView
116                 : <DataTableDefaultView />;
117         }
118
119         renderHeadCell = (column: DataColumn<T>, index: number) => {
120             const { name, key, renderHeader, filters, sortDirection } = column;
121             const { onSortToggle, onFiltersChange, classes } = this.props;
122             return <TableCell className={classes.tableCell} key={key || index}>
123                 {renderHeader ?
124                     renderHeader() :
125                     countNodes(filters) > 0
126                         ? <DataTableFiltersPopover
127                             name={`${name} filters`}
128                             mutuallyExclusive={column.mutuallyExclusiveFilters}
129                             onChange={filters =>
130                                 onFiltersChange &&
131                                 onFiltersChange(filters, column)}
132                             filters={filters}>
133                             {name}
134                         </DataTableFiltersPopover>
135                         : sortDirection
136                             ? <TableSortLabel
137                                 active={sortDirection !== SortDirection.NONE}
138                                 direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
139                                 IconComponent={this.ArrowIcon}
140                                 hideSortIcon
141                                 onClick={() =>
142                                     onSortToggle &&
143                                     onSortToggle(column)}>
144                                 {name}
145                             </TableSortLabel>
146                             : <span>
147                                 {name}
148                             </span>}
149             </TableCell>;
150         }
151
152         ArrowIcon = ({ className, ...props }: SvgIconProps) => (
153             <IconButton component='span' className={this.props.classes.arrowButton} tabIndex={-1}>
154                 <ArrowDownwardIcon {...props} className={classnames(className, this.props.classes.arrow)} />
155             </IconButton>
156         )
157
158         renderBodyRow = (item: any, index: number) => {
159             const { onRowClick, onRowDoubleClick, extractKey, classes, currentItemUuid, currentRoute } = this.props;
160             return <TableRow
161                 hover
162                 key={extractKey ? extractKey(item) : index}
163                 onClick={event => onRowClick && onRowClick(event, item)}
164                 onContextMenu={this.handleRowContextMenu(item)}
165                 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}
166                 selected={item === currentItemUuid}>
167                 {this.mapVisibleColumns((column, index) => (
168                     <TableCell key={column.key || index} className={currentRoute === '/workflows' ? classes.tableCellWorkflows : classes.tableCell}>
169                         {column.render(item)}
170                     </TableCell>
171                 ))}
172             </TableRow>;
173         }
174
175         mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
176             return this.props.columns.filter(column => column.selected).map(fn);
177         }
178
179         handleRowContextMenu = (item: T) =>
180             (event: React.MouseEvent<HTMLElement>) =>
181                 this.props.onContextMenu(event, item)
182
183     }
184 );