Merge branch '14319_loaded_contents_for_project_in_trash'
[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, Tooltip } 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
15 type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
16
17 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
18     searchBox: {
19         paddingBottom: theme.spacing.unit * 2
20     },
21     toolbar: {
22         paddingTop: theme.spacing.unit * 2
23     },
24     footer: {
25         overflow: 'auto'
26     },
27     root: {
28         height: '100%'
29     },
30     moreOptionsButton: {
31         padding: 0
32     }
33 });
34
35 interface DataExplorerDataProps<T> {
36     items: T[];
37     itemsAvailable: number;
38     columns: DataColumns<T>;
39     searchValue: string;
40     rowsPerPage: number;
41     rowsPerPageOptions: number[];
42     page: number;
43     contextMenuColumn: boolean;
44     dataTableDefaultView?: React.ReactNode;
45     working?: boolean;
46 }
47
48 interface DataExplorerActionProps<T> {
49     onSetColumns: (columns: DataColumns<T>) => void;
50     onSearch: (value: string) => void;
51     onRowClick: (item: T) => void;
52     onRowDoubleClick: (item: T) => void;
53     onColumnToggle: (column: DataColumn<T>) => void;
54     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
55     onSortToggle: (column: DataColumn<T>) => void;
56     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
57     onChangePage: (page: number) => void;
58     onChangeRowsPerPage: (rowsPerPage: number) => void;
59     extractKey?: (item: T) => React.Key;
60 }
61
62 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
63
64 export const DataExplorer = withStyles(styles)(
65     class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
66         componentDidMount() {
67             if (this.props.onSetColumns) {
68                 this.props.onSetColumns(this.props.columns);
69             }
70         }
71         render() {
72             const {
73                 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
74                 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
75                 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
76                 dataTableDefaultView
77             } = this.props;
78             return <Paper className={classes.root}>
79                 <Toolbar className={classes.toolbar}>
80                     <Grid container justify="space-between" wrap="nowrap" alignItems="center">
81                         <div className={classes.searchBox}>
82                             <SearchInput
83                                 value={searchValue}
84                                 onSearch={onSearch} />
85                         </div>
86                         <ColumnSelector
87                             columns={columns}
88                             onColumnToggle={onColumnToggle} />
89                     </Grid>
90                 </Toolbar>
91                 <DataTable
92                     columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
93                     items={items}
94                     onRowClick={(_, item: T) => onRowClick(item)}
95                     onContextMenu={onContextMenu}
96                     onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
97                     onFiltersChange={onFiltersChange}
98                     onSortToggle={onSortToggle}
99                     extractKey={extractKey}
100                     working={working}
101                     defaultView={dataTableDefaultView}
102                 />
103                 <Toolbar className={classes.footer}>
104                     <Grid container justify="flex-end">
105                         <TablePagination
106                             count={itemsAvailable}
107                             rowsPerPage={rowsPerPage}
108                             rowsPerPageOptions={rowsPerPageOptions}
109                             page={this.props.page}
110                             onChangePage={this.changePage}
111                             onChangeRowsPerPage={this.changeRowsPerPage}
112                             component="div" />
113                     </Grid>
114                 </Toolbar>
115             </Paper>;
116         }
117
118         changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
119             this.props.onChangePage(page);
120         }
121
122         changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
123             this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
124         }
125
126         renderContextMenuTrigger = (item: T) =>
127             <Grid container justify="center">
128                 <Tooltip title="More options" disableFocusListener>
129                     <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
130                         <MoreVertIcon />
131                     </IconButton>
132                 </Tooltip>
133             </Grid>
134
135         contextMenuColumn: DataColumn<any> = {
136             name: "Actions",
137             selected: true,
138             configurable: false,
139             sortDirection: SortDirection.NONE,
140             filters: [],
141             key: "context-actions",
142             render: this.renderContextMenuTrigger
143         };
144     }
145 );