X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/430515acafe8731fdbeea5f9caae58991683ad94..5e9c2244747eff9fc02ff3c0d140bdde5e0b6845:/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 9aeb28a7..c7a296a6 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -2,190 +2,272 @@
 //
 // 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 MoreVertIcon from "@material-ui/icons/MoreVert";
-import Popover from '../popover/popover';
-import { formatFileSize, formatDate } from '../../common/formatters';
-import { DataItem } from './data-item';
-
-interface DataExplorerProps {
-    items: DataItem[];
-    onItemClick: (item: DataItem) => void;
+import React from 'react';
+import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, WithStyles, TablePagination, IconButton, Tooltip, Button } from '@material-ui/core';
+import { ColumnSelector } from "components/column-selector/column-selector";
+import { DataTable, DataColumns, DataTableFetchMode } from "components/data-table/data-table";
+import { DataColumn } from "components/data-table/data-column";
+import { SearchInput } from 'components/search-input/search-input';
+import { ArvadosTheme } from "common/custom-theme";
+import { createTree } from 'models/tree';
+import { DataTableFilters } from 'components/data-table-filters/data-table-filters-tree';
+import {
+    CloseIcon,
+    IconType,
+    MaximizeIcon,
+    UnMaximizeIcon,
+    MoreOptionsIcon
+} from 'components/icon/icon';
+import { PaperProps } from '@material-ui/core/Paper';
+import { MPVPanelProps } from 'components/multi-panel-view/multi-panel-view';
+
+type CssRules = 'searchBox' | 'headerMenu' | "toolbar" | "footer" | "root" | 'moreOptionsButton' | 'title' | 'dataTable' | 'container';
+
+const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
+    searchBox: {
+        paddingBottom: 0,
+    },
+    toolbar: {
+        paddingTop: 0,
+        paddingRight: theme.spacing.unit,
+    },
+    footer: {
+        overflow: 'auto'
+    },
+    root: {
+        height: '100%',
+    },
+    moreOptionsButton: {
+        padding: 0
+    },
+    title: {
+        display: 'inline-block',
+        paddingLeft: theme.spacing.unit * 2,
+        paddingTop: theme.spacing.unit * 2,
+        fontSize: '18px'
+    },
+    dataTable: {
+        height: '100%',
+        overflow: 'auto',
+    },
+    container: {
+        height: '100%',
+    },
+    headerMenu: {
+        float: 'right',
+        display: 'inline-block',
+    }
+});
+
+interface DataExplorerDataProps<T> {
+    fetchMode: DataTableFetchMode;
+    items: T[];
+    itemsAvailable: number;
+    columns: DataColumns<T>;
+    searchLabel?: string;
+    searchValue: string;
+    rowsPerPage: number;
+    rowsPerPageOptions: number[];
+    page: number;
+    contextMenuColumn: boolean;
+    defaultViewIcon?: IconType;
+    defaultViewMessages?: string[];
+    working?: boolean;
+    currentRefresh?: string;
+    currentRoute?: string;
+    hideColumnSelector?: boolean;
+    paperProps?: PaperProps;
+    actions?: React.ReactNode;
+    hideSearchInput?: boolean;
+    title?: React.ReactNode;
+    paperKey?: string;
+    currentItemUuid: string;
+    elementPath?: string;
+}
+
+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: DataTableFilters, column: DataColumn<T>) => void;
+    onChangePage: (page: number) => void;
+    onChangeRowsPerPage: (rowsPerPage: number) => void;
+    onLoadMore: (page: number) => void;
+    extractKey?: (item: T) => React.Key;
 }
 
