X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/4bbaf2a0cede89ea50d63d210c6631adc1970620..7364c06cd354952de2d469aa6285b7624864acf7:/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 9aeb28a7a5..026820be28 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,66 +3,111 @@
 // 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 { 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, { 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: [
-            {
-                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
+        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 <Paper>
+            <ContextMenu
+                {...this.state.contextMenu}
+                onClose={this.closeContextMenu} />
             <Toolbar>
                 <Grid container justify="flex-end">
                     <ColumnSelector
@@ -72,7 +117,8 @@ class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState>
             </Toolbar>
             <DataTable
                 columns={this.state.columns}
-                items={this.props.items} />
+                items={this.props.items}
+                onRowContextMenu={this.openItemMenuOnRowClick} />
             <Toolbar />
         </Paper>;
     }
@@ -101,6 +147,43 @@ 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.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.closeContextMenu();
+            this.props.contextActions[action](item);
+        };
+    }
+
 }
 
 const renderIcon = (dataItem: DataItem) => {
@@ -139,53 +222,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;
