Skip to content

Commit

Permalink
FEATURE: RAIL-1129 - Refactoring pivot headers and menus
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Horak committed Sep 25, 2018
1 parent 2d63112 commit 4a3c73b
Show file tree
Hide file tree
Showing 12 changed files with 628 additions and 228 deletions.
27 changes: 13 additions & 14 deletions src/components/core/PivotTable.tsx
Expand Up @@ -28,7 +28,8 @@ import {
ID_SEPARATOR,
getIdsFromUri,
ROW_TOTAL,
assortDimensionHeaders
assortDimensionHeaders,
getParsedFields
} from '../../helpers/agGrid';

import { LoadingComponent } from '../simple/LoadingComponent';
Expand All @@ -44,7 +45,7 @@ import { ICommonChartProps } from './base/BaseChart';
import { IDataSource } from '../../interfaces/DataSource';
import { IPivotTableConfig } from '../../interfaces/Table';
import { BaseVisualization } from './base/BaseVisualization';
import PivotHeader from './PivotTableHeader';
import ColumnHeader from './pivotTable/ColumnHeader';

import { getCellClassNames, getMeasureCellFormattedValue, getMeasureCellStyle } from '../../helpers/tableCell';

Expand All @@ -70,6 +71,7 @@ import InjectedIntl = ReactIntl.InjectedIntl;
import { AVAILABLE_TOTALS } from '../visualizations/table/totals/utils';

import '../../../styles/scss/pivotTable.scss';
import ColumnGroupHeader from './pivotTable/ColumnGroupHeader';

