Merge branch 'master' into 14604-ui-improvements
[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, SortDirection } 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 }
55
56 interface DataExplorerActionProps<T> {
57     onSetColumns: (columns: DataColumns<T>) => void;
58     onSearch: (value: string) => void;
59     onRowClick: (item: T) => void;
60     onRowDoubleClick: (item: T) => void;
61     onColumnToggle: (column: DataColumn<T>) => void;
62     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
63     onSortToggle: (column: DataColumn<T>) => void;
64     onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
65     onChangePage: (page: number) => void;
66     onChangeRowsPerPage: (rowsPerPage: number) => void;
67     onLoadMore: (page: number) => void;
68     extractKey?: (item: T) => React.Key;
69 }
70
71 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
72
73 export const DataExplorer = withStyles(styles)(
74     class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
75         componentDidMount() {
76             if (this.props.onSetColumns) {
77                 this.props.onSetColumns(this.props.columns);
78             }
79         }
80         render() {
81             const {
82                 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
83                 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
84                 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
85                 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput,
86                 paperKey, fetchMode
87             } = this.props;
88             return <Paper className={classes.root} {...paperProps} key={paperKey}>
89                 {(!hideColumnSelector || !hideSearchInput) && <Toolbar className={classes.toolbar}>
90                     <Grid container justify="space-between" wrap="nowrap" alignItems="center">
91                         {!hideSearchInput && <div className={classes.searchBox}>
92                             <SearchInput
93                                 value={searchValue}
94                                 onSearch={onSearch} />
95                         </div>}
96                         {actions}
97                         {!hideColumnSelector && <ColumnSelector
98                             columns={columns}
99                             onColumnToggle={onColumnToggle} />}
100                     </Grid>
101                 </Toolbar>}
102                 <DataTable
103                     columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
104                     items={items}
105                     onRowClick={(_, item: T) => onRowClick(item)}
106                     onContextMenu={onContextMenu}
107                     onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
108                     onFiltersChange={onFiltersChange}
109                     onSortToggle={onSortToggle}
110                     extractKey={extractKey}
111                     working={working}
112                     defaultView={dataTableDefaultView} />
113                 <Toolbar className={classes.footer}>
114                     <Grid container justify="flex-end">
115                         {fetchMode === DataTableFetchMode.PAGINATED ? <TablePagination
116                             count={itemsAvailable}
117                             rowsPerPage={rowsPerPage}
118                             rowsPerPageOptions={rowsPerPageOptions}
119                             page={this.props.page}
120                             onChangePage={this.changePage}
121                             onChangeRowsPerPage={this.changeRowsPerPage}
122                             component="div" /> : <Button
123                                 variant="text"
124                                 size="medium"
125                                 onClick={this.loadMore}
126                                 >Load more</Button>}
127                     </Grid>
128                 </Toolbar>
129             </Paper>;
130         }
131
132         changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
133             this.props.onChangePage(page);
134         }
135
136         changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
137             this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
138         }
139
140         loadMore = () => {
141             this.props.onLoadMore(this.props.page + 1);
142         }
143
144         renderContextMenuTrigger = (item: T) =>
145             <Grid container justify="center">
146                 <Tooltip title="More options" disableFocusListener>
147                     <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
148                         <MoreOptionsIcon />
149                     </IconButton>
150                 </Tooltip>
151             </Grid>
152
153         contextMenuColumn: DataColumn<any> = {
154             name: "Actions",
155             selected: true,
156             configurable: false,
157             filters: createTree(),
158             key: "context-actions",
159             render: this.renderContextMenuTrigger
160         };
161     }
162 );