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