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, Button } from '@material-ui/core';
7 import { ColumnSelector } from "~/components/column-selector/column-selector";
8 import { DataTable, DataColumns } from "~/components/data-table/data-table";
9 import { DataColumn, SortDirection } 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';
17 type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
21 paddingBottom: theme.spacing.unit * 2
24 paddingTop: theme.spacing.unit * 2
37 interface DataExplorerDataProps<T> {
39 itemsAvailable: number;
40 columns: DataColumns<T>;
43 rowsPerPageOptions: number[];
45 contextMenuColumn: boolean;
46 dataTableDefaultView?: React.ReactNode;
48 hideColumnSelector?: boolean;
49 paperProps?: PaperProps;
50 actions?: React.ReactNode;
51 hideSearchInput?: boolean;
54 interface DataExplorerActionProps<T> {
55 onSetColumns: (columns: DataColumns<T>) => void;
56 onSearch: (value: string) => void;
57 onRowClick: (item: T) => void;
58 onRowDoubleClick: (item: T) => void;
59 onColumnToggle: (column: DataColumn<T>) => void;
60 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
61 onSortToggle: (column: DataColumn<T>) => void;
62 onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
63 onChangePage: (page: number) => void;
64 onChangeRowsPerPage: (rowsPerPage: number) => void;
65 extractKey?: (item: T) => React.Key;
68 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
70 export const DataExplorer = withStyles(styles)(
71 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
73 if (this.props.onSetColumns) {
74 this.props.onSetColumns(this.props.columns);
79 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
80 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
81 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
82 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput
84 return <Paper className={classes.root} {...paperProps}>
85 <Toolbar className={classes.toolbar}>
86 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
87 {!hideSearchInput && <div className={classes.searchBox}>
90 onSearch={onSearch} />
93 {!hideColumnSelector && <ColumnSelector
95 onColumnToggle={onColumnToggle} />}
99 columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
101 onRowClick={(_, item: T) => onRowClick(item)}
102 onContextMenu={onContextMenu}
103 onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
104 onFiltersChange={onFiltersChange}
105 onSortToggle={onSortToggle}
106 extractKey={extractKey}
108 defaultView={dataTableDefaultView}
110 <Toolbar className={classes.footer}>
111 <Grid container justify="flex-end">
113 count={itemsAvailable}
114 rowsPerPage={rowsPerPage}
115 rowsPerPageOptions={rowsPerPageOptions}
116 page={this.props.page}
117 onChangePage={this.changePage}
118 onChangeRowsPerPage={this.changeRowsPerPage}
125 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
126 this.props.onChangePage(page);
129 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
130 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
133 renderContextMenuTrigger = (item: T) =>
134 <Grid container justify="center">
135 <Tooltip title="More options" disableFocusListener>
136 <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
142 contextMenuColumn: DataColumn<any> = {
146 sortDirection: SortDirection.NONE,
147 filters: createTree(),
148 key: "context-actions",
149 render: this.renderContextMenuTrigger