X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/bca77b4dbbc90bc60de06bd90a20e9a46c02fcfa..994434ce0881e5e4f309982790352e9af66cdb05:/src/components/data-explorer/data-explorer.tsx

diff --git a/src/components/data-explorer/data-explorer.tsx b/src/components/data-explorer/data-explorer.tsx
index 4fc9fb9c39..08f52d00d3 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,119 +3,143 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { Grid, Paper, Toolbar } from '@material-ui/core';
-import ContextMenu, { ContextMenuActionGroup } from "../../components/context-menu/context-menu";
-import ColumnSelector from "../../components/column-selector/column-selector";
-import DataTable from "../../components/data-table/data-table";
-import { mockAnchorFromMouseEvent } from "../../components/popover/helpers";
-import { DataColumn, toggleSortDirection } from "../../components/data-table/data-column";
-import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
-import { DataExplorerColumn } from './data-explorer-column';
+import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, WithStyles, TablePagination, IconButton, Tooltip } from '@material-ui/core';
+import MoreVertIcon from "@material-ui/icons/MoreVert";
+import { ColumnSelector } from "../column-selector/column-selector";
+import { DataTable, DataColumns } from "../data-table/data-table";
+import { DataColumn, SortDirection } from "../data-table/data-column";
+import { DataTableFilterItem } from '../data-table-filters/data-table-filters';
+import { SearchInput } from '../search-input/search-input';
+import { ArvadosTheme } from "~/common/custom-theme";
 
-interface DataExplorerProps<T> {
-    items: T[];
-    columns: Array<DataExplorerColumn<T>>;
-    contextActions: Array<ContextMenuActionGroup<T>>;
-    onRowClick: (item: T) => void;
-    onColumnToggle: (column: DataExplorerColumn<T>) => void;
-    onSortingToggle: (column: DataExplorerColumn<T>) => void;
-    onFiltersChange: (columns: DataExplorerColumn<T>) => void;
-}
-
-interface DataExplorerState<T> {
-    contextMenu: {
-        anchorEl?: HTMLElement;
-        item?: T;
-    };
-}
-
-class DataExplorer<T> extends React.Component<DataExplorerProps<T>, DataExplorerState<T>> {
-    state: DataExplorerState<T> = {
-        contextMenu: {}
-    };
+type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
 
-    render() {
-        return <Paper>
-            <ContextMenu
-                {...this.state.contextMenu}
-                actions={this.contextActions}
-                onClose={this.closeContextMenu} />
-            <Toolbar>
-                <Grid container justify="flex-end">
-                    <ColumnSelector
-                        columns={this.columns}
-                        onColumnToggle={this.toggleColumn} />
-                </Grid>
-            </Toolbar>
-            <DataTable
-                columns={this.columns}
-                items={this.props.items}
-                onRowClick={(_, row: T) => this.props.onRowClick(row)}
-                onRowContextMenu={this.openItemMenuOnRowClick} />
-            <Toolbar />
-        </Paper>;
+const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
+    searchBox: {
+        paddingBottom: theme.spacing.unit * 2
+    },
+    toolbar: {
+        paddingTop: theme.spacing.unit * 2
+    },
+    footer: {
+        overflow: 'auto'
+    },
+    root: {
+        height: '100%'
+    },
+    moreOptionsButton: {
+        padding: 0
     }
+});
 
-    get columns(): Array<DataColumn<T>> {
-        return this.props.columns.map((column): DataColumn<T> => ({
-            configurable: column.configurable,
-            filters: column.filters,
-            name: column.name,
-            onFiltersChange: column.filterable ? this.changeFilters(column) : undefined,
-            onSortToggle: column.sortable ? this.toggleSort(column) : undefined,
-            render: column.render,
-            renderHeader: column.renderHeader,
-            selected: column.selected,
-            sortDirection: column.sortDirection
-        }));
-    }
-
-    get contextActions() {
-        return this.props.contextActions.map(actionGroup =>
-            actionGroup.map(action => ({
-                ...action,
-                onClick: (item: T) => {
-                    this.closeContextMenu();
-                    action.onClick(item);
-                }
-            })));
-    }
-
-    toggleColumn = (column: DataExplorerColumn<T>) => {
-        this.props.onColumnToggle(column);
-    }
+interface DataExplorerDataProps<T> {
+    items: T[];
+    itemsAvailable: number;
+    columns: DataColumns<T>;
+    searchValue: string;
+    rowsPerPage: number;
+    rowsPerPageOptions: number[];
+    page: number;
+    contextMenuColumn: boolean;
+    dataTableDefaultView?: React.ReactNode;
+    working?: boolean;
+}
 
