X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/fbd2012fa56f6de44dfc4008d70e5a21cfcaf459..25216cc7acedc987c26a159f0b640210c0ef101e:/src/components/data-table/data-table.tsx

diff --git a/src/components/data-table/data-table.tsx b/src/components/data-table/data-table.tsx
index 0367dfa9..589dc0d3 100644
--- a/src/components/data-table/data-table.tsx
+++ b/src/components/data-table/data-table.tsx
@@ -3,63 +3,172 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { Table, TableBody, TableRow, TableCell, TableHead, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
-import { DataColumn } from './data-column';
+import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles, IconButton } from '@material-ui/core';
+import classnames from 'classnames';
+import { DataColumn, SortDirection } from './data-column';
+import { DataTableDefaultView } from '../data-table-default-view/data-table-default-view';
+import { DataTableFilters } from '../data-table-filters/data-table-filters-tree';
+import { DataTableFiltersPopover } from '../data-table-filters/data-table-filters-popover';
+import { countNodes } from 'models/tree';
+import { SvgIconProps } from '@material-ui/core/SvgIcon';
+import ArrowDownwardIcon from '@material-ui/icons/ArrowDownward';
 
-export interface DataTableProps<T> {
-    items: T[];
-    columns: Array<DataColumn<T>>;
-    onItemClick?: (item: T) => void;
+export type DataColumns<T> = Array<DataColumn<T>>;
+
+export enum DataTableFetchMode {
+    PAGINATED,
+    INFINITE
 }
 
-class DataTable<T> extends React.Component<DataTableProps<T> & WithStyles<CssRules>> {
-    render() {
-        const { items, columns, classes, onItemClick } = this.props;
-        return <div className={classes.tableContainer}>
-            {items.length > 0 ?
-                <Table>
-                    <TableHead>
-                        <TableRow>
-                            {columns
-                                .filter(column => column.selected)
-                                .map(({ name, renderHeader, key }, index) =>
-                                    <TableCell key={key || index}>
-                                        {renderHeader ? renderHeader() : name}
-                                    </TableCell>
-                                )}
-                        </TableRow>
-                    </TableHead>
-                    <TableBody className={classes.tableBody}>
-                        {items
-                            .map((item, index) =>
-                                <TableRow
-                                    hover
-                                    key={index}
-                                    onClick={() => onItemClick && onItemClick(item)}>
-                                    {columns
-                                        .filter(column => column.selected)
-                                        .map((column, index) => (
-                                            <TableCell key={column.key || index}>
-                                                {column.render(item)}
-                                            </TableCell>
-                                        ))}
-                                </TableRow>
-                            )}
-                    </TableBody>
-                </Table> : <Typography>No items</Typography>}
-        </div>;
-    }
+export interface DataTableDataProps<T> {
+    items: T[];
+    columns: DataColumns<T>;
+    onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
+    onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
+    onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
+    onSortToggle: (column: DataColumn<T>) => void;
+    onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
+    extractKey?: (item: T) => React.Key;
+    working?: boolean;
+    defaultView?: React.ReactNode;
+    currentItemUuid?: string;
+    currentRoute?: string;
 }
 
-type CssRules = "tableBody" | "tableContainer";
+type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell' | 'arrow' | 'arrowButton' | 'tableCellWorkflows';
 
 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
-    tableContainer: {
-        overflowX: 'auto'
+    root: {
+        overflowX: 'auto',
+        overflowY: 'auto',
+        height: 'calc(100vh - 280px)',
+    },
+    content: {
+        display: 'inline-block',
+        width: '100%'
     },
     tableBody: {
         background: theme.palette.background.paper
+    },
+    noItemsInfo: {
+        textAlign: "center",
+        padding: theme.spacing.unit
+    },
+    tableCell: {
+        wordWrap: 'break-word',
+        paddingRight: '24px'
+    },
+    tableCellWorkflows: {
+        '&:nth-last-child(2)': {
+            padding: '0px',
+            maxWidth: '48px'
+        },
+        '&:last-child': {
+            padding: '0px',
+            paddingRight: '24px',
+            width: '48px'
+        }
+    },
+    arrow: {
+        margin: 0
+    },
+    arrowButton: {
+        color: theme.palette.text.primary
     }
 });
 
-export default withStyles(styles)(DataTable);
+type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
+
+export const DataTable = withStyles(styles)(
+    class Component<T> extends React.Component<DataTableProps<T>> {
+        render() {
+            const { items, classes } = this.props;
+            return <div className={classes.root}>
+                <div className={classes.content}>
+                    <Table>
+                        <TableHead>
+                            <TableRow>
+                                {this.mapVisibleColumns(this.renderHeadCell)}
+                            </TableRow>
+                        </TableHead>
+                        <TableBody className={classes.tableBody}>
+                            {items.map(this.renderBodyRow)}
+                        </TableBody>
+                    </Table>
+                    {items.length === 0 && this.props.working !== undefined && !this.props.working && this.renderNoItemsPlaceholder()}
+                </div>
+            </div>;
+        }
+
+        renderNoItemsPlaceholder = () => {
+            return this.props.defaultView
+                ? this.props.defaultView
+                : <DataTableDefaultView />;
+        }
+
+        renderHeadCell = (column: DataColumn<T>, index: number) => {
+            const { name, key, renderHeader, filters, sortDirection } = column;
+            const { onSortToggle, onFiltersChange, classes } = this.props;
+            return <TableCell className={classes.tableCell} key={key || index}>
+                {renderHeader ?
+                    renderHeader() :
+                    countNodes(filters) > 0
+                        ? <DataTableFiltersPopover
+                            name={`${name} filters`}
+                            mutuallyExclusive={column.mutuallyExclusiveFilters}
+                            onChange={filters =>
+                                onFiltersChange &&
+                                onFiltersChange(filters, column)}
+                            filters={filters}>
+                            {name}
+                        </DataTableFiltersPopover>
+                        : sortDirection
+                            ? <TableSortLabel
+                                active={sortDirection !== SortDirection.NONE}
+                                direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
+                                IconComponent={this.ArrowIcon}
+                                hideSortIcon
+                                onClick={() =>
+                                    onSortToggle &&
+                                    onSortToggle(column)}>
+                                {name}
+                            </TableSortLabel>
+                            : <span>
+                                {name}
+                            </span>}
+            </TableCell>;
+        }
+
+        ArrowIcon = ({ className, ...props }: SvgIconProps) => (
+            <IconButton component='span' className={this.props.classes.arrowButton} tabIndex={-1}>
+                <ArrowDownwardIcon {...props} className={classnames(className, this.props.classes.arrow)} />
+            </IconButton>
+        )
+
+        renderBodyRow = (item: any, index: number) => {
+            const { onRowClick, onRowDoubleClick, extractKey, classes, currentItemUuid, currentRoute } = this.props;
+            return <TableRow
+                hover
+                key={extractKey ? extractKey(item) : index}
+                onClick={event => onRowClick && onRowClick(event, item)}
+                onContextMenu={this.handleRowContextMenu(item)}
+                onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}
+                selected={item === currentItemUuid}>
+                {this.mapVisibleColumns((column, index) => (
+                    <TableCell key={column.key || index} className={currentRoute === '/workflows' ? classes.tableCellWorkflows : classes.tableCell}>
+                        {column.render(item)}
+                    </TableCell>
+                ))}
+            </TableRow>;
+        }
+
+        mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
+            return this.props.columns.filter(column => column.selected).map(fn);
+        }
+
+        handleRowContextMenu = (item: T) =>
+            (event: React.MouseEvent<HTMLElement>) =>
+                this.props.onContextMenu(event, item)
+
+    }
+);
