1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
17 export type DataColumns<T> = Array<DataColumn<T>>;
19 export enum DataTableFetchMode {
24 export interface DataTableDataProps<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;
34 defaultView?: React.ReactNode;
35 currentItemUuid?: string;
36 currentRoute?: string;
39 type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell' | 'arrow' | 'arrowButton' | 'tableCellWorkflows' | 'loader';
41 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
46 display: 'inline-block',
50 background: theme.palette.background.paper
59 padding: theme.spacing.unit
62 wordWrap: 'break-word',
66 '&:nth-last-child(2)': {
80 color: theme.palette.text.primary
84 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
86 export const DataTable = withStyles(styles)(
87 class Component<T> extends React.Component<DataTableProps<T>> {
89 const { items, classes } = this.props;
90 return <div className={classes.root}>
91 <div className={classes.content}>
95 {this.mapVisibleColumns(this.renderHeadCell)}
98 <TableBody className={classes.tableBody}>
99 { this.props.working !== undefined && !this.props.working && items.map(this.renderBodyRow) }
102 { this.props.working &&
103 <div className={classes.loader}>
104 <DataTableDefaultView
106 messages={['Loading data, please wait.']} />
108 {items.length === 0 && this.props.working !== undefined && !this.props.working && this.renderNoItemsPlaceholder()}
113 renderNoItemsPlaceholder = () => {
114 return this.props.defaultView
115 ? this.props.defaultView
116 : <DataTableDefaultView />;
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}>
125 countNodes(filters) > 0
126 ? <DataTableFiltersPopover
127 name={`${name} filters`}
128 mutuallyExclusive={column.mutuallyExclusiveFilters}
131 onFiltersChange(filters, column)}
134 </DataTableFiltersPopover>
137 active={sortDirection !== SortDirection.NONE}
138 direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
139 IconComponent={this.ArrowIcon}
143 onSortToggle(column)}>
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)} />
158 renderBodyRow = (item: any, index: number) => {
159 const { onRowClick, onRowDoubleClick, extractKey, classes, currentItemUuid, currentRoute } = this.props;
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)}
175 mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
176 return this.props.columns.filter(column => column.selected).map(fn);
179 handleRowContextMenu = (item: T) =>
180 (event: React.MouseEvent<HTMLElement>) =>
181 this.props.onContextMenu(event, item)