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