X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/c90e813adcec89899d9db95843295a84fb058c3e..6aaf65540506d590fa826b08166c050b924a99ff:/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 e86113ef..25d81c62 100644
--- a/src/components/data-table/data-table.tsx
+++ b/src/components/data-table/data-table.tsx
@@ -3,100 +3,36 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
+import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles } from '@material-ui/core';
 import { DataColumn, SortDirection } from './data-column';
-import DataTableFilters, { DataTableFilterItem } from "../data-table-filters/data-table-filters";
+import { DataTableFilters, DataTableFilterItem } from "../data-table-filters/data-table-filters";
+import { DataTableDefaultView } from '../data-table-default-view/data-table-default-view';
 
-export type DataColumns<T> = Array<DataColumn<T>>;
+export type DataColumns<T, F extends DataTableFilterItem = DataTableFilterItem> = Array<DataColumn<T, F>>;
 
-export interface DataTableProps<T> {
+export interface DataTableDataProps<T> {
     items: T[];
     columns: DataColumns<T>;
     onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
-    onRowContextMenu: (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: DataTableFilterItem[], column: DataColumn<T>) => void;
+    extractKey?: (item: T) => React.Key;
+    working?: boolean;
+    defaultView?: React.ReactNode;
 }
 
-class DataTable<T> extends React.Component<DataTableProps<T> & WithStyles<CssRules>> {
-    render() {
-        const { items, classes } = this.props;
-        return <div className={classes.tableContainer}>
-            {items.length > 0 ?
-                <Table>
-                    <TableHead>
-                        <TableRow>
-                            {this.mapVisibleColumns(this.renderHeadCell)}
-                        </TableRow>
-                    </TableHead>
-                    <TableBody className={classes.tableBody}>
-                        {items.map(this.renderBodyRow)}
-                    </TableBody>
-                </Table> : <Typography
-                    className={classes.noItemsInfo}
-                    variant="body2"
-                    gutterBottom>
-                    No items
-                </Typography>}
-        </div>;
-    }
-
-    renderHeadCell = (column: DataColumn<T>, index: number) => {
-        const { name, key, renderHeader, filters, sortDirection } = column;
-        const { onSortToggle, onFiltersChange } = this.props;
-        return <TableCell key={key || index}>
-            {renderHeader ?
-                renderHeader() :
-                filters
-                    ? <DataTableFilters
-                        name={`${name} filters`}
-                        onChange={filters =>
-                            onFiltersChange &&
-                            onFiltersChange(filters, column)}
-                        filters={filters}>
-                        {name}
-                    </DataTableFilters>
-                    : sortDirection
-                        ? <TableSortLabel
-                            active={sortDirection !== "none"}
-                            direction={sortDirection !== "none" ? sortDirection : undefined}
-                            onClick={() =>
-                                onSortToggle &&
-                                onSortToggle(column)}>
-                            {name}
-                        </TableSortLabel>
-                        : <span>
-                            {name}
-                        </span>}
-        </TableCell>;
-    }
-
-    renderBodyRow = (item: T, index: number) => {
-        const { columns, onRowClick, onRowContextMenu } = this.props;
-        return <TableRow
-            hover
-            key={index}
-            onClick={event => onRowClick && onRowClick(event, item)}
-            onContextMenu={event => onRowContextMenu && onRowContextMenu(event, item)}>
-            {this.mapVisibleColumns((column, index) => (
-                <TableCell key={column.key || index}>
-                    {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);
-    }
-
-}
-
-type CssRules = "tableBody" | "tableContainer" | "noItemsInfo";
+type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell';
 
 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
-    tableContainer: {
-        overflowX: 'auto'
+    root: {
+        overflowX: 'auto',
+        overflowY: 'hidden'
+    },
+    content: {
+        display: 'inline-block',
+        width: '100%'
     },
     tableBody: {
         background: theme.palette.background.paper
@@ -104,7 +40,94 @@ const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     noItemsInfo: {
         textAlign: "center",
         padding: theme.spacing.unit
+    },
+    tableCell: {
+        wordWrap: 'break-word'
     }
 });
 
-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 } = this.props;
+            return <TableCell key={key || index}>
+                {renderHeader ?
+                    renderHeader() :
+                    filters.length > 0
+                        ? <DataTableFilters
+                            name={`${name} filters`}
+                            onChange={filters =>
+                                onFiltersChange &&
+                                onFiltersChange(filters, column)}
+                            filters={filters}>
+                            {name}
+                        </DataTableFilters>
+                        : sortDirection
+                            ? <TableSortLabel
+                                active={sortDirection !== SortDirection.NONE}
+                                direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
+                                onClick={() =>
+                                    onSortToggle &&
+                                    onSortToggle(column)}>
+                                {name}
+                            </TableSortLabel>
+                            : <span>
+                                {name}
+                            </span>}
+            </TableCell>;
+        }
+
+        renderBodyRow = (item: T, index: number) => {
+            const { onRowClick, onRowDoubleClick, extractKey, classes } = 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)}>
+                {this.mapVisibleColumns((column, index) => (
+                    <TableCell key={column.key || index} className={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)
+
+    }
+);
