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 { ColumnSelector } from "../column-selector/column-selector";
8 import { DataTable, DataColumns } from "../data-table/data-table";
9 import { DataColumn, SortDirection } from "../data-table/data-column";
10 import { SearchInput } from '../search-input/search-input';
11 import { ArvadosTheme } from "~/common/custom-theme";
12 import { createTree } from '~/models/tree';
13 import { DataTableFilters } from '../data-table-filters/data-table-filters-tree';
14 import { MoreOptionsIcon } from '~/components/icon/icon';
16 type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
18 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
20 paddingBottom: theme.spacing.unit * 2
23 paddingTop: theme.spacing.unit * 2
36 interface DataExplorerDataProps<T> {
38 itemsAvailable: number;
39 columns: DataColumns<T>;
42 rowsPerPageOptions: number[];
44 contextMenuColumn: boolean;
45 dataTableDefaultView?: React.ReactNode;
49 interface DataExplorerActionProps<T> {
50 onSetColumns: (columns: DataColumns<T>) => void;
51 onSearch: (value: string) => void;
52 onRowClick: (item: T) => void;
53 onRowDoubleClick: (item: T) => void;
54 onColumnToggle: (column: DataColumn<T>) => void;
55 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
56 onSortToggle: (column: DataColumn<T>) => void;
57 onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
58 onChangePage: (page: number) => void;
59 onChangeRowsPerPage: (rowsPerPage: number) => void;
60 extractKey?: (item: T) => React.Key;
63 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
65 export const DataExplorer = withStyles(styles)(
66 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
68 if (this.props.onSetColumns) {
69 this.props.onSetColumns(this.props.columns);
74 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
75 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
76 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
79 return <Paper className={classes.root}>
80 <Toolbar className={classes.toolbar}>
81 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
82 <div className={classes.searchBox}>
85 onSearch={onSearch} />
89 onColumnToggle={onColumnToggle} />
93 columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
95 onRowClick={(_, item: T) => onRowClick(item)}
96 onContextMenu={onContextMenu}
97 onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
98 onFiltersChange={onFiltersChange}
99 onSortToggle={onSortToggle}
100 extractKey={extractKey}
102 defaultView={dataTableDefaultView}
104 <Toolbar className={classes.footer}>
105 <Grid container justify="flex-end">
107 count={itemsAvailable}
108 rowsPerPage={rowsPerPage}
109 rowsPerPageOptions={rowsPerPageOptions}
110 page={this.props.page}
111 onChangePage={this.changePage}
112 onChangeRowsPerPage={this.changeRowsPerPage}
119 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
120 this.props.onChangePage(page);
123 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
124 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
127 renderContextMenuTrigger = (item: T) =>
128 <Grid container justify="center">
129 <Tooltip title="More options" disableFocusListener>
130 <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
136 contextMenuColumn: DataColumn<any> = {
140 sortDirection: SortDirection.NONE,
141 filters: createTree(),
142 key: "context-actions",
143 render: this.renderContextMenuTrigger