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