X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/e806c7001a4be0bb4b5a27995d89bd560d5fe8db..278f75bd896ee35887282f61e10bbeaf6471011d:/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 ada0159788..9aacadf342 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,224 +3,146 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { DataTable, DataTableProps, Column, ColumnsConfigurator } from "../../components/data-table";
-import { Typography, Grid, ListItem, Divider, List, ListItemIcon, ListItemText } from '@material-ui/core';
-import IconButton, { IconButtonProps } from '@material-ui/core/IconButton';
+import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, Theme, WithStyles, TablePagination, IconButton } from '@material-ui/core';
 import MoreVertIcon from "@material-ui/icons/MoreVert";
-import Popover from '../popover/popover';
-import { formatFileSize, formatDate } from './formatters';
-import { DataItem } from './data-item';
+import ContextMenu, { ContextMenuActionGroup, ContextMenuAction } 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 } from "../../components/data-table/data-column";
+import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
+import SearchInput from '../search-input/search-input';
+
+interface DataExplorerProps<T> {
+    items: T[];
+    columns: Array<DataColumn<T>>;
+    contextActions: ContextMenuActionGroup[];
+    searchValue: string;
+    rowsPerPage: number;
+    page: number;
+    onSearch: (value: string) => void;
+    onRowClick: (item: T) => void;
+    onColumnToggle: (column: DataColumn<T>) => void;
+    onContextAction: (action: ContextMenuAction, item: T) => void;
+    onSortToggle: (column: DataColumn<T>) => void;
+    onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
+    onChangePage: (page: number) => void;
+    onChangeRowsPerPage: (rowsPerPage: number) => void;
+}
 
