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;
55 interface DataExplorerActionProps<T> {
56 onSetColumns: (columns: DataColumns<T>) => void;
57 onSearch: (value: string) => void;
58 onRowClick: (item: T) => void;
59 onRowDoubleClick: (item: T) => void;
60 onColumnToggle: (column: DataColumn<T>) => void;
61 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
62 onSortToggle: (column: DataColumn<T>) => void;
63 onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
64 onChangePage: (page: number) => void;
65 onChangeRowsPerPage: (rowsPerPage: number) => void;
66 extractKey?: (item: T) => React.Key;
69 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
71 export const DataExplorer = withStyles(styles)(
72 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
74 if (this.props.onSetColumns) {
75 this.props.onSetColumns(this.props.columns);
80 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
81 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
82 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
83 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput,
86 return <Paper className={classes.root} {...paperProps} key={paperKey}>
87 <Toolbar className={classes.toolbar}>
88 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
89 {!hideSearchInput && <div className={classes.searchBox}>
92 onSearch={onSearch} />
95 {!hideColumnSelector && <ColumnSelector
97 onColumnToggle={onColumnToggle} />}
101 columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
103 onRowClick={(_, item: T) => onRowClick(item)}
104 onContextMenu={onContextMenu}
105 onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
106 onFiltersChange={onFiltersChange}
107 onSortToggle={onSortToggle}
108 extractKey={extractKey}
110 defaultView={dataTableDefaultView} />
111 <Toolbar className={classes.footer}>
112 <Grid container justify="flex-end">
114 count={itemsAvailable}
115 rowsPerPage={rowsPerPage}
116 rowsPerPageOptions={rowsPerPageOptions}
117 page={this.props.page}
118 onChangePage={this.changePage}
119 onChangeRowsPerPage={this.changeRowsPerPage}
126 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
127 this.props.onChangePage(page);
130 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
131 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
134 renderContextMenuTrigger = (item: T) =>
135 <Grid container justify="center">
136 <Tooltip title="More options" disableFocusListener>
137 <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
143 contextMenuColumn: DataColumn<any> = {
147 sortDirection: SortDirection.NONE,
148 filters: createTree(),
149 key: "context-actions",
150 render: this.renderContextMenuTrigger