X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/fbd2012fa56f6de44dfc4008d70e5a21cfcaf459..7444429308f438fb2e29188db8f208223ed39128:/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 2c2c56e999..ded1b5e39c 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,68 +3,128 @@
 // 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, toggleSortDirection, SortDirection, resetSortDirection } 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 TableSortLabel from '@material-ui/core/TableSortLabel';
 import { formatFileSize, formatDate } from '../../common/formatters';
 import { DataItem } from './data-item';
-
+import { mockAnchorFromMouseEvent } from '../popover/helpers';
+import ContextMenu 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;
+    };
+}
 
 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: () => this.renderActionsHeader(),
-                render: renderItemActions
-            }
-        ]
+        contextMenu: {},
+        columns: [{
+            name: "Name",
+            selected: true,
+            sortDirection: "asc",
+            onSortToggle: () => this.toggleSort("Name"),
+            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,
+            onSortToggle: () => this.toggleSort("Last modified"),
+            render: item => renderDate(item.lastModified)
+        }, {
+            name: "Actions",
+            selected: true,
+            configurable: false,
+            renderHeader: () => null,
+            render: item => this.renderActions(item)
+        }]
     };
 
+    contextMenuActions = [[{
+        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")
+    }
+    ]];
+
     render() {
-        return <DataTable
-            columns={this.state.columns}
-            items={this.props.items} />;
+        return <Paper>
+            <ContextMenu
+                {...this.state.contextMenu}
+                actions={this.contextMenuActions}
+                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>) => {
@@ -74,13 +134,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 +151,50 @@ 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: {} });
+    }
+
+    handleContextAction(action: keyof DataExplorerContextActions) {
+        return (item: DataItem) => {
+            this.closeContextMenu();
+            this.props.contextActions[action](item);
+        };
+    }
+
+    toggleSort = (columnName: string) => {
+        this.setState({
+            columns: this.state.columns.map((column, index) =>
+                column.name === columnName ? toggleSortDirection(column) : resetSortDirection(column))
+        });
+    }
+
 }
 
 const renderIcon = (dataItem: DataItem) => {
@@ -136,53 +233,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;
