Move default views to corresponding panels
[arvados-workbench2.git] / src / components / data-explorer / data-explorer.tsx
index 40d6ae687836968d992fd00e530a40e72fd2d488..0147937f834d5f0bd7a7f3f5525a084e5f7c00c1 100644 (file)
 // 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, WithStyles, TablePagination, 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 { 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";
+import { IconType } from '../icon/icon';
+
+type CssRules = 'searchBox' | "toolbar" | 'defaultRoot' | 'defaultMessage' | 'defaultIcon';
+
+const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
+    searchBox: {
+        paddingBottom: theme.spacing.unit * 2
+    },
+    toolbar: {
+        paddingTop: theme.spacing.unit * 2
+    },
+    defaultRoot: {
+        position: 'absolute',
+        width: '80%',
+        left: '50%',
+        top: '50%',
+        transform: 'translate(-50%, -50%)'
+    },
+    defaultMessage: {
+        fontSize: '1.75rem',
+    },
+    defaultIcon: {
+        fontSize: '6rem'
+    }
+});
+
+interface DataExplorerDataProps<T> {
+    items: T[];
+    itemsAvailable: number;
+    columns: DataColumns<T>;
+    searchValue: string;
+    rowsPerPage: number;
+    rowsPerPageOptions: number[];
+    page: number;
+    contextMenuColumn: boolean;
+    noItemsPlaceholder?: React.ReactNode;
 }
 
-interface DataExplorerState {
-    columns: Array<DataColumn<DataItem>>;
-    contextMenu: {
-        anchorEl?: HTMLElement;
-        item?: DataItem;
-        actions: Array<ContextMenuActionGroup<DataItem>>;
-    };
+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;
 }
 
-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")
+type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
+
+export const DataExplorer = withStyles(styles)(
+    class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
+        componentDidMount() {
+            if (this.props.onSetColumns) {
+                this.props.onSetColumns(this.props.columns);
             }
-            ]]
-        },
-        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)
-        }, {
+        }
+        render() {
+            const {
+                columns, onContextMenu, onFiltersChange, onSortToggle, extractKey,
+                rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
+                items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
+                noItemsPlaceholder
+            } = this.props;
+            return <Paper>
+                <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}
+                    noItemsPlaceholder={noItemsPlaceholder}
+                />
+                <Toolbar>
+                    <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="flex-end">
+                <IconButton onClick={event => this.props.onContextMenu(event, item)}>
+                    <MoreVertIcon />
+                </IconButton>
+            </Grid>
+
+        contextMenuColumn: DataColumn<any> = {
             name: "Actions",
             selected: true,
             configurable: false,
-            renderHeader: () => null,
-            render: item => this.renderActions(item)
-        }]
-    };
-
-    render() {
-        return <Paper>
-            <ContextMenu
-                {...this.state.contextMenu}
-                onClose={this.closeContextMenu} />
-            <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}
-                onRowContextMenu={this.openItemMenuOnRowClick} />
-            <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 });
+            sortDirection: SortDirection.NONE,
+            filters: [],
+            key: "context-actions",
+            render: this.renderContextMenuTrigger,
+            width: "auto"
+        };
     }
-
-    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>
-
-    renderActions = (item: DataItem) =>
-        <Grid container justify="flex-end">
-            <IconButton onClick={event => this.openItemMenuOnActionsClick(event, item)}>
-                <MoreVertIcon />
-            </IconButton>
-        </Grid>
-
-    openItemMenuOnRowClick = (event: React.MouseEvent<HTMLElement>, item: DataItem) => {
-        event.preventDefault();
-        this.setContextMenuState({
-            anchorEl: mockAnchorFromMouseEvent(event),
-            item
-        });
-    }
-
-    openItemMenuOnActionsClick = (event: React.MouseEvent<HTMLElement>, item: DataItem) => {
-        this.setContextMenuState({
-            anchorEl: event.currentTarget,
-            item
-        });
-    }
-
-    closeContextMenu = () => {
-        this.setContextMenuState({});
-    }
-
-    setContextMenuState = (contextMenu: { anchorEl?: HTMLElement; item?: DataItem; }) => {
-        this.setState(prev => ({ contextMenu: { ...contextMenu, actions: prev.contextMenu.actions } }));
-    }
-
-    handleContextAction(action: keyof DataExplorerContextActions) {
-        return (item: DataItem) => this.props.contextActions[action](item);
-    }
-
-}
-
-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;
+);