-type DataExplorerState = Pick<DataTableProps<DataItem>, "columns">;
-
-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
+type DataExplorerProps<T> = DataExplorerDataProps<T> &
+    DataExplorerActionProps<T> & WithStyles<CssRules> & MPVPanelProps;
+
+export const DataExplorer = withStyles(styles)(
+    class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
+        state = {
+            showLoading: false,
+            prevRefresh: '',
+            prevRoute: '',
+        };
+
+        componentDidUpdate(prevProps: DataExplorerProps<T>) {
+            const currentRefresh = this.props.currentRefresh || '';
+            const currentRoute = this.props.currentRoute || '';
+
+            if (currentRoute !== this.state.prevRoute) {
+                // Component already mounted, but the user comes from a route change,
+                // like browsing through a project hierarchy.
+                this.setState({
+                    showLoading: this.props.working,
+                    prevRoute: currentRoute,
+                });
+            }
+
+            if (currentRefresh !== this.state.prevRefresh) {
+                // Component already mounted, but the user just clicked the
+                // refresh button.
+                this.setState({
+                    showLoading: this.props.working,
+                    prevRefresh: currentRefresh,
+                });
+            }
+            if (this.state.showLoading && !this.props.working) {
+                this.setState({
+                    showLoading: false,
+                });
             }
-        ]
-    };
-
-    render() {
-        return <Paper>
-            <Toolbar>
-                <Grid container justify="flex-end">
-                    <ColumnSelector
-                        columns={this.state.columns}
-                        onColumnToggle={this.toggleColumn} />
+        }
+
+        componentDidMount() {
+            if (this.props.onSetColumns) {
+                this.props.onSetColumns(this.props.columns);
+            }
+            // Component just mounted, so we need to show the loading indicator.
+            this.setState({
+                showLoading: this.props.working,
+                prevRefresh: this.props.currentRefresh || '',
+                prevRoute: this.props.currentRoute || '',
+            });
+        }
+
+        render() {
+            const {
+                columns, onContextMenu, onFiltersChange, onSortToggle, extractKey,
+                rowsPerPage, rowsPerPageOptions, onColumnToggle, searchLabel, searchValue, onSearch,
+                items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
+                defaultViewIcon, defaultViewMessages, hideColumnSelector, actions, paperProps, hideSearchInput,
+                paperKey, fetchMode, currentItemUuid, title,
+                doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName, panelMaximized, elementPath
+            } = this.props;
+
+            return <Paper className={classes.root} {...paperProps} key={paperKey} data-cy={this.props["data-cy"]}>
+                <Grid container direction="column" wrap="nowrap" className={classes.container}>
+                    <div>
+                        {title && <Grid item xs className={classes.title}>{title}</Grid>}
+                        {
+                            (!hideColumnSelector || !hideSearchInput || !!actions) &&
+                            <Grid className={classes.headerMenu} item xs>
+                                <Toolbar className={classes.toolbar}>
+                                    {!hideSearchInput && <div className={classes.searchBox}>
+                                        {!hideSearchInput && <SearchInput
+                                            label={searchLabel}
+                                            value={searchValue}
+                                            selfClearProp={currentItemUuid}
+                                            onSearch={onSearch} />}
+                                    </div>}
+                                    {actions}
+                                    {!hideColumnSelector && <ColumnSelector
+                                        columns={columns}
+                                        onColumnToggle={onColumnToggle} />}
+                                    { doUnMaximizePanel && panelMaximized &&
+                                    <Tooltip title={`Unmaximize ${panelName || 'panel'}`} disableFocusListener>
+                                        <IconButton onClick={doUnMaximizePanel}><UnMaximizeIcon /></IconButton>
+                                    </Tooltip> }
+                                    { doMaximizePanel && !panelMaximized &&
+                                        <Tooltip title={`Maximize ${panelName || 'panel'}`} disableFocusListener>
+                                            <IconButton onClick={doMaximizePanel}><MaximizeIcon /></IconButton>
+                                        </Tooltip> }
+                                    { doHidePanel &&
+                                        <Tooltip title={`Close ${panelName || 'panel'}`} disableFocusListener>
+                                            <IconButton disabled={panelMaximized} onClick={doHidePanel}><CloseIcon /></IconButton>
+                                        </Tooltip> }
+                                </Toolbar>
+                            </Grid>
+                        }
+                    </div>
+                <Grid item xs="auto" className={classes.dataTable}><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={this.state.showLoading}
+                    defaultViewIcon={defaultViewIcon}
+                    defaultViewMessages={defaultViewMessages}
+                    currentItemUuid={currentItemUuid}
+                    currentRoute={paperKey} /></Grid>
+                <Grid item xs><Toolbar className={classes.footer}>
+                    {
+                        elementPath &&
+                        <Grid container>
+                            <span data-cy="element-path">
+                                {elementPath}
+                            </span>
+                        </Grid>
+                    }
+                    <Grid container={!elementPath} justify="flex-end">
+                        {fetchMode === DataTableFetchMode.PAGINATED ? <TablePagination
+                            count={itemsAvailable}
+                            rowsPerPage={rowsPerPage}
+                            rowsPerPageOptions={rowsPerPageOptions}
+                            page={this.props.page}
+                            onChangePage={this.changePage}
+                            onChangeRowsPerPage={this.changeRowsPerPage}
+                            // Disable next button on empty lists since that's not default behavior
+                            nextIconButtonProps={(itemsAvailable > 0) ? {} : {disabled: true}}
+                            component="div" /> : <Button
+                                variant="text"
+                                size="medium"
+                                onClick={this.loadMore}
+                            >Load more</Button>}
+                    </Grid>
+                </Toolbar></Grid>
                 </Grid>
-            </Toolbar>
-            <DataTable
-                columns={this.state.columns}
-                items={this.props.items} />
-            <Toolbar />
-        </Paper>;
-    }
+            </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 });
-    }
+        changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
+            this.props.onChangePage(page);
+        }
 
-    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>
+        changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
+            this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
+        }
 
-}
+        loadMore = () => {
+            this.props.onLoadMore(this.props.page + 1);
+        }
+
+        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)}>
+                        <MoreOptionsIcon />
+                    </IconButton>
+                </Tooltip>
+            </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,
+            filters: createTree(),
+            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;
+);
