X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/4bbaf2a0cede89ea50d63d210c6631adc1970620..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 9aeb28a7a5..08f52d00d3 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,189 +3,143 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { DataTable, DataTableProps, DataColumn, ColumnSelector } from "../../components/data-table";
-import { Typography, Grid, ListItem, Divider, List, ListItemIcon, ListItemText, Paper, Toolbar } from '@material-ui/core';
-import IconButton, { IconButtonProps } from '@material-ui/core/IconButton';
+import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, WithStyles, TablePagination, IconButton, Tooltip } from '@material-ui/core';
 import MoreVertIcon from "@material-ui/icons/MoreVert";
-import Popover from '../popover/popover';
-import { formatFileSize, formatDate } from '../../common/formatters';
-import { DataItem } from './data-item';
+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";
+
+type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
+
+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
+    }
+});
+
+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;
+}
 
-interface DataExplorerProps {
-    items: DataItem[];
-    onItemClick: (item: DataItem) => void;
+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;
 }
 
-type DataExplorerState = Pick<DataTableProps<DataItem>, "columns">;
+type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
 
-class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState> {
-    state: DataExplorerState = {
-        columns: [
-            {
-                name: "Name",
-                selected: true,
-                render: item => this.renderName(item)
-            },
-            {
-                name: "Status",
-                selected: true,
-                render: item => renderStatus(item.status)
-            },
-            {
-                name: "Type",
-                selected: true,
-                render: item => renderType(item.type)
-            },
-            {
-                name: "Owner",
-                selected: true,
-                render: item => renderOwner(item.owner)
-            },
-            {
-                name: "File size",
-                selected: true,
-                render: (item) => renderFileSize(item.fileSize)
-            },
-            {
-                name: "Last modified",
-                selected: true,
-                render: item => renderDate(item.lastModified)
-            },
-            {
-                name: "Actions",
-                selected: true,
-                configurable: false,
-                renderHeader: () => null,
-                render: renderItemActions
+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() {
-        return <Paper>
-            <Toolbar>
-                <Grid container justify="flex-end">
-                    <ColumnSelector
-                        columns={this.state.columns}
-                        onColumnToggle={this.toggleColumn} />
-                </Grid>
-            </Toolbar>
-            <DataTable
-                columns={this.state.columns}
-                items={this.props.items} />
-            <Toolbar />
-        </Paper>;
-    }
-
-    toggleColumn = (column: DataColumn<DataItem>) => {
-        const index = this.state.columns.indexOf(column);
-        const columns = this.state.columns.slice(0);
-        columns.splice(index, 1, { ...column, selected: !column.selected });
-        this.setState({ columns });
-    }
-
-    renderName = (item: DataItem) =>
-        <Grid
-            container
-            alignItems="center"
-            wrap="nowrap"
-            spacing={16}
-            onClick={() => this.props.onItemClick(item)}>
-            <Grid item>
-                {renderIcon(item)}
-            </Grid>
-            <Grid item>
-                <Typography color="primary">
-                    {item.name}
-                </Typography>
+        }
+        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>;
+        }
+
+        changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
+            this.props.onChangePage(page);
+        }
+
+        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>
-        </Grid>
-
-}
 
-const renderIcon = (dataItem: DataItem) => {
-    switch (dataItem.type) {
-        case "arvados#group":
-            return <i className="fas fa-folder fa-lg" />;
-        case "arvados#groupList":
-            return <i className="fas fa-th fa-lg" />;
-        default:
-            return <i />;
+        contextMenuColumn: DataColumn<any> = {
+            name: "Actions",
+            selected: true,
+            configurable: false,
+            sortDirection: SortDirection.NONE,
+            filters: [],
+            key: "context-actions",
+            render: this.renderContextMenuTrigger
+        };
     }
-};
-
-const renderDate = (date: string) =>
-    <Typography noWrap>
-        {formatDate(date)}
-    </Typography>;
-
-const renderFileSize = (fileSize?: number) =>
-    <Typography noWrap>
-        {formatFileSize(fileSize)}
-    </Typography>;
-
-const renderOwner = (owner: string) =>
-    <Typography noWrap color="primary">
-        {owner}
-    </Typography>;
-
-const renderType = (type: string) =>
-    <Typography noWrap>
-        {type}
-    </Typography>;
-
-const renderStatus = (status?: string) =>
-    <Typography noWrap align="center">
-        {status || "-"}
-    </Typography>;
-
-const renderItemActions = () =>
-    <Grid container justify="flex-end">
-        <Popover triggerComponent={ItemActionsTrigger}>
-            <List dense>
-                {[{
-                    icon: "fas fa-users",
-                    label: "Share"
-                },
-                {
-                    icon: "fas fa-sign-out-alt",
-                    label: "Move to"
-                },
-                {
-                    icon: "fas fa-star",
-                    label: "Add to favourite"
-                },
-                {
-                    icon: "fas fa-edit",
-                    label: "Rename"
-                },
-                {
-                    icon: "fas fa-copy",
-                    label: "Make a copy"
-                },
-                {
-                    icon: "fas fa-download",
-                    label: "Download"
-                }].map(renderAction)}
-                < Divider />
-                {renderAction({ icon: "fas fa-trash-alt", label: "Remove" })}
-            </List>
-        </Popover>
-    </Grid>;
-
-const renderAction = (action: { label: string, icon: string }, index?: number) =>
-    <ListItem button key={index}>
-        <ListItemIcon>
-            <i className={action.icon} />
-        </ListItemIcon>
-        <ListItemText>
-            {action.label}
-        </ListItemText>
-    </ListItem>;
-
-const ItemActionsTrigger: React.SFC<IconButtonProps> = (props) =>
-    <IconButton {...props}>
-        <MoreVertIcon />
-    </IconButton>;
-
-export default DataExplorer;
+);
