refs #14785-selection-of-table-content
[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, Button } from '@material-ui/core';
7 import { ColumnSelector } from "~/components/column-selector/column-selector";
8 import { DataTable, DataColumns, DataTableFetchMode } from "~/components/data-table/data-table";
9 import { DataColumn } from "~/components/data-table/data-column";
10 import { SearchInput } from '~/components/search-input/search-input';
11 import { ArvadosTheme } from "~/common/custom-theme";
12 import { createTree } from '~/models/tree';
13 import { DataTableFilters } from '~/components/data-table-filters/data-table-filters-tree';
14 import { MoreOptionsIcon } from '~/components/icon/icon';
15 import { PaperProps } from '@material-ui/core/Paper';
16
17 type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
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     footer: {
27         overflow: 'auto'
28     },
29     root: {
30         height: '100%'
31     },
32     moreOptionsButton: {
33         padding: 0
34     }
35 });
36
37 interface DataExplorerDataProps<T> {
38     fetchMode: DataTableFetchMode;
39     items: T[];
40     itemsAvailable: number;
41     columns: DataColumns<T>;
42     searchValue: string;
43     rowsPerPage: number;
44     rowsPerPageOptions: number[];
45     page: number;
46     contextMenuColumn: boolean;
47     dataTableDefaultView?: React.ReactNode;
48     working?: boolean;
49     hideColumnSelector?: boolean;
50     paperProps?: PaperProps;
51     actions?: React.ReactNode;
52     hideSearchInput?: boolean;
53     paperKey?: string;
54     currentItemUuid: string;
55 }
56
57 interface DataExplorerActionProps<T> {
58     onSetColumns: (columns: DataColumns<T>) => void;
59     onSearch: (value: string) => void;
60     onRowClick: (item: T) => void;
61     onRowDoubleClick: (item: T) => void;
62     onColumnToggle: (column: DataColumn<T>) => void;
63     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
64     onSortToggle: (column: DataColumn<T>) => void;
65     onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
66     onChangePage: (page: number) => void;
67     onChangeRowsPerPage: (rowsPerPage: number) => void;
68     onLoadMore: (page: number) => void;
69     extractKey?: (item: T) => React.Key;
70 }
71
72 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
73
74 export const DataExplorer = withStyles(styles)(
75     class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
76         componentDidMount() {
77             if (this.props.onSetColumns) {
78                 this.props.onSetColumns(this.props.columns);
79             }
80         }
81         render() {
82             const {
83                 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
84                 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
85                 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
86                 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput,
87                 paperKey, fetchMode, currentItemUuid
88             } = this.props;
89             return <Paper className={classes.root} {...paperProps} key={paperKey}>
90                 {(!hideColumnSelector || !hideSearchInput) && <Toolbar className={classes.toolbar}>
91                     <Grid container justify="space-between" wrap="nowrap" alignItems="center">
92                         {!hideSearchInput && <div className={classes.searchBox}>
93                             <SearchInput
94                                 value={searchValue}
95                                 onSearch={onSearch} />
96                         </div>}
97                         {actions}
98                         {!hideColumnSelector && <ColumnSelector
99                             columns={columns}
100                             onColumnToggle={onColumnToggle} />}
101                     </Grid>
102                 </Toolbar>}
103                 <DataTable
104                     columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
105                     items={items}
106                     onRowClick={(_, item: T) => onRowClick(item)}
107                     onContextMenu={onContextMenu}
108                     onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
109                     onFiltersChange={onFiltersChange}
110                     onSortToggle={onSortToggle}
111                     extractKey={extractKey}
112                     working={working}
113                     defaultView={dataTableDefaultView} 
114                     currentItemUuid={currentItemUuid} 
115                     currentRoute={paperKey} />
116                 <Toolbar className={classes.footer}>
117                     <Grid container justify="flex-end">
118                         {fetchMode === DataTableFetchMode.PAGINATED ? <TablePagination
119                             count={itemsAvailable}
120                             rowsPerPage={rowsPerPage}
121                             rowsPerPageOptions={rowsPerPageOptions}
122                             page={this.props.page}
123                             onChangePage={this.changePage}
124                             onChangeRowsPerPage={this.changeRowsPerPage}
125                             component="div" /> : <Button
126                                 variant="text"
127                                 size="medium"
128                                 onClick={this.loadMore}
129                                 >Load more</Button>}
130                     </Grid>
131                 </Toolbar>
132             </Paper>;
133         }
134
135         changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
136             this.props.onChangePage(page);
137         }
138
139         changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
140             this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
141         }
142
143         loadMore = () => {
144             this.props.onLoadMore(this.props.page + 1);
145         }
146
147         renderContextMenuTrigger = (item: T) =>
148             <Grid container justify="center">
149                 <Tooltip title="More options" disableFocusListener>
150                     <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
151                         <MoreOptionsIcon />
152                     </IconButton>
153                 </Tooltip>
154             </Grid>
155
156         contextMenuColumn: DataColumn<any> = {
157             name: "Actions",
158             selected: true,
159             configurable: false,
160             filters: createTree(),
161             key: "context-actions",
162             render: this.renderContextMenuTrigger
163         };
164     }
165 );