Skip to content

Commit

Permalink
refactor: 去除多余的 TotalsStatus,只保留 TotalStatus (#2623)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo authored Mar 25, 2024
1 parent afeee6e commit 4d0dfc9
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,9 @@ describe('Pivot Dataset Total Test', () => {

test('should get correct total cell data when totals calculated by calcFunc and Existential dimension grouping', () => {
const totalStatus = {
isRowTotal: true,
isColTotal: true,
isRowGrandTotal: true,
isRowSubTotal: true,
isColGrandTotal: true,
isColSubTotal: true,
};

Expand Down Expand Up @@ -713,63 +713,63 @@ describe('Pivot Dataset Total Test', () => {

test('should get correct total status', () => {
const {
isRowTotal: isRowTotal1,
isRowGrandTotal: isRowGrandTotal1,
isRowSubTotal: isRowSubTotal1,
isColTotal: isColTotal1,
isColGrandTotal: isColGrandTotal1,
isColSubTotal: isColSubTotal1,
} = dataSet.getTotalStatus({
[EXTRA_FIELD]: 'number',
});

expect(isRowTotal1).toBeTrue();
expect(isRowGrandTotal1).toBeTrue();
expect(isRowSubTotal1).toBeFalse();
expect(isColTotal1).toBeTrue();
expect(isColGrandTotal1).toBeTrue();
expect(isColSubTotal1).toBeFalse();

const {
isRowTotal: isRowTotal2,
isRowGrandTotal: isRowGrandTotal2,
isRowSubTotal: isRowSubTotal2,
isColTotal: isColTotal2,
isColGrandTotal: isColGrandTotal2,
isColSubTotal: isColSubTotal2,
} = dataSet.getTotalStatus({
type: '家具',
[EXTRA_FIELD]: 'number',
});

expect(isRowTotal2).toBeTrue();
expect(isRowGrandTotal2).toBeTrue();
expect(isRowSubTotal2).toBeFalse();
expect(isColTotal2).toBeFalse();
expect(isColGrandTotal2).toBeFalse();
expect(isColSubTotal2).toBeTrue();

const {
isRowTotal: isRowTotal3,
isRowGrandTotal: isRowGrandTotal3,
isRowSubTotal: isRowSubTotal3,
isColTotal: isColTotal3,
isColGrandTotal: isColGrandTotal3,
isColSubTotal: isColSubTotal3,
} = dataSet.getTotalStatus({
province: '浙江',
[EXTRA_FIELD]: 'number',
});

expect(isRowTotal3).toBeFalse();
expect(isRowGrandTotal3).toBeFalse();
expect(isRowSubTotal3).toBeTrue();
expect(isColTotal3).toBeTrue();
expect(isColGrandTotal3).toBeTrue();
expect(isColSubTotal3).toBeFalse();

const {
isRowTotal: isRowTotal4,
isRowGrandTotal: isRowGrandTotal4,
isRowSubTotal: isRowSubTotal4,
isColTotal: isColTotal4,
isColGrandTotal: isColGrandTotal4,
isColSubTotal: isColSubTotal4,
} = dataSet.getTotalStatus({
province: '浙江',
type: '家具',
[EXTRA_FIELD]: 'number',
});

expect(isRowTotal4).toBeFalse();
expect(isRowGrandTotal4).toBeFalse();
expect(isRowSubTotal4).toBeTrue();
expect(isColTotal4).toBeFalse();
expect(isColGrandTotal4).toBeFalse();
expect(isColSubTotal4).toBeTrue();
});
});
Expand Down
24 changes: 12 additions & 12 deletions packages/s2-core/__tests__/unit/utils/data-set-operate-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ describe('Data Set Operate Test', () => {
expect(
getAggregationAndCalcFuncByQuery(
{
isRowTotal: true,
isRowGrandTotal: true,
isRowSubTotal: false,
isColTotal: false,
isColGrandTotal: false,
isColSubTotal: false,
},
totalsOptions,
Expand All @@ -102,9 +102,9 @@ describe('Data Set Operate Test', () => {
expect(
getAggregationAndCalcFuncByQuery(
{
isRowTotal: true,
isRowGrandTotal: true,
isRowSubTotal: false,
isColTotal: true,
isColGrandTotal: true,
isColSubTotal: false,
},
totalsOptions,
Expand All @@ -116,9 +116,9 @@ describe('Data Set Operate Test', () => {
expect(
getAggregationAndCalcFuncByQuery(
{
isRowTotal: true,
isRowGrandTotal: true,
isRowSubTotal: false,
isColTotal: false,
isColGrandTotal: false,
isColSubTotal: true,
},
totalsOptions,
Expand All @@ -133,9 +133,9 @@ describe('Data Set Operate Test', () => {
expect(
getAggregationAndCalcFuncByQuery(
{
isRowTotal: false,
isRowGrandTotal: false,
isRowSubTotal: true,
isColTotal: false,
isColGrandTotal: false,
isColSubTotal: false,
},
totalsOptions,
Expand All @@ -150,9 +150,9 @@ describe('Data Set Operate Test', () => {
expect(
getAggregationAndCalcFuncByQuery(
{
isRowTotal: false,
isRowGrandTotal: false,
isRowSubTotal: true,
isColTotal: true,
isColGrandTotal: true,
isColSubTotal: false,
},
totalsOptions,
Expand All @@ -167,9 +167,9 @@ describe('Data Set Operate Test', () => {
expect(
getAggregationAndCalcFuncByQuery(
{
isRowTotal: false,
isRowGrandTotal: false,
isRowSubTotal: true,
isColTotal: false,
isColGrandTotal: false,
isColSubTotal: true,
},
totalsOptions,
Expand Down
7 changes: 0 additions & 7 deletions packages/s2-core/src/common/interface/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,6 @@ export interface Fields extends BaseFields {
customValueOrder?: number;
}

export interface TotalsStatus {
isRowTotal: boolean;
isRowSubTotal: boolean;
isColTotal: boolean;
isColSubTotal: boolean;
}

export enum Aggregation {
SUM = 'SUM',
MIN = 'MIN',
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/src/data-set/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ export interface CheckAccordQueryParams {
}

export interface TotalStatus {
isRowTotal: boolean;
isRowGrandTotal: boolean;
isRowSubTotal: boolean;
isColTotal: boolean;
isColGrandTotal: boolean;
isColSubTotal: boolean;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export class PivotDataSet extends BaseDataSet {
const calcAction = calcActionByType[aggregation!];

// 前端计算汇总值
if (calcAction || !!calcFunc) {
if (calcAction || calcFunc) {
const data = this.getCellMultiData({
query,
queryType: QueryDataType.DetailOnly,
Expand Down Expand Up @@ -467,7 +467,7 @@ export class PivotDataSet extends BaseDataSet {
}
}

public getTotalStatus = (query: Query) => {
public getTotalStatus = (query: Query): TotalStatus => {
const { columns, rows } = this.fields;
const isTotals = (dimensions: string[], isSubTotal?: boolean) => {
if (isSubTotal) {
Expand All @@ -480,9 +480,9 @@ export class PivotDataSet extends BaseDataSet {
};

return {
isRowTotal: isTotals(filterExtraDimension(rows as string[])),
isRowGrandTotal: isTotals(filterExtraDimension(rows as string[])),
isRowSubTotal: isTotals(rows as string[], true),
isColTotal: isTotals(filterExtraDimension(columns as string[])),
isColGrandTotal: isTotals(filterExtraDimension(columns as string[])),
isColSubTotal: isTotals(columns as string[], true),
};
};
Expand Down
3 changes: 3 additions & 0 deletions packages/s2-core/src/facet/pivot-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { getIndexRangeWithOffsets } from '../utils/facet';
import { getRowsForGrid } from '../utils/grid';
import { floor } from '../utils/math';
import { getCellWidth } from '../utils/text';
import { getHeaderTotalStatus } from '../utils/dataset/pivot-data-set';
import { FrozenFacet } from './frozen-facet';
import { Frame } from './header';
import { buildHeaderHierarchy } from './layout/build-header-hierarchy';
Expand Down Expand Up @@ -112,6 +113,7 @@ export class PivotFacet extends FrozenFacet {
const isTotals =
row.isTotals || row.isTotalMeasure || col.isTotals || col.isTotalMeasure;

const totalStatus = getHeaderTotalStatus(row, col);
const hideMeasure = options.style?.colCell?.hideValue ?? false;

/*
Expand All @@ -129,6 +131,7 @@ export class PivotFacet extends FrozenFacet {
query: dataQuery,
rowNode: row,
isTotals,
totalStatus,
});

const valueField = dataQuery[EXTRA_FIELD]!;
Expand Down
12 changes: 7 additions & 5 deletions packages/s2-core/src/utils/data-set-operate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
EMPTY_EXTRA_FIELD_PLACEHOLDER,
TOTAL_VALUE,
} from '../common/constant/field';
import type { CalcTotals, Totals, TotalsStatus } from '../common/interface';
import type { CalcTotals, Totals } from '../common/interface';
import type { TotalStatus } from '../data-set';

export const getListBySorted = (
list: string[],
Expand Down Expand Up @@ -59,10 +60,11 @@ export const sortByItems = (arr1: string[], arr2: string[]) => {
};

export function getAggregationAndCalcFuncByQuery(
totalsStatus: TotalsStatus,
totalsStatus: TotalStatus,
totalsOptions?: Totals | null,
) {
const { isRowTotal, isRowSubTotal, isColTotal, isColSubTotal } = totalsStatus;
const { isRowGrandTotal, isRowSubTotal, isColGrandTotal, isColSubTotal } =
totalsStatus;
const { row, col } = totalsOptions || {};
const {
calcGrandTotals: rowCalcTotals = {},
Expand All @@ -84,9 +86,9 @@ export function getAggregationAndCalcFuncByQuery(

// 优先级: 列总计/小计 > 行总计/小计
return (
getCalcTotals(colCalcTotals, isColTotal) ||
getCalcTotals(colCalcTotals, isColGrandTotal) ||
getCalcTotals(colCalcSubTotals, isColSubTotal) ||
getCalcTotals(rowCalcTotals, isRowTotal) ||
getCalcTotals(rowCalcTotals, isRowGrandTotal) ||
getCalcTotals(rowCalcSubTotals, isRowSubTotal)
);
}
4 changes: 2 additions & 2 deletions packages/s2-core/src/utils/dataset/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ export function generateExtraFieldMeta(

export function getHeaderTotalStatus(row: Node, col: Node): TotalStatus {
return {
isRowTotal: row.isGrandTotals!,
isRowGrandTotal: row.isGrandTotals!,
isRowSubTotal: row.isSubTotals!,
isColTotal: col.isGrandTotals!,
isColGrandTotal: col.isGrandTotals!,
isColSubTotal: col.isSubTotals!,
};
}
Expand Down

0 comments on commit 4d0dfc9

Please sign in to comment.