export interface IPivotTableProps extends ICommonChartProps {
resultSpec?: AFM.IResultSpec;
Expand Down Expand Up @@ -169,13 +171,6 @@ export const getTreeLeaves = (tree: any, getChildren = (node: any) => node && no
return leaves;
};

export const getParsedFields = (colId: string): string[][] => {
// supported colIds are 'a_2009', 'a_2009_4-a_2071_12', 'a_2009_4-a_2071_12-m_3'
return colId
.split(FIELD_SEPARATOR)
.map((field: string) => (field.split(ID_SEPARATOR)));
};

export const getSortItemByColId = (
execution: Execution.IExecutionResponses,
colId: string,
Expand Down Expand Up @@ -239,14 +234,14 @@ export const getSortItemByColId = (

export interface ISortModelItem {
colId: string;
sort: string;
sort: AFM.SortDirection;
}

export const getSortsFromModel = (
sortModel: ISortModelItem[], // AgGrid has any, but we can do better
execution: Execution.IExecutionResponses
) => {
return sortModel.map((sortModelItem: any) => {
return sortModel.map((sortModelItem: ISortModelItem) => {
const { colId, sort } = sortModelItem;
const sortHeader = getSortItemByColId(execution, colId, sort);
invariant(sortHeader, `unable to find sort item by field ${colId}`);
Expand Down Expand Up @@ -544,13 +539,18 @@ export class PivotTableInner extends

defaultColDef: {
cellClass: this.getCellClass(null),
headerComponentFramework: ColumnHeader as any,
headerComponentParams: {
enableMenu: false
}
},
defaultColGroupDef: {
headerClass: this.getHeaderClass(null),
children: []
children: [],
headerGroupComponentFramework: ColumnGroupHeader as any,
headerGroupComponentParams: {
enableMenu: false
}
},
onCellClicked: this.cellClicked,

Expand Down Expand Up @@ -629,8 +629,7 @@ export class PivotTableInner extends
// Custom renderers
frameworkComponents: {
// any is needed here because of incompatible types with AgGridReact types
loadingRenderer: RowLoadingElement as any, // loading indicator
agColumnHeader: PivotHeader as any
loadingRenderer: RowLoadingElement as any // loading indicator
},

// Custom CSS classes
Expand Down
139 changes: 0 additions & 139 deletions src/components/core/PivotTableHeader.tsx

This file was deleted.

39 changes: 39 additions & 0 deletions src/components/core/pivotTable/ColumnGroupHeader.tsx
@@ -0,0 +1,39 @@
// (C) 2007-2018 GoodData Corporation
import * as React from 'react';

import { IHeaderGroupParams } from 'ag-grid';
import { IHeaderReactComp } from 'ag-grid-react/lib/interfaces';

import HeaderCell, { ALIGN_LEFT } from './HeaderCell';

export interface IParams extends IHeaderGroupParams {
enableMenu: boolean;
}

export default class ColumnGroupHeader
extends React.Component<IParams>
implements IHeaderReactComp {

public onMenuClick = () => {
// tslint:disable-next-line no-console
console.log('menu clicked');
}

public render() {
const { enableMenu } = this.props;
const columnGroupDef = this.props.columnGroup.getColGroupDef();
const parent = this.props.columnGroup.getParent();
const showMenu = enableMenu && !!(parent && columnGroupDef.headerName);

return (
<HeaderCell
displayText={this.props.displayName}
enableMenu={showMenu}
enableSorting={false}
menuPosition={ALIGN_LEFT}
textAlign={ALIGN_LEFT}
onMenuClick={this.onMenuClick}
/>
);
}
}
93 changes: 93 additions & 0 deletions src/components/core/pivotTable/ColumnHeader.tsx
@@ -0,0 +1,93 @@
// (C) 2007-2018 GoodData Corporation
import * as React from 'react';
import * as PropTypes from 'prop-types';
import { IHeaderParams } from 'ag-grid';
import { AFM } from '@gooddata/typings';

import { getParsedFields } from '../../../helpers/agGrid';
import { IHeaderReactComp } from 'ag-grid-react/lib/interfaces';
import HeaderCell, { ALIGN_LEFT, ALIGN_RIGHT } from './HeaderCell';

export interface IPivotHeaderState {
sorting?: AFM.SortDirection;
}

export const ASC: AFM.SortDirection = 'asc';
export const DESC: AFM.SortDirection = 'desc';
export const ATTRIBUTE_FIELD_TYPE = 'a';

class PivotHeader extends React.Component<IHeaderParams, IPivotHeaderState> implements IHeaderReactComp {
public static propTypes = {
enableMenu: PropTypes.bool,
enableSorting: PropTypes.bool,
displayName: PropTypes.string,
column: PropTypes.any,
reactContainer: PropTypes.any,
showColumnMenu: PropTypes.func,
setSort: PropTypes.func
};

public state: IPivotHeaderState = {
sorting: null
};

public componentWillMount() {
this.props.column.addEventListener('sortChanged', this.getCurrentSortDirection);
this.setState({
sorting: this.props.column.getSort() as AFM.SortDirection
});
}

public componentWillUnmount() {
this.props.column.removeEventListener('sortChanged', this.getCurrentSortDirection);
}

public getCurrentSortDirection = () => {
const currentSort: AFM.SortDirection = this.props.column.getSort() as AFM.SortDirection;
this.setState({
sorting: currentSort
});
}

public getDefaultSortDirection(): AFM.SortDirection {
return (this.getFieldType() === ATTRIBUTE_FIELD_TYPE) ? ASC : DESC;
}

public onMenuClick = () => {
// tslint:disable-next-line no-console
console.log('menu clicked');
}

public onSortRequested = (sortDir: AFM.SortDirection) => {
const multiSort = false; // Enable support for multisort with CMD key with 'event.shiftKey';
this.props.setSort(sortDir, multiSort);
}

public render() {
const { displayName, enableSorting, enableMenu } = this.props;
const textAlign = this.getFieldType() === ATTRIBUTE_FIELD_TYPE ? ALIGN_LEFT : ALIGN_RIGHT;

return (
<HeaderCell
textAlign={textAlign}
displayText={displayName}
enableMenu={enableMenu}
enableSorting={enableSorting}
sortDirection={this.state.sorting}
defaultSortDirection={this.getDefaultSortDirection()}
onSortClick={this.onSortRequested}
onMenuClick={this.onMenuClick}
/>
);
}

private getFieldType() {
const colId = this.props.column.getColId();
const fields = getParsedFields(colId);
const [lastFieldType] = fields[fields.length - 1];

return lastFieldType;
}
}

export default PivotHeader;

0 comments on commit 4a3c73b

Please sign in to comment.