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 } 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";
14 import { DefaultView } from '../default-view/default-view';
15 import { IconType } from '../icon/icon';
17 type CssRules = 'searchBox' | "toolbar" | 'defaultRoot' | 'defaultMessage' | 'defaultIcon';
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
21 paddingBottom: theme.spacing.unit * 2
24 paddingTop: theme.spacing.unit * 2
31 transform: 'translate(-50%, -50%)'
41 interface DataExplorerDataProps<T> {
43 itemsAvailable: number;
44 columns: DataColumns<T>;
47 rowsPerPageOptions: number[];
49 defaultIcon: IconType;
50 defaultMessages: string[];
51 contextMenuColumn: 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: DataTableFilterItem[], 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, extractKey,
80 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
81 items, itemsAvailable, onRowClick, onRowDoubleClick, defaultIcon, defaultMessages, classes
84 { items.length > 0 ? (
86 <Toolbar className={classes.toolbar}>
87 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
88 <div className={classes.searchBox}>
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 <Grid container justify="flex-end">
110 count={itemsAvailable}
111 rowsPerPage={rowsPerPage}
112 rowsPerPageOptions={rowsPerPageOptions}
113 page={this.props.page}
114 onChangePage={this.changePage}
115 onChangeRowsPerPage={this.changeRowsPerPage}
122 classRoot={classes.defaultRoot}
124 classIcon={classes.defaultIcon}
125 messages={defaultMessages}
126 classMessage={classes.defaultMessage} />
131 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
132 this.props.onChangePage(page);
135 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
136 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
139 renderContextMenuTrigger = (item: T) =>
140 <Grid container justify="flex-end">
141 <IconButton onClick={event => this.props.onContextMenu(event, item)}>
146 contextMenuColumn: DataColumn<any> = {
150 sortDirection: SortDirection.NONE,
152 key: "context-actions",
153 render: this.renderContextMenuTrigger