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";
17 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
19 paddingBottom: theme.spacing.unit * 2
22 paddingTop: theme.spacing.unit * 2
32 interface DataExplorerDataProps<T> {
34 itemsAvailable: number;
35 columns: DataColumns<T>;
38 rowsPerPageOptions: number[];
40 contextMenuColumn: boolean;
41 dataTableDefaultView?: React.ReactNode;
45 interface DataExplorerActionProps<T> {
46 onSetColumns: (columns: DataColumns<T>) => void;
47 onSearch: (value: string) => void;
48 onRowClick: (item: T) => void;
49 onRowDoubleClick: (item: T) => void;
50 onColumnToggle: (column: DataColumn<T>) => void;
51 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
52 onSortToggle: (column: DataColumn<T>) => void;
53 onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
54 onChangePage: (page: number) => void;
55 onChangeRowsPerPage: (rowsPerPage: number) => void;
56 extractKey?: (item: T) => React.Key;
59 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
61 export const DataExplorer = withStyles(styles)(
62 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
64 if (this.props.onSetColumns) {
65 this.props.onSetColumns(this.props.columns);
70 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
71 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
72 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
75 return <Paper className={classes.root}>
76 <Toolbar className={classes.toolbar}>
77 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
78 <div className={classes.searchBox}>
81 onSearch={onSearch} />
85 onColumnToggle={onColumnToggle} />
89 columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
91 onRowClick={(_, item: T) => onRowClick(item)}
92 onContextMenu={onContextMenu}
93 onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
94 onFiltersChange={onFiltersChange}
95 onSortToggle={onSortToggle}
96 extractKey={extractKey}
98 defaultView={dataTableDefaultView}
100 <Toolbar className={classes.footer}>
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 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
116 this.props.onChangePage(page);
119 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
120 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
123 renderContextMenuTrigger = (item: T) =>
124 <Grid container justify="flex-end">
125 <Tooltip title="More options">
126 <IconButton onClick={event => this.props.onContextMenu(event, item)}>
132 contextMenuColumn: DataColumn<any> = {
136 sortDirection: SortDirection.NONE,
138 key: "context-actions",
139 render: this.renderContextMenuTrigger