1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from "react";
19 } from "@material-ui/core";
20 import classnames from "classnames";
21 import { DataColumn, SortDirection } from "./data-column";
22 import { DataTableDefaultView } from "../data-table-default-view/data-table-default-view";
23 import { DataTableFilters } from "../data-table-filters/data-table-filters-tree";
24 import { DataTableMultiselectPopover } from "../data-table-multiselect-popover/data-table-multiselect-popover";
25 import { DataTableFiltersPopover } from "../data-table-filters/data-table-filters-popover";
26 import { countNodes, getTreeDirty } from "models/tree";
27 import { IconType, PendingIcon } from "components/icon/icon";
28 import { SvgIconProps } from "@material-ui/core/SvgIcon";
29 import ArrowDownwardIcon from "@material-ui/icons/ArrowDownward";
30 import { createTree } from "models/tree";
31 import { DataTableMultiselectOption } from "../data-table-multiselect-popover/data-table-multiselect-popover";
33 export type DataColumns<I, R> = Array<DataColumn<I, R>>;
35 export enum DataTableFetchMode {
40 export interface DataTableDataProps<I> {
42 columns: DataColumns<I, any>;
43 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
44 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: I) => void;
45 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
46 onSortToggle: (column: DataColumn<I, any>) => void;
47 onFiltersChange: (filters: DataTableFilters, column: DataColumn<I, any>) => void;
48 extractKey?: (item: I) => React.Key;
50 defaultViewIcon?: IconType;
51 defaultViewMessages?: string[];
52 currentItemUuid?: string;
53 currentRoute?: string;
54 toggleMSToolbar: (isVisible: boolean) => void;
55 setCheckedListOnStore: (checkedList: TCheckedList) => void;
56 checkedList: TCheckedList;
71 | "tableCellWorkflows"
74 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
79 display: "inline-block",
83 background: theme.palette.background.paper,
92 padding: theme.spacing.unit,
106 wordWrap: "break-word",
107 paddingRight: "24px",
113 tableCellWorkflows: {
114 "&:nth-last-child(2)": {
120 paddingRight: "24px",
128 color: theme.palette.text.primary,
132 export type TCheckedList = Record<string, boolean>;
134 type DataTableState = {
138 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
140 export const DataTable = withStyles(styles)(
141 class Component<T> extends React.Component<DataTableProps<T>> {
142 state: DataTableState = {
146 componentDidMount(): void {
147 this.initializeCheckedList(this.props.items);
150 componentDidUpdate(prevProps: Readonly<DataTableProps<T>>, prevState: DataTableState) {
151 const { items, setCheckedListOnStore } = this.props;
152 const { isSelected } = this.state;
153 if (prevProps.items !== items) {
154 if (isSelected === true) this.setState({ isSelected: false });
155 if (items.length) this.initializeCheckedList(items);
156 else setCheckedListOnStore({});
160 checkBoxColumn: DataColumn<any, any> = {
161 name: "checkBoxColumn",
164 filters: createTree(),
166 const { classes, checkedList } = this.props;
171 className={classes.checkBox}
172 checked={checkedList && checkedList[uuid] ? checkedList[uuid] : false}
173 onChange={() => this.handleSelectOne(uuid)}
174 onDoubleClick={ev => ev.stopPropagation()}></input>
179 multiselectOptions: DataTableMultiselectOption[] = [
180 { name: "All", fn: list => this.handleSelectAll(list) },
181 { name: "None", fn: list => this.handleSelectNone(list) },
182 { name: "Invert", fn: list => this.handleInvertSelect(list) },
185 initializeCheckedList = (uuids: any[]): void => {
186 const newCheckedList = { ...this.props.checkedList };
188 uuids.forEach(uuid => {
189 if (!newCheckedList.hasOwnProperty(uuid)) {
190 newCheckedList[uuid] = false;
193 for (const key in newCheckedList) {
194 if (!uuids.includes(key)) {
195 delete newCheckedList[key];
198 this.props.setCheckedListOnStore(newCheckedList);
201 isAllSelected = (list: TCheckedList): boolean => {
202 for (const key in list) {
203 if (list[key] === false) return false;
208 isAnySelected = (): boolean => {
209 const { checkedList } = this.props;
210 if (!Object.keys(checkedList).length) return false;
211 for (const key in checkedList) {
212 if (checkedList[key] === true) return true;
217 handleSelectOne = (uuid: string): void => {
218 const { checkedList } = this.props;
219 const newCheckedList = { ...checkedList };
220 newCheckedList[uuid] = !checkedList[uuid];
221 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
222 this.props.setCheckedListOnStore(newCheckedList);
225 handleSelectorSelect = (): void => {
226 const { checkedList } = this.props;
227 const { isSelected } = this.state;
228 isSelected ? this.handleSelectNone(checkedList) : this.handleSelectAll(checkedList);
231 handleSelectAll = (list: TCheckedList): void => {
232 if (Object.keys(list).length) {
233 const newCheckedList = { ...list };
234 for (const key in newCheckedList) {
235 newCheckedList[key] = true;
237 this.setState({ isSelected: true });
238 this.props.setCheckedListOnStore(newCheckedList);
242 handleSelectNone = (list: TCheckedList): void => {
243 const newCheckedList = { ...list };
244 for (const key in newCheckedList) {
245 newCheckedList[key] = false;
247 this.setState({ isSelected: false });
248 this.props.setCheckedListOnStore(newCheckedList);
251 handleInvertSelect = (list: TCheckedList): void => {
252 if (Object.keys(list).length) {
253 const newCheckedList = { ...list };
254 for (const key in newCheckedList) {
255 newCheckedList[key] = !list[key];
257 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
258 this.props.setCheckedListOnStore(newCheckedList);
263 const { items, classes, working, columns } = this.props;
264 if (columns[0].name === this.checkBoxColumn.name) columns.shift();
265 columns.unshift(this.checkBoxColumn);
267 <div className={classes.root}>
268 <div className={classes.content}>
271 <TableRow>{this.mapVisibleColumns(this.renderHeadCell)}</TableRow>
273 <TableBody className={classes.tableBody}>{!working && items.map(this.renderBodyRow)}</TableBody>
276 <div className={classes.loader}>
277 <DataTableDefaultView
279 messages={["Loading data, please wait."]}
283 {items.length === 0 && !working && this.renderNoItemsPlaceholder(this.props.columns)}
289 renderNoItemsPlaceholder = (columns: DataColumns<T, any>) => {
290 const dirty = columns.some(column => getTreeDirty("")(column.filters));
292 <DataTableDefaultView
293 icon={this.props.defaultViewIcon}
294 messages={this.props.defaultViewMessages}
295 filtersApplied={dirty}
300 renderHeadCell = (column: DataColumn<T, any>, index: number) => {
301 const { name, key, renderHeader, filters, sort } = column;
302 const { onSortToggle, onFiltersChange, classes, checkedList } = this.props;
303 const { isSelected } = this.state;
304 return column.name === "checkBoxColumn" ? (
307 className={classes.checkBoxCell}>
308 <div className={classes.checkBoxHead}>
309 <Tooltip title={this.state.isSelected ? "Deselect All" : "Select All"}>
312 className={classes.checkBox}
314 disabled={!this.props.items.length}
315 onChange={this.handleSelectorSelect}></input>
317 <DataTableMultiselectPopover
319 disabled={!this.props.items.length}
320 options={this.multiselectOptions}
321 checkedList={checkedList}></DataTableMultiselectPopover>
326 className={index === 1 ? classes.firstTableCell : classes.tableCell}
330 ) : countNodes(filters) > 0 ? (
331 <DataTableFiltersPopover
332 name={`${name} filters`}
333 mutuallyExclusive={column.mutuallyExclusiveFilters}
334 onChange={filters => onFiltersChange && onFiltersChange(filters, column)}
337 </DataTableFiltersPopover>
340 active={sort.direction !== SortDirection.NONE}
341 direction={sort.direction !== SortDirection.NONE ? sort.direction : undefined}
342 IconComponent={this.ArrowIcon}
344 onClick={() => onSortToggle && onSortToggle(column)}>
354 ArrowIcon = ({ className, ...props }: SvgIconProps) => (
357 className={this.props.classes.arrowButton}
361 className={classnames(className, this.props.classes.arrow)}
366 renderBodyRow = (item: any, index: number) => {
367 const { onRowClick, onRowDoubleClick, extractKey, classes, currentItemUuid, currentRoute } = this.props;
371 key={extractKey ? extractKey(item) : index}
372 onClick={event => onRowClick && onRowClick(event, item)}
373 onContextMenu={this.handleRowContextMenu(item)}
374 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}
375 selected={item === currentItemUuid}>
376 {this.mapVisibleColumns((column, index) => (
378 key={column.key || index}
380 currentRoute === "/workflows"
381 ? classes.tableCellWorkflows
383 ? classes.checkBoxCell
384 : `${classes.tableCell} ${index === 1 ? classes.firstTableCell : ""}`
386 {column.render(item)}
393 mapVisibleColumns = (fn: (column: DataColumn<T, any>, index: number) => React.ReactElement<any>) => {
394 return this.props.columns.filter(column => column.selected).map(fn);
397 handleRowContextMenu = (item: T) => (event: React.MouseEvent<HTMLElement>) => this.props.onContextMenu(event, item);