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, Tooltip } 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, SortDirection } 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";
15 type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
17 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
19 paddingBottom: theme.spacing.unit * 2
22 paddingTop: theme.spacing.unit * 2
35 interface DataExplorerDataProps<T> {
37 itemsAvailable: number;
38 columns: DataColumns<T>;
41 rowsPerPageOptions: number[];
43 contextMenuColumn: boolean;
44 dataTableDefaultView?: React.ReactNode;
48 interface DataExplorerActionProps<T> {
49 onSetColumns: (columns: DataColumns<T>) => void;
50 onSearch: (value: string) => void;
51 onRowClick: (item: T) => void;
52 onRowDoubleClick: (item: T) => void;
53 onColumnToggle: (column: DataColumn<T>) => void;
54 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
55 onSortToggle: (column: DataColumn<T>) => void;
56 onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
57 onChangePage: (page: number) => void;
58 onChangeRowsPerPage: (rowsPerPage: number) => void;
59 extractKey?: (item: T) => React.Key;
62 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
64 export const DataExplorer = withStyles(styles)(
65 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
67 if (this.props.onSetColumns) {
68 this.props.onSetColumns(this.props.columns);
73 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
74 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
75 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
78 return <Paper className={classes.root}>
79 <Toolbar className={classes.toolbar}>
80 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
81 <div className={classes.searchBox}>
84 onSearch={onSearch} />
88 onColumnToggle={onColumnToggle} />
92 columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
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 defaultView={dataTableDefaultView}
103 <Toolbar className={classes.footer}>
104 <Grid container justify="flex-end">
106 count={itemsAvailable}
107 rowsPerPage={rowsPerPage}
108 rowsPerPageOptions={rowsPerPageOptions}
109 page={this.props.page}
110 onChangePage={this.changePage}
111 onChangeRowsPerPage={this.changeRowsPerPage}
118 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
119 this.props.onChangePage(page);
122 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
123 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
126 renderContextMenuTrigger = (item: T) =>
127 <Grid container justify="center">
128 <Tooltip title="More options" disableFocusListener>
129 <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
135 contextMenuColumn: DataColumn<any> = {
139 sortDirection: SortDirection.NONE,
141 key: "context-actions",
142 render: this.renderContextMenuTrigger