Merge branch 'master' into 14093-data-explorer-table-cell-width
[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, WithStyles, TablePagination, IconButton } from '@material-ui/core';
7 import MoreVertIcon from "@material-ui/icons/MoreVert";
8 import { ColumnSelector } from "../column-selector/column-selector";
9 import { DataTable, DataColumns } from "../data-table/data-table";
10 import { DataColumn, SortDirection } from "../data-table/data-column";
11 import { DataTableFilterItem } from '../data-table-filters/data-table-filters';
12 import { SearchInput } from '../search-input/search-input';
13 import { ArvadosTheme } from "~/common/custom-theme";
14 import { DefaultView } from '../default-view/default-view';
15 import { IconType } from '../icon/icon';
16
17 type CssRules = 'searchBox' | "toolbar" | 'defaultRoot' | 'defaultMessage' | 'defaultIcon';
18
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
20     searchBox: {
21         paddingBottom: theme.spacing.unit * 2
22     },
23     toolbar: {
24         paddingTop: theme.spacing.unit * 2
25     },
26     defaultRoot: {
27         position: 'absolute',
28         width: '80%',
29         left: '50%',
30         top: '50%',
31         transform: 'translate(-50%, -50%)'
32     },
33     defaultMessage: {
34         fontSize: '1.75rem',
35     },
36     defaultIcon: {
37         fontSize: '6rem'
38     }
39 });
40
41 interface DataExplorerDataProps<T> {
42     items: T[];
43     itemsAvailable: number;
44     columns: DataColumns<T>;
45     searchValue: string;
46     rowsPerPage: number;
47     rowsPerPageOptions: number[];
48     page: number;
49     defaultIcon: IconType;
50     defaultMessages: string[];
51     contextMenuColumn: boolean;
52 }
53
54 interface DataExplorerActionProps<T> {
55     onSetColumns: (columns: DataColumns<T>) => void;
56     onSearch: (value: string) => void;
57     onRowClick: (item: T) => void;
58     onRowDoubleClick: (item: T) => void;
59     onColumnToggle: (column: DataColumn<T>) => void;
60     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
61     onSortToggle: (column: DataColumn<T>) => void;
62     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
63     onChangePage: (page: number) => void;
64     onChangeRowsPerPage: (rowsPerPage: number) => void;
65     extractKey?: (item: T) => React.Key;
66 }
67
68 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
69
70 export const DataExplorer = withStyles(styles)(
71     class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
72         componentDidMount() {
73             if (this.props.onSetColumns) {
74                 this.props.onSetColumns(this.props.columns);
75             }
76         }
77         render() {
78             const {
79                 columns, onContextMenu, onFiltersChange, onSortToggle, extractKey,
80                 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
81                 items, itemsAvailable, onRowClick, onRowDoubleClick, defaultIcon, defaultMessages, classes
82             } = this.props;
83             return <div>
84                 { items.length > 0 ? (
85                     <Paper>
86                         <Toolbar className={classes.toolbar}>
87                             <Grid container justify="space-between" wrap="nowrap" alignItems="center">
88                                 <div className={classes.searchBox}>
89                                     <SearchInput
90                                         value={searchValue}
91                                         onSearch={onSearch}/>
92                                 </div>
93                                 <ColumnSelector
94                                     columns={columns}
95                                     onColumnToggle={onColumnToggle}/>
96                             </Grid>
97                         </Toolbar>
98                         <DataTable
99                             columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
100                             items={items}
101                             onRowClick={(_, item: T) => onRowClick(item)}
102                             onContextMenu={onContextMenu}
103                             onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
104                             onFiltersChange={onFiltersChange}
105                             onSortToggle={onSortToggle}
106                             extractKey={extractKey}/>
107                         <Toolbar>
108                             <Grid container justify="flex-end">
109                                 <TablePagination
110                                     count={itemsAvailable}
111                                     rowsPerPage={rowsPerPage}
112                                     rowsPerPageOptions={rowsPerPageOptions}
113                                     page={this.props.page}
114                                     onChangePage={this.changePage}
115                                     onChangeRowsPerPage={this.changeRowsPerPage}
116                                     component="div" />
117                             </Grid>
118                         </Toolbar>
119                     </Paper>
120                 ) : (
121                     <DefaultView
122                         classRoot={classes.defaultRoot}
123                         icon={defaultIcon}
124                         classIcon={classes.defaultIcon}
125                         messages={defaultMessages}
126                         classMessage={classes.defaultMessage} />
127                 )}
128             </div>;
129         }
130
131         changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
132             this.props.onChangePage(page);
133         }
134
135         changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
136             this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
137         }
138
139         renderContextMenuTrigger = (item: T) =>
140             <Grid container justify="flex-end">
141                 <IconButton onClick={event => this.props.onContextMenu(event, item)}>
142                     <MoreVertIcon/>
143                 </IconButton>
144             </Grid>
145
146         contextMenuColumn: DataColumn<any> = {
147             name: "Actions",
148             selected: true,
149             configurable: false,
150             sortDirection: SortDirection.NONE,
151             filters: [],
152             key: "context-actions",
153             render: this.renderContextMenuTrigger
154         };
155     }
156 );