-interface DataExplorerProps {
-    items: DataItem[];
-    onItemClick: (item: DataItem) => void;
+interface DataExplorerState<T> {
+    contextMenu: {
+        anchorEl?: HTMLElement;
+        item?: T;
+    };
 }
 
-type DataExplorerState = Pick<DataTableProps<DataItem>, "columns">;
-
-class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState> {
-
-    state: DataExplorerState = {
-        columns: [
-            {
-                header: "Name",
-                selected: true,
-                render: item => this.renderName(item)
-            },
-            {
-                header: "Status",
-                selected: true,
-                render: item => renderStatus(item.status)
-            },
-            {
-                header: "Type",
-                selected: true,
-                render: item => renderType(item.type)
-            },
-            {
-                header: "Owner",
-                selected: true,
-                render: item => renderOwner(item.owner)
-            },
-            {
-                header: "File size",
-                selected: true,
-                render: (item) => renderFileSize(item.fileSize)
-            },
-            {
-                header: "Last modified",
-                selected: true,
-                render: item => renderDate(item.lastModified)
-            },
-            {
-                header: "Actions",
-                key: "Actions",
-                selected: true,
-                configurable: false,
-                renderHeader: () => this.renderActionsHeader(),
-                render: renderItemActions
-            }
-        ]
+class DataExplorer<T> extends React.Component<DataExplorerProps<T> & WithStyles<CssRules>, DataExplorerState<T>> {
+    state: DataExplorerState<T> = {
+        contextMenu: {}
     };
 
     render() {
-        return (
+        return <Paper>
+            <ContextMenu
+                anchorEl={this.state.contextMenu.anchorEl}
+                actions={this.props.contextActions}
+                onActionClick={this.callAction}
+                onClose={this.closeContextMenu} />
+            <Toolbar className={this.props.classes.toolbar}>
+                <Grid container justify="space-between" wrap="nowrap" alignItems="center">
+                    <div className={this.props.classes.searchBox}>
+                        {this.props.items.length > 0 && <SearchInput
+                            value={this.props.searchValue}
+                            onSearch={this.props.onSearch} />}
+                    </div>
+                    <ColumnSelector
+                        columns={this.props.columns}
+                        onColumnToggle={this.props.onColumnToggle} />
+                </Grid>
+            </Toolbar>
             <DataTable
-                columns={this.state.columns}
+                columns={[
+                    ...this.props.columns,
+                    this.contextMenuColumn]}
                 items={this.props.items}
-            />
-        );
+                onRowClick={(_, item: T) => this.props.onRowClick(item)}
+                onRowContextMenu={this.openContextMenu}
+                onFiltersChange={this.props.onFiltersChange}
+                onSortToggle={this.props.onSortToggle} />
+            <Toolbar>
+                {this.props.items.length > 0 &&
+                    <Grid container justify="flex-end">
+                        <TablePagination
+                            count={this.props.items.length}
+                            rowsPerPage={this.props.rowsPerPage}
+                            page={this.props.page}
+                            onChangePage={this.changePage}
+                            onChangeRowsPerPage={this.changeRowsPerPage}
+                            component="div"
+                        />
+                    </Grid>}
+            </Toolbar>
+        </Paper>;
     }
 
-    toggleColumn = (column: Column<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 });
+    openContextMenu = (event: React.MouseEvent<HTMLElement>, item: T) => {
+        event.preventDefault();
+        this.setState({
+            contextMenu: {
+                anchorEl: mockAnchorFromMouseEvent(event),
+                item
+            }
+        });
     }
 
-    renderActionsHeader = () => {
-        return (
-            <Grid container justify="flex-end">
-                <ColumnsConfigurator
-                    columns={this.state.columns}
-                    onColumnToggle={this.toggleColumn}
-                />
-            </Grid>
-        );
+    closeContextMenu = () => {
+        this.setState({ contextMenu: {} });
     }
 
-    renderName = (item: DataItem) => {
-        return (
-            (
-                <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>
-                    </Grid>
-                </Grid>
-            )
-        );
+    callAction = (action: ContextMenuAction) => {
+        const { item } = this.state.contextMenu;
+        this.closeContextMenu();
+        if (item) {
+            this.props.onContextAction(action, item);
+        }
+    }
+
+    changePage = (event: React.MouseEvent<HTMLButtonElement> | null, page: number) => {
+        this.props.onChangePage(page);
     }
-}
 
-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 />;
+    changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
+        this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
     }
-};
-
-const renderDate = (date: string) => {
-    return (
-        <Typography noWrap>
-            {formatDate(date)}
-        </Typography>
-    );
-};
-
-const renderFileSize = (fileSize?: number) => {
-    return (
-        <Typography noWrap>
-            {typeof fileSize === "number" ? formatFileSize(fileSize) : "-"}
-        </Typography>
-    );
-};
-
-const renderOwner = (owner: string) => {
-    return (
-        <Typography noWrap color="primary">
-            {owner}
-        </Typography>
-    );
-};
-
-const renderType = (type: string) => {
-    return (
-        <Typography noWrap>
-            {type}
-        </Typography>
-    );
-};
-
-const renderStatus = (status?: string) => {
-    return (
-        <Typography noWrap align="center">
-            {status || "-"}
-        </Typography>
-    );
-};
-
-const renderItemActions = () => {
-    return (
+
+    renderContextMenuTrigger = (item: T) =>
         <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>
+            <IconButton onClick={event => this.openContextMenu(event, item)}>
+                <MoreVertIcon />
+            </IconButton>
         </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;
+
+    contextMenuColumn = {
+        name: "Actions",
+        selected: true,
+        key: "context-actions",
+        renderHeader: () => null,
+        render: this.renderContextMenuTrigger,
+        width: "auto"
+    };
+
+}
+
+type CssRules = "searchBox" | "toolbar";
+
+const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
+    searchBox: {
+        paddingBottom: theme.spacing.unit * 2
+    },
+    toolbar: {
+        paddingTop: theme.spacing.unit * 2
+    }
+});
+
+export default withStyles(styles)(DataExplorer);
