gristlabs_grist-core/app/client/models/UnionRowSource.ts
Alex Hall bd52665f96 (core) Allow adding rows to widgets filtered by a link using a formula column
Summary:
When a widget `A` is selected by a widget `B` so that `A` is filtered, adding a new row to `A` uses the values in the selected row of `B` and the columns relevant to the linking as default values for the new row. This ensures that the new row matches the current linking filter and remains visible. However this would previously cause a sandbox error when one of the linking columns was a formula column, which doesn't allow setting values. This diff ignores formula columns when picking default values.

Since the value of the formula column in the new row typically won't match the linking filter, extra measures are needed to avoid the new row immediately disappearing. Regular filters already have a mechanism for this, but I didn't manage to extend it to also work for linking. Thanks @dsagal for creating `UnionRowSource` (originally in D4017) which is now used as the solution for temporarily exempting rows from both kinds of filtering.

While testing, I also came across another bug in linking summary tables that caused incorrect filtering, which I fixed with some changes to `DynamicQuerySet`.

Test Plan: Extended an nbrowser test, which both tests for the main change as well as the secondary bugfix.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Subscribers: dsagal

Differential Revision: https://phab.getgrist.com/D4135
2023-12-18 20:28:41 +02:00

60 lines
1.4 KiB
TypeScript

import {RowList, RowListener, RowSource} from 'app/client/models/rowset';
import {UIRowId} from "app/plugin/GristAPI";
export class UnionRowSource extends RowListener implements RowSource {
protected _allRows = new Map<UIRowId, Set<RowSource>>();
constructor(parentRowSources: RowSource[]) {
super();
for (const p of parentRowSources) {
this.subscribeTo(p);
}
}
public getAllRows(): RowList {
return this._allRows.keys();
}
public getNumRows(): number {
return this._allRows.size;
}
public onAddRows(rows: RowList, rowSource: RowSource) {
const outputRows = [];
for (const r of rows) {
let sources = this._allRows.get(r);
if (!sources) {
sources = new Set();
this._allRows.set(r, sources);
outputRows.push(r);
}
sources.add(rowSource);
}
if (outputRows.length > 0) {
this.trigger('rowChange', 'add', outputRows);
}
}
public onRemoveRows(rows: RowList, rowSource: RowSource) {
const outputRows = [];
for (const r of rows) {
const sources = this._allRows.get(r);
if (!sources) {
continue;
}
sources.delete(rowSource);
if (sources.size === 0) {
outputRows.push(r);
this._allRows.delete(r);
}
}
if (outputRows.length > 0) {
this.trigger('rowChange', 'remove', outputRows);
}
}
public onUpdateRows(rows: RowList) {
this.trigger('rowChange', 'update', rows);
}
}