Implement context menu actions
[arvados.git] / src / components / data-explorer / data-explorer.tsx
index 2c2c56e9998986cfc0078bce0ccb61811b071032..96e259f8f0ffff74ce10a5bfc9dcb3728518e58e 100644 (file)
@@ -3,23 +3,32 @@
 // 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 } 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 '../../common/formatters';
 import { DataItem } from './data-item';
+import { mockAnchorFromMouseEvent } from '../popover/helpers';
+import { ContextMenu, ContextMenuActions } from './context-menu';
 
 interface DataExplorerProps {
     items: DataItem[];
     onItemClick: (item: DataItem) => void;
+    contextMenuActions: ContextMenuActions;
 }
 
-type DataExplorerState = Pick<DataTableProps<DataItem>, "columns">;
+interface DataExplorerState {
+    columns: Array<DataColumn<DataItem>>;
+    contextMenu: {
+        anchorEl?: HTMLElement;
+        item?: DataItem;
+    };
+}
 
 class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState> {
     state: DataExplorerState = {
+        contextMenu: {},
         columns: [
             {
                 name: "Name",
@@ -44,7 +53,7 @@ class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState>
             {
                 name: "File size",
                 selected: true,
-                render: (item) => renderFileSize(item.fileSize)
+                render: item => renderFileSize(item.fileSize)
             },
             {
                 name: "Last modified",
@@ -55,16 +64,31 @@ class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState>
                 name: "Actions",
                 selected: true,
                 configurable: false,
-                renderHeader: () => this.renderActionsHeader(),
-                render: renderItemActions
+                renderHeader: () => null,
+                render: item => this.renderActions(item)
             }
         ]
     };
 
     render() {
-        return <DataTable
-            columns={this.state.columns}
-            items={this.props.items} />;
+        return <Paper>
+            <ContextMenu
+                {...this.state.contextMenu}
+                onClose={this.closeContextMenu}
+                actions={this.props.contextMenuActions} />
+            <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>) => {
@@ -74,13 +98,6 @@ class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState>
         this.setState({ columns });
     }
 
-    renderActionsHeader = () =>
-        <Grid container justify="flex-end">
-            <ColumnSelector
-                columns={this.state.columns}
-                onColumnToggle={this.toggleColumn} />
-        </Grid>
-
     renderName = (item: DataItem) =>
         <Grid
             container
@@ -98,6 +115,36 @@ class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState>
             </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.setState({
+            contextMenu: {
+                anchorEl: mockAnchorFromMouseEvent(event),
+                item
+            }
+        });
+    }
+
+    openItemMenuOnActionsClick = (event: React.MouseEvent<HTMLElement>, item: DataItem) => {
+        this.setState({
+            contextMenu: {
+                anchorEl: event.currentTarget,
+                item
+            }
+        });
+    }
+
+    closeContextMenu = () => {
+        this.setState({ contextMenu: {} });
+    }
+
 }
 
 const renderIcon = (dataItem: DataItem) => {
@@ -136,53 +183,4 @@ const renderStatus = (status?: string) =>
         {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;