X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/e25469d58a038391fa16184da4f7078a0eae805a..e7c978d1f6fa309e8462fc952f2735c39a4040bb:/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 f2fe3be27e..09a327268b 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,217 +3,158 @@
 // 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';
-
-export interface DataItem {
-    name: string;
-    type: string;
-    owner: string;
-    lastModified: string;
-    fileSize?: number;
-    status?: string;
+import ContextMenu, { ContextMenuActionGroup, ContextMenuAction } from "../../components/context-menu/context-menu";
+import ColumnSelector from "../../components/column-selector/column-selector";
+import DataTable, { DataColumns, DataItem } 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[];
+    itemsAvailable: number;
+    columns: DataColumns<T>;
+    contextActions: ContextMenuActionGroup[];
+    searchValue: string;
+    rowsPerPage: number;
+    rowsPerPageOptions?: 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 => (
-                    <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>
-                )
-            },
-            {
-                header: "Status",
-                selected: true,
-                render: item => (
-                    <Typography noWrap align="center">
-                        {item.status || "-"}
-                    </Typography>
-                )
-            },
-            {
-                header: "Type",
-                selected: true,
-                render: item => (
-                    <Typography noWrap>
-                        {item.type}
-                    </Typography>
-                )
-            },
-            {
-                header: "Owner",
-                selected: true,
-                render: item => (
-                    <Typography noWrap color="primary">
-                        {item.owner}
-                    </Typography>
-                )
-            },
-            {
-                header: "File size",
-                selected: true,
-                render: ({ fileSize }) => (
-                    <Typography noWrap>
-                        {typeof fileSize === "number" ? formatFileSize(fileSize) : "-"}
-                    </Typography>
-                )
-            },
-            {
-                header: "Last modified",
-                selected: true,
-                render: item => (
-                    <Typography noWrap>
-                        {formatDate(item.lastModified)}
-                    </Typography>
-                )
-            },
-            {
-                header: "Actions",
-                key: "Actions",
-                selected: true,
-                configurable: false,
-                renderHeader: () => (
-                    <Grid container justify="flex-end">
-                        <ColumnsConfigurator
-                            columns={this.state.columns}
-                            onColumnToggle={this.toggleColumn}
-                        />
-                    </Grid>
-                ),
-                render: item => (
-                    <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>
-                )
-            }
-        ]
+class DataExplorer<T extends DataItem> 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}>
+                        <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.itemsAvailable}
+                            rowsPerPage={this.props.rowsPerPage}
+                            rowsPerPageOptions={this.props.rowsPerPageOptions}
+                            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();
+        event.stopPropagation();
+        this.setState({
+            contextMenu: {
+                anchorEl: mockAnchorFromMouseEvent(event),
+                item
+            }
+        });
     }
-}
 
-const formatDate = (isoDate: string) => {
-    const date = new Date(isoDate);
-    return date.toLocaleString();
-};
-
-const formatFileSize = (size: number) => {
-    switch (true) {
-        case size > 1000000000000:
-            return `${size / 1000000000000} TB`;
-        case size > 1000000000:
-            return `${size / 1000000000} GB`;
-        case size > 1000000:
-            return `${size / 1000000} MB`;
-        case size > 1000:
-            return `${size / 1000} KB`;
-        default:
-            return `${size} B`;
+    closeContextMenu = () => {
+        this.setState({ contextMenu: {} });
     }
-};
-
-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 />;
+
+    callAction = (action: ContextMenuAction) => {
+        const { item } = this.state.contextMenu;
+        this.closeContextMenu();
+        if (item) {
+            this.props.onContextAction(action, item);
+        }
     }
-};
-
-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;
+
+    changePage = (event: React.MouseEvent<HTMLButtonElement> | null, 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="flex-end">
+            <IconButton onClick={event => this.openContextMenuTrigger(event, item)}>
+                <MoreVertIcon />
+            </IconButton>
+        </Grid>
+
+    openContextMenuTrigger = (event: React.MouseEvent<HTMLElement>, item: T) => {
+        event.preventDefault();
+        this.setState({
+            contextMenu: {
+                anchorEl: event.currentTarget,
+                item
+            }
+        });
+    }
+
+    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);
