//
// SPDX-License-Identifier: AGPL-3.0
-export interface DataColumn<T> {
+import React from "react";
+import { DataTableFilters } from "../data-table-filters/data-table-filters-tree";
+import { createTree } from 'models/tree';
+
+/**
+ *
+ * @template I Type of dataexplorer item reference
+ * @template R Type of resource to use to restrict values of column sort.field
+ */
+export interface DataColumn<I, R> {
+ key?: React.Key;
name: string;
selected: boolean;
- configurable?: boolean;
- key?: React.Key;
- render: (item: T) => React.ReactElement<void>;
- renderHeader?: () => React.ReactElement<void> | null;
+ configurable: boolean;
+
+ /**
+ * If set to true, filters on this column will be displayed in a
+ * radio group and only one filter can be selected at a time.
+ */
+ mutuallyExclusiveFilters?: boolean;
+ sort?: {direction: SortDirection, field: keyof R};
+ filters: DataTableFilters;
+ render: (item: I) => React.ReactElement<any>;
+ renderHeader?: () => React.ReactElement<any>;
}
-export const isColumnConfigurable = <T>(column: DataColumn<T>) => {
- return column.configurable === undefined || column.configurable;
-};
\ No newline at end of file
+export enum SortDirection {
+ ASC = "asc",
+ DESC = "desc",
+ NONE = "none"
+}
+
+export const toggleSortDirection = <I, R>(column: DataColumn<I, R>): DataColumn<I, R> => {
+ return column.sort
+ ? column.sort.direction === SortDirection.ASC
+ ? { ...column, sort: {...column.sort, direction: SortDirection.DESC} }
+ : { ...column, sort: {...column.sort, direction: SortDirection.ASC} }
+ : column;
+};
+
+export const resetSortDirection = <I, R>(column: DataColumn<I, R>): DataColumn<I, R> => {
+ return column.sort ? { ...column, sort: {...column.sort, direction: SortDirection.NONE} } : column;
+};
+
+export const createDataColumn = <I, R>(dataColumn: Partial<DataColumn<I, R>>): DataColumn<I, R> => ({
+ key: '',
+ name: '',
+ selected: true,
+ configurable: true,
+ filters: createTree(),
+ render: () => React.createElement('span'),
+ ...dataColumn,
+});