Add close menu call when handling conext menu action
[arvados.git] / src / components / data-explorer / data-explorer.tsx
index ada01597888c83a8bf117d842d580c2ed70a793d..026820be2849b11ad4ef5273d22190fd4d7c63a9 100644 (file)
 // 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 { DataTable, DataColumn, ColumnSelector } from "../../components/data-table";
+import { Typography, Grid, Paper, Toolbar } from '@material-ui/core';
+import IconButton from '@material-ui/core/IconButton';
 import MoreVertIcon from "@material-ui/icons/MoreVert";
-import Popover from '../popover/popover';
-import { formatFileSize, formatDate } from './formatters';
+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;
 }
 
-type DataExplorerState = Pick<DataTableProps<DataItem>, "columns">;
+interface DataExplorerState {
+    columns: Array<DataColumn<DataItem>>;
+    contextMenu: {
+        anchorEl?: HTMLElement;
+        item?: DataItem;
+        actions: Array<ContextMenuActionGroup<DataItem>>;
+    };
+}
 
 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
+        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)
+        }]
     };
 
     render() {
-        return (
+        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: Column<DataItem>) => {
+    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 });
     }
 
-    renderActionsHeader = () => {
-        return (
-            <Grid container justify="flex-end">
-                <ColumnsConfigurator
-                    columns={this.state.columns}
-                    onColumnToggle={this.toggleColumn}
-                />
+    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
+        });
     }
 
-    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>
-            )
-        );
+    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.closeContextMenu();
+            this.props.contextActions[action](item);
+        };
+    }
+
 }
 
 const renderIcon = (dataItem: DataItem) => {
@@ -125,102 +197,29 @@ const renderIcon = (dataItem: DataItem) => {
     }
 };
 
-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 (
-        <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>
-);
+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;