1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, WithStyles, TablePagination, IconButton } from '@material-ui/core';
7 import MoreVertIcon from "@material-ui/icons/MoreVert";
8 import { ColumnSelector } from "../column-selector/column-selector";
9 import { DataTable, DataColumns } from "../data-table/data-table";
10 import { DataColumn } from "../data-table/data-column";
11 import { DataTableFilterItem } from '../data-table-filters/data-table-filters';
12 import { SearchInput } from '../search-input/search-input';
13 import { ArvadosTheme } from "../../common/custom-theme";
14 import { DefaultView } from '../default-view/default-view';
15 import { IconType } from '../icon/icon';
17 type CssRules = 'searchBox' | "toolbar" | 'defaultRoot' | 'defaultMessage' | 'defaultIcon';
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
21 paddingBottom: theme.spacing.unit * 2
24 paddingTop: theme.spacing.unit * 2
31 transform: 'translate(-50%, -50%)'
41 interface DataExplorerDataProps<T> {
43 itemsAvailable: number;
44 columns: DataColumns<T>;
47 rowsPerPageOptions: number[];
49 defaultIcon: IconType;
50 defaultMessages: string[];
53 interface DataExplorerActionProps<T> {
54 onSearch: (value: string) => void;
55 onRowClick: (item: T) => void;
56 onRowDoubleClick: (item: T) => void;
57 onColumnToggle: (column: DataColumn<T>) => void;
58 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
59 onSortToggle: (column: DataColumn<T>) => void;
60 onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
61 onChangePage: (page: number) => void;
62 onChangeRowsPerPage: (rowsPerPage: number) => void;
63 extractKey?: (item: T) => React.Key;
66 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
68 export const DataExplorer = withStyles(styles)(
69 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
72 columns, onContextMenu, onFiltersChange, onSortToggle, extractKey,
73 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
74 items, itemsAvailable, onRowClick, onRowDoubleClick, defaultIcon, defaultMessages, classes
77 { items.length > 0 ? (
79 <Toolbar className={classes.toolbar}>
80 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
81 <div className={classes.searchBox}>
88 onColumnToggle={onColumnToggle}/>
92 columns={[...columns, this.contextMenuColumn]}
94 onRowClick={(_, item: T) => onRowClick(item)}
95 onContextMenu={onContextMenu}
96 onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
97 onFiltersChange={onFiltersChange}
98 onSortToggle={onSortToggle}
99 extractKey={extractKey}/>
101 <Grid container justify="flex-end">
103 count={itemsAvailable}
104 rowsPerPage={rowsPerPage}
105 rowsPerPageOptions={rowsPerPageOptions}
106 page={this.props.page}
107 onChangePage={this.changePage}
108 onChangeRowsPerPage={this.changeRowsPerPage}
115 classRoot={classes.defaultRoot}
117 classIcon={classes.defaultIcon}
118 messages={defaultMessages}
119 classMessage={classes.defaultMessage} />
124 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
125 this.props.onChangePage(page);
128 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
129 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
132 renderContextMenuTrigger = (item: T) =>
133 <Grid container justify="flex-end">
134 <IconButton onClick={event => this.props.onContextMenu(event, item)}>
139 contextMenuColumn = {
143 key: "context-actions",
144 render: this.renderContextMenuTrigger,