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 } 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";
32 import { isExactlyOneSelected } from "store/multiselect/multiselect-actions";
33 import { PendingIcon } from "components/icon/icon";
35 export type DataColumns<I, R> = Array<DataColumn<I, R>>;
37 export enum DataTableFetchMode {
42 export interface DataTableDataProps<I> {
44 columns: DataColumns<I, any>;
45 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
46 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: I) => void;
47 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
48 onSortToggle: (column: DataColumn<I, any>) => void;
49 onFiltersChange: (filters: DataTableFilters, column: DataColumn<I, any>) => void;
50 extractKey?: (item: I) => React.Key;
52 defaultViewIcon?: IconType;
53 defaultViewMessages?: string[];
54 toggleMSToolbar: (isVisible: boolean) => void;
55 setCheckedListOnStore: (checkedList: TCheckedList) => void;
56 currentRoute?: string;
57 currentRouteUuid: string;
58 checkedList: TCheckedList;
59 selectedResourceUuid: string;
60 setSelectedUuid: (uuid: string | null) => void;
77 | "tableCellWorkflows";
79 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
84 display: "inline-block",
88 background: theme.palette.background.paper,
92 padding: theme.spacing.unit,
99 paddingLeft: '0.9rem',
100 marginRight: '0.5rem'
109 paddingLeft: '0.35rem',
110 paddingTop: '0.1rem',
111 marginLeft: '0.5rem',
118 wordWrap: "break-word",
119 paddingRight: "24px",
125 tableCellWorkflows: {
126 "&:nth-last-child(2)": {
132 paddingRight: "24px",
140 color: theme.palette.text.primary,
144 export type TCheckedList = Record<string, boolean>;
146 type DataTableState = {
151 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
153 export const DataTable = withStyles(styles)(
154 class Component<T> extends React.Component<DataTableProps<T>> {
155 state: DataTableState = {
160 componentDidMount(): void {
161 this.initializeCheckedList([]);
162 // If table is initialized loaded but empty
163 // isLoaded won't be set true by componentDidUpdate later
164 // So we set it to true here
165 if (!this.props.working) {
166 this.setState({ isLoaded: true });
170 componentDidUpdate(prevProps: Readonly<DataTableProps<T>>, prevState: DataTableState) {
171 const { items, currentRouteUuid, setCheckedListOnStore } = this.props;
172 const { isSelected } = this.state;
173 const singleSelected = isExactlyOneSelected(this.props.checkedList);
174 if (prevProps.items !== items) {
175 if (isSelected === true) this.setState({ isSelected: false });
176 if (items.length) this.initializeCheckedList(items);
177 else setCheckedListOnStore({});
179 if (prevProps.currentRoute !== this.props.currentRoute) {
180 this.initializeCheckedList([]);
182 if (singleSelected && singleSelected !== isExactlyOneSelected(prevProps.checkedList)) {
183 this.props.setSelectedUuid(singleSelected);
185 if (!singleSelected && !!currentRouteUuid && !this.isAnySelected()) {
186 this.props.setSelectedUuid(currentRouteUuid);
188 if (!singleSelected && this.isAnySelected()) {
189 this.props.setSelectedUuid(null);
191 if(prevProps.working === true && this.props.working === false) {
192 this.setState({ isLoaded: true });
194 if((this.props.items.length > 0) && !this.state.isLoaded) {
195 this.setState({ isLoaded: true });
199 componentWillUnmount(): void {
200 this.initializeCheckedList([]);
203 checkBoxColumn: DataColumn<any, any> = {
204 name: "checkBoxColumn",
207 filters: createTree(),
209 const { classes, checkedList } = this.props;
212 className={classes.clickBox}
215 this.handleSelectOne(uuid)
217 onDoubleClick={(ev) => ev.stopPropagation()}
220 data-cy={`multiselect-checkbox-${uuid}`}
223 className={classes.checkBox}
224 checked={checkedList && checkedList[uuid] ? checkedList[uuid] : false}
225 onChange={() => this.handleSelectOne(uuid)}
226 onDoubleClick={(ev) => ev.stopPropagation()}
233 multiselectOptions: DataTableMultiselectOption[] = [
234 { name: "All", fn: list => this.handleSelectAll(list) },
235 { name: "None", fn: list => this.handleSelectNone(list) },
236 { name: "Invert", fn: list => this.handleInvertSelect(list) },
239 initializeCheckedList = (uuids: any[]): void => {
240 const newCheckedList = { ...this.props.checkedList };
242 if(Object.keys(newCheckedList).length === 0){
243 for(const uuid of uuids){
244 newCheckedList[uuid] = false
248 for (const key in newCheckedList) {
249 if (!uuids.includes(key)) {
250 delete newCheckedList[key];
253 this.props.setCheckedListOnStore(newCheckedList);
256 isAllSelected = (list: TCheckedList): boolean => {
257 for (const key in list) {
258 if (list[key] === false) return false;
263 isAnySelected = (): boolean => {
264 const { checkedList } = this.props;
265 if (!Object.keys(checkedList).length) return false;
266 for (const key in checkedList) {
267 if (checkedList[key] === true) return true;
272 handleSelectOne = (uuid: string): void => {
273 const { checkedList } = this.props;
274 const newCheckedList = { ...checkedList };
275 newCheckedList[uuid] = !checkedList[uuid];
276 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
277 this.props.setCheckedListOnStore(newCheckedList);
280 handleSelectorSelect = (): void => {
281 const { checkedList } = this.props;
282 const { isSelected } = this.state;
283 isSelected ? this.handleSelectNone(checkedList) : this.handleSelectAll(checkedList);
286 handleSelectAll = (list: TCheckedList): void => {
287 if (Object.keys(list).length) {
288 const newCheckedList = { ...list };
289 for (const key in newCheckedList) {
290 newCheckedList[key] = true;
292 this.setState({ isSelected: true });
293 this.props.setCheckedListOnStore(newCheckedList);
297 handleSelectNone = (list: TCheckedList): void => {
298 const newCheckedList = { ...list };
299 for (const key in newCheckedList) {
300 newCheckedList[key] = false;
302 this.setState({ isSelected: false });
303 this.props.setCheckedListOnStore(newCheckedList);
306 handleInvertSelect = (list: TCheckedList): void => {
307 if (Object.keys(list).length) {
308 const newCheckedList = { ...list };
309 for (const key in newCheckedList) {
310 newCheckedList[key] = !list[key];
312 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
313 this.props.setCheckedListOnStore(newCheckedList);
318 const { items, classes, columns, isNotFound } = this.props;
319 const { isLoaded } = this.state;
320 if (columns.length && columns[0].name === this.checkBoxColumn.name) columns.shift();
321 columns.unshift(this.checkBoxColumn);
323 <div className={classes.root}>
324 <div className={classes.content}>
327 <TableRow>{this.mapVisibleColumns(this.renderHeadCell)}</TableRow>
329 <TableBody className={classes.tableBody}>{(isLoaded && !isNotFound) && items.map(this.renderBodyRow)}</TableBody>
331 {(!isLoaded || isNotFound || items.length === 0) && this.renderNoItemsPlaceholder(this.props.columns)}
337 renderNoItemsPlaceholder = (columns: DataColumns<T, any>) => {
338 const { isLoaded } = this.state;
339 const { working, isNotFound } = this.props;
340 const dirty = columns.some(column => getTreeDirty("")(column.filters));
341 if (isNotFound && isLoaded) {
343 <DataTableDefaultView
344 icon={this.props.defaultViewIcon}
345 messages={["No items found"]}
349 if (isLoaded === false || working === true) {
351 <DataTableDefaultView
353 messages={["Loading data, please wait"]}
357 // isLoaded && !working && !isNotFound
359 <DataTableDefaultView
360 icon={this.props.defaultViewIcon}
361 messages={this.props.defaultViewMessages}
362 filtersApplied={dirty}
368 renderHeadCell = (column: DataColumn<T, any>, index: number) => {
369 const { name, key, renderHeader, filters, sort } = column;
370 const { onSortToggle, onFiltersChange, classes, checkedList } = this.props;
371 const { isSelected } = this.state;
372 return column.name === "checkBoxColumn" ? (
375 className={classes.checkBoxCell}>
376 <div className={classes.checkBoxHead}>
377 <Tooltip title={this.state.isSelected ? "Deselect all" : "Select all"}>
380 className={classes.checkBox}
382 disabled={!this.props.items.length}
383 onChange={this.handleSelectorSelect}></input>
385 <DataTableMultiselectPopover
387 disabled={!this.props.items.length}
388 options={this.multiselectOptions}
389 checkedList={checkedList}></DataTableMultiselectPopover>
394 className={index === 1 ? classes.firstTableCell : classes.tableCell}
398 ) : countNodes(filters) > 0 ? (
399 <DataTableFiltersPopover
400 name={`${name} filters`}
401 mutuallyExclusive={column.mutuallyExclusiveFilters}
402 onChange={filters => onFiltersChange && onFiltersChange(filters, column)}
405 </DataTableFiltersPopover>
408 active={sort.direction !== SortDirection.NONE}
409 direction={sort.direction !== SortDirection.NONE ? sort.direction : undefined}
410 IconComponent={this.ArrowIcon}
412 onClick={() => onSortToggle && onSortToggle(column)}>
422 ArrowIcon = ({ className, ...props }: SvgIconProps) => (
425 className={this.props.classes.arrowButton}
429 className={classnames(className, this.props.classes.arrow)}
434 renderBodyRow = (item: any, index: number) => {
435 const { onRowClick, onRowDoubleClick, extractKey, classes, selectedResourceUuid, currentRoute } = this.props;
438 data-cy={'data-table-row'}
440 key={extractKey ? extractKey(item) : index}
441 onClick={event => onRowClick && onRowClick(event, item)}
442 onContextMenu={this.handleRowContextMenu(item)}
443 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}
444 selected={item === selectedResourceUuid}>
445 {this.mapVisibleColumns((column, index) => (
447 key={column.key || index}
449 currentRoute === "/workflows"
450 ? classes.tableCellWorkflows
452 ? classes.checkBoxCell
453 : `${classes.tableCell} ${index === 1 ? classes.firstTableCell : ""}`
455 {column.render(item)}
462 mapVisibleColumns = (fn: (column: DataColumn<T, any>, index: number) => React.ReactElement<any>) => {
463 return this.props.columns.filter(column => column.selected).map(fn);
466 handleRowContextMenu = (item: T) => (event: React.MouseEvent<HTMLElement>) => this.props.onContextMenu(event, item);