17579: Fixed test issues removed not required files
[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
87         componentDidMount() {
88             if (this.props.onSetColumns) {
89                 this.props.onSetColumns(this.props.columns);
90             }
91         }
92
93         render() {
94             const {
95                 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
96                 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchLabel, searchValue, onSearch,
97                 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
98                 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput,
99                 paperKey, fetchMode, currentItemUuid, title
100             } = this.props;
101
102             return <Paper className={classes.root} {...paperProps} key={paperKey}>
103                 {title && <div className={classes.title}>{title}</div>}
104                 {(!hideColumnSelector || !hideSearchInput) && <Toolbar className={title ? classes.toolbarUnderTitle : classes.toolbar}>
105                     <Grid container justify="space-between" wrap="nowrap" alignItems="center">
106                         <div className={classes.searchBox}>
107                             {!hideSearchInput && <SearchInput
108                                 label={searchLabel}
109                                 value={searchValue}
110                                 selfClearProp={currentItemUuid}
111                                 onSearch={onSearch} />}
112                         </div>
113                         {actions}
114                         {!hideColumnSelector && <ColumnSelector
115                             columns={columns}
116                             onColumnToggle={onColumnToggle} />}
117                     </Grid>
118                 </Toolbar>}
119                 <DataTable
120                     columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
121                     items={items}
122                     onRowClick={(_, item: T) => onRowClick(item)}
123                     onContextMenu={onContextMenu}
124                     onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
125                     onFiltersChange={onFiltersChange}
126                     onSortToggle={onSortToggle}
127                     extractKey={extractKey}
128                     working={working}
129                     defaultView={dataTableDefaultView}
130                     currentItemUuid={currentItemUuid}
131                     currentRoute={paperKey} />
132                 <Toolbar className={classes.footer}>
133                     <Grid container justify="flex-end">
134                         {fetchMode === DataTableFetchMode.PAGINATED ? <TablePagination
135                             count={itemsAvailable}
136                             rowsPerPage={rowsPerPage}
137                             rowsPerPageOptions={rowsPerPageOptions}
138                             page={this.props.page}
139                             onChangePage={this.changePage}
140                             onChangeRowsPerPage={this.changeRowsPerPage}
141                             // Disable next button on empty lists since that's not default behavior
142                             nextIconButtonProps={(itemsAvailable > 0) ? {} : {disabled: true}}
143                             component="div" /> : <Button
144                                 variant="text"
145                                 size="medium"
146                                 onClick={this.loadMore}
147                             >Load more</Button>}
148                     </Grid>
149                 </Toolbar>
150             </Paper>;
151         }
152
153         changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
154             this.props.onChangePage(page);
155         }
156
157         changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
158             this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
159         }
160
161         loadMore = () => {
162             this.props.onLoadMore(this.props.page + 1);
163         }
164
165         renderContextMenuTrigger = (item: T) =>
166             <Grid container justify="center">
167                 <Tooltip title="More options" disableFocusListener>
168                     <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
169                         <MoreOptionsIcon />
170                     </IconButton>
171                 </Tooltip>
172             </Grid>
173
174         contextMenuColumn: DataColumn<any> = {
175             name: "Actions",
176             selected: true,
177             configurable: false,
178             filters: createTree(),
179             key: "context-actions",
180             render: this.renderContextMenuTrigger
181         };
182     }
183 );