9013f948bfd3910b46f6542ac649cc96cfc2d7bd
[arvados-workbench2.git] / src / components / data-explorer / data-explorer.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, Theme, WithStyles, TablePagination, Table } from '@material-ui/core';
7 import ContextMenu, { ContextMenuActionGroup, ContextMenuAction } from "../../components/context-menu/context-menu";
8 import ColumnSelector from "../../components/column-selector/column-selector";
9 import DataTable from "../../components/data-table/data-table";
10 import { mockAnchorFromMouseEvent } from "../../components/popover/helpers";
11 import { DataColumn, toggleSortDirection } from "../../components/data-table/data-column";
12 import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
13 import SearchInput from '../search-input/search-input';
14
15 interface DataExplorerProps<T> {
16     items: T[];
17     columns: Array<DataColumn<T>>;
18     contextActions: ContextMenuActionGroup[];
19     searchValue: string;
20     rowsPerPage: number;
21     page: number;
22     onSearch: (value: string) => void;
23     onRowClick: (item: T) => void;
24     onColumnToggle: (column: DataColumn<T>) => void;
25     onContextAction: (action: ContextMenuAction, item: T) => void;
26     onSortToggle: (column: DataColumn<T>) => void;
27     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
28     onChangePage: (page: number) => void;
29     onChangeRowsPerPage: (rowsPerPage: number) => void;
30 }
31
32 interface DataExplorerState<T> {
33     contextMenu: {
34         anchorEl?: HTMLElement;
35         item?: T;
36     };
37 }
38
39 class DataExplorer<T> extends React.Component<DataExplorerProps<T> & WithStyles<CssRules>, DataExplorerState<T>> {
40     state: DataExplorerState<T> = {
41         contextMenu: {}
42     };
43
44     render() {
45         return <Paper>
46             <ContextMenu
47                 anchorEl={this.state.contextMenu.anchorEl}
48                 actions={this.props.contextActions}
49                 onActionClick={this.callAction}
50                 onClose={this.closeContextMenu} />
51             <Toolbar className={this.props.classes.toolbar}>
52                 {this.props.items.length > 0 &&
53                     <Grid container justify="space-between" wrap="nowrap" alignItems="center">
54                         <div className={this.props.classes.searchBox}>
55                             <SearchInput
56                                 value={this.props.searchValue}
57                                 onSearch={this.props.onSearch} />
58                         </div>
59                         <ColumnSelector
60                             columns={this.props.columns}
61                             onColumnToggle={this.props.onColumnToggle} />
62                     </Grid>}
63
64             </Toolbar>
65             <DataTable
66                 columns={this.props.columns}
67                 items={this.props.items}
68                 onRowClick={(_, item: T) => this.props.onRowClick(item)}
69                 onRowContextMenu={this.openContextMenu}
70                 onFiltersChange={this.props.onFiltersChange}
71                 onSortToggle={this.props.onSortToggle} />
72             <Toolbar>
73                 {this.props.items.length > 0 &&
74                     <Grid container justify="flex-end">
75                         <TablePagination
76                             count={this.props.items.length}
77                             rowsPerPage={this.props.rowsPerPage}
78                             page={this.props.page}
79                             onChangePage={this.changePage}
80                             onChangeRowsPerPage={this.changeRowsPerPage}
81                             component="div"
82                         />
83                     </Grid>}
84             </Toolbar>
85         </Paper>;
86     }
87
88     openContextMenu = (event: React.MouseEvent<HTMLElement>, item: T) => {
89         event.preventDefault();
90         this.setState({
91             contextMenu: {
92                 anchorEl: mockAnchorFromMouseEvent(event),
93                 item
94             }
95         });
96     }
97
98     closeContextMenu = () => {
99         this.setState({ contextMenu: {} });
100     }
101
102     callAction = (action: ContextMenuAction) => {
103         const { item } = this.state.contextMenu;
104         this.closeContextMenu();
105         if (item) {
106             this.props.onContextAction(action, item);
107         }
108     }
109
110     changePage = (event: React.MouseEvent<HTMLButtonElement> | null, page: number) => {
111         this.props.onChangePage(page);
112     }
113
114     changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
115         this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
116     }
117
118 }
119
120 type CssRules = "searchBox" | "toolbar";
121
122 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
123     searchBox: {
124         paddingBottom: theme.spacing.unit * 2
125     },
126     toolbar: {
127         paddingTop: theme.spacing.unit * 2
128     }
129 });
130
131 export default withStyles(styles)(DataExplorer);