gristlabs_grist-core/app/server/lib/ServerColumnGetters.ts
Alex Hall bc54a6646e (core) Filter rows based on linked widgets when exporting view
Summary:
Fixes a problem reported here: https://community.getgrist.com/t/exporting-the-records-in-a-linked-view/2556/4

The download CSV/Excel link now contains an additional `linkingFilter` URL parameter containing JSON-encoded `filters` and `operations`. This object is originally created in the frontend in `LinkingState`, and previously it was only used internally in the frontend. It would make its way via `QuerySetManager` to `QuerySet.getFilterFunc` where the actual filtering logic happened. Now most of that logic has been moved to a similar function in `common`. The new function works with a new interface `ColumnGettersByColId` which abstract over the different ways data is accessed in the client and server in this context. There's no significant new logic in the diff, just refactoring and wiring.

Test Plan: Expanded two `nbrowser/SelectBy*.ts` test suites to also check the contents of a downloaded CSV in different linking scenarios.

Reviewers: paulfitz

Reviewed By: paulfitz

Differential Revision: https://phab.getgrist.com/D3961
2023-07-26 21:49:52 +02:00

70 lines
2.2 KiB
TypeScript

import {ColumnGetter, ColumnGetters, ColumnGettersByColId} from 'app/common/ColumnGetters';
import * as gristTypes from 'app/common/gristTypes';
import {safeJsonParse} from 'app/common/gutil';
import {choiceGetter} from 'app/common/SortFunc';
import {Sort} from 'app/common/SortSpec';
import {BulkColValues} from 'app/plugin/GristData';
/**
*
* An implementation of ColumnGetters for the server, currently
* drawing on the data and metadata prepared for CSV export.
*
*/
export class ServerColumnGetters implements ColumnGetters, ColumnGettersByColId {
private _rowIndices: Map<number, number>;
private _colIndices: Map<number, string>;
constructor(rowIds: number[], private _dataByColId: BulkColValues, private _columns: any[]) {
this._rowIndices = new Map<number, number>(rowIds.map((rowId, index) => [rowId, index] as [number, number]));
this._colIndices = new Map<number, string>(_columns.map(col => [col.id, col.colId] as [number, string]));
}
public getColGetter(colSpec: Sort.ColSpec): ColumnGetter | null {
const colRef = Sort.getColRef(colSpec);
const colId = this._colIndices.get(colRef);
if (colId === undefined) {
return null;
}
let getter = this.getColGetterByColId(colId);
if (!getter) {
return null;
}
const details = Sort.specToDetails(colSpec);
if (details.orderByChoice) {
const rowModel = this._columns.find(c => c.id == colRef);
if (rowModel?.type === 'Choice') {
const choices: string[] = safeJsonParse(rowModel.widgetOptions, {}).choices || [];
getter = choiceGetter(getter, choices);
}
}
return getter;
}
public getManualSortGetter(): ((rowId: number) => any) | null {
const manualSortCol = this._columns.find(c => c.colId === gristTypes.MANUALSORT);
if (!manualSortCol) {
return null;
}
return this.getColGetter(manualSortCol.id);
}
public getColGetterByColId(colId: string): ColumnGetter | null {
if (colId === "id") {
return (rowId: number) => rowId;
}
const col = this._dataByColId[colId];
if (!col) {
return null;
}
return (rowId: number) => {
const idx = this._rowIndices.get(rowId);
if (idx === undefined) {
return null;
}
return col[idx];
};
}
}