-    toggleSort = (column: DataExplorerColumn<T>) => () => {
-        this.props.onSortingToggle(toggleSortDirection(column));
-    }
+interface DataExplorerActionProps<T> {
+    onSetColumns: (columns: DataColumns<T>) => void;
+    onSearch: (value: string) => void;
+    onRowClick: (item: T) => void;
+    onRowDoubleClick: (item: T) => void;
+    onColumnToggle: (column: DataColumn<T>) => void;
+    onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
+    onSortToggle: (column: DataColumn<T>) => void;
+    onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
+    onChangePage: (page: number) => void;
+    onChangeRowsPerPage: (rowsPerPage: number) => void;
+    extractKey?: (item: T) => React.Key;
+}
 
-    changeFilters = (column: DataExplorerColumn<T>) => (filters: DataTableFilterItem[]) => {
-        this.props.onFiltersChange({ ...column, filters });
-    }
+type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
 
-    openItemMenuOnRowClick = (event: React.MouseEvent<HTMLElement>, item: T) => {
-        event.preventDefault();
-        this.setState({
-            contextMenu: {
-                anchorEl: mockAnchorFromMouseEvent(event),
-                item
+export const DataExplorer = withStyles(styles)(
+    class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
+        componentDidMount() {
+            if (this.props.onSetColumns) {
+                this.props.onSetColumns(this.props.columns);
             }
-        });
-    }
+        }
+        render() {
+            const {
+                columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
+                rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
+                items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
+                dataTableDefaultView
+            } = this.props;
+            return <Paper className={classes.root}>
+                <Toolbar className={classes.toolbar}>
+                    <Grid container justify="space-between" wrap="nowrap" alignItems="center">
+                        <div className={classes.searchBox}>
+                            <SearchInput
+                                value={searchValue}
+                                onSearch={onSearch} />
+                        </div>
+                        <ColumnSelector
+                            columns={columns}
+                            onColumnToggle={onColumnToggle} />
+                    </Grid>
+                </Toolbar>
+                <DataTable
+                    columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
+                    items={items}
+                    onRowClick={(_, item: T) => onRowClick(item)}
+                    onContextMenu={onContextMenu}
+                    onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
+                    onFiltersChange={onFiltersChange}
+                    onSortToggle={onSortToggle}
+                    extractKey={extractKey}
+                    working={working}
+                    defaultView={dataTableDefaultView}
+                />
+                <Toolbar className={classes.footer}>
+                    <Grid container justify="flex-end">
+                        <TablePagination
+                            count={itemsAvailable}
+                            rowsPerPage={rowsPerPage}
+                            rowsPerPageOptions={rowsPerPageOptions}
+                            page={this.props.page}
+                            onChangePage={this.changePage}
+                            onChangeRowsPerPage={this.changeRowsPerPage}
+                            component="div" />
+                    </Grid>
+                </Toolbar>
+            </Paper>;
+        }
 
-    openItemMenuOnActionsClick = (event: React.MouseEvent<HTMLElement>, item: T) => {
-        this.setState({
-            contextMenu: {
-                anchorEl: event.currentTarget,
-                item
-            }
-        });
-    }
+        changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
+            this.props.onChangePage(page);
+        }
 
-    closeContextMenu = () => {
-        this.setState({ contextMenu: {} });
-    }
+        changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
+            this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
+        }
 
-}
+        renderContextMenuTrigger = (item: T) =>
+            <Grid container justify="center">
+                <Tooltip title="More options" disableFocusListener>
+                    <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
+                        <MoreVertIcon />
+                    </IconButton>
+                </Tooltip>
+            </Grid>
 
-export default DataExplorer;
+        contextMenuColumn: DataColumn<any> = {
+            name: "Actions",
+            selected: true,
+            configurable: false,
+            sortDirection: SortDirection.NONE,
+            filters: [],
+            key: "context-actions",
+            render: this.renderContextMenuTrigger
+        };
+    }
+);
