X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/7364c06cd354952de2d469aa6285b7624864acf7..c918b794f369e2d38f85e2c2ca7bb11fc69555fe:/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 026820be..9cc1cc95 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,223 +3,157 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { DataTable, DataColumn, ColumnSelector } from "../../components/data-table";
-import { Typography, Grid, Paper, Toolbar } from '@material-ui/core';
-import IconButton from '@material-ui/core/IconButton';
+import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, Theme, WithStyles, TablePagination, Table, IconButton } from '@material-ui/core';
 import MoreVertIcon from "@material-ui/icons/MoreVert";
-import { formatFileSize, formatDate } from '../../common/formatters';
-import { DataItem } from './data-item';
-import { mockAnchorFromMouseEvent } from '../popover/helpers';
-import ContextMenu, { ContextMenuActionGroup } from '../context-menu/context-menu';
-
-export interface DataExplorerContextActions {
-    onAddToFavourite: (dataIitem: DataItem) => void;
-    onCopy: (dataIitem: DataItem) => void;
-    onDownload: (dataIitem: DataItem) => void;
-    onMoveTo: (dataIitem: DataItem) => void;
-    onRemove: (dataIitem: DataItem) => void;
-    onRename: (dataIitem: DataItem) => void;
-    onShare: (dataIitem: DataItem) => void;
-}
-interface DataExplorerProps {
-    items: DataItem[];
-    onItemClick: (item: DataItem) => void;
-    contextActions: DataExplorerContextActions;
+import ContextMenu, { ContextMenuActionGroup, ContextMenuAction } from "../../components/context-menu/context-menu";
+import ColumnSelector from "../../components/column-selector/column-selector";
+import DataTable, { DataColumns } 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: DataColumns<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 DataExplorerState {
-    columns: Array<DataColumn<DataItem>>;
+interface DataExplorerState<T> {
     contextMenu: {
         anchorEl?: HTMLElement;
-        item?: DataItem;
-        actions: Array<ContextMenuActionGroup<DataItem>>;
+        item?: T;
     };
 }
 
-class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState> {
-    state: DataExplorerState = {
-        contextMenu: {
-            actions: [[{
-                icon: "fas fa-users fa-fw",
-                name: "Share",
-                onClick: this.handleContextAction("onShare")
-            }, {
-                icon: "fas fa-sign-out-alt fa-fw",
-                name: "Move to",
-                onClick: this.handleContextAction("onMoveTo")
-            }, {
-                icon: "fas fa-star fa-fw",
-                name: "Add to favourite",
-                onClick: this.handleContextAction("onAddToFavourite")
-            }, {
-                icon: "fas fa-edit fa-fw",
-                name: "Rename",
-                onClick: this.handleContextAction("onRename")
-            }, {
-                icon: "fas fa-copy fa-fw",
-                name: "Make a copy",
-                onClick: this.handleContextAction("onCopy")
-            }, {
-                icon: "fas fa-download fa-fw",
-                name: "Download",
-                onClick: this.handleContextAction("onDownload")
-            }], [{
-                icon: "fas fa-trash-alt fa-fw",
-                name: "Remove",
-                onClick: this.handleContextAction("onRemove")
-            }
-            ]]
-        },
-        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: item => this.renderActions(item)
-        }]
+class DataExplorer<T> extends React.Component<DataExplorerProps<T> & WithStyles<CssRules>, DataExplorerState<T>> {
+    state: DataExplorerState<T> = {
+        contextMenu: {}
     };
 
     render() {
         return <Paper>
             <ContextMenu
-                {...this.state.contextMenu}
+                anchorEl={this.state.contextMenu.anchorEl}
+                actions={this.props.contextActions}
+                onActionClick={this.callAction}
                 onClose={this.closeContextMenu} />
-            <Toolbar>
-                <Grid container justify="flex-end">
-                    <ColumnSelector
-                        columns={this.state.columns}
-                        onColumnToggle={this.toggleColumn} />
-                </Grid>
+            <Toolbar className={this.props.classes.toolbar}>
+                {this.props.items.length > 0 &&
+                    <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}
-                onRowContextMenu={this.openItemMenuOnRowClick} />
-            <Toolbar />
+                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: 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 });
+    openContextMenu = (event: React.MouseEvent<HTMLElement>, item: T) => {
+        event.preventDefault();
+        this.setState({
+            contextMenu: {
+                anchorEl: mockAnchorFromMouseEvent(event),
+                item
+            }
+        });
     }
 
-    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>
-            </Grid>
-        </Grid>
+    closeContextMenu = () => {
+        this.setState({ contextMenu: {} });
+    }
 
-    renderActions = (item: DataItem) =>
+    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);
+    }
+
+    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.openItemMenuOnActionsClick(event, item)}>
+            <IconButton onClick={event => this.openContextMenuWithTrigger(event, item)}>
                 <MoreVertIcon />
             </IconButton>
         </Grid>
 
-    openItemMenuOnRowClick = (event: React.MouseEvent<HTMLElement>, item: DataItem) => {
+    openContextMenuWithTrigger = (event: React.MouseEvent<HTMLElement>, item: T) => {
         event.preventDefault();
-        this.setContextMenuState({
-            anchorEl: mockAnchorFromMouseEvent(event),
-            item
+        this.setState({
+            contextMenu: {
+                anchorEl: event.currentTarget,
+                item
+            }
         });
     }
 
-    openItemMenuOnActionsClick = (event: React.MouseEvent<HTMLElement>, item: DataItem) => {
-        this.setContextMenuState({
-            anchorEl: event.currentTarget,
-            item
-        });
-    }
+    contextMenuColumn = {
+        name: "Actions",
+        selected: true,
+        key: "context-actions",
+        renderHeader: () => null,
+        render: this.renderContextMenuTrigger
+    };
 
-    closeContextMenu = () => {
-        this.setContextMenuState({});
-    }
+}
 
-    setContextMenuState = (contextMenu: { anchorEl?: HTMLElement; item?: DataItem; }) => {
-        this.setState(prev => ({ contextMenu: { ...contextMenu, actions: prev.contextMenu.actions } }));
-    }
+type CssRules = "searchBox" | "toolbar";
 
-    handleContextAction(action: keyof DataExplorerContextActions) {
-        return (item: DataItem) => {
-            this.closeContextMenu();
-            this.props.contextActions[action](item);
-        };
+const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
+    searchBox: {
+        paddingBottom: theme.spacing.unit * 2
+    },
+    toolbar: {
+        paddingTop: theme.spacing.unit * 2
     }
+});
 
-}
-
-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 />;
-    }
-};
-
-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>;
-
-export default DataExplorer;
+export default withStyles(styles)(DataExplorer);
