(core) Refactor more value parsing code into common

Summary:
Following discussion in https://phab.getgrist.com/D3164:

- Change createParser to accept docData and one or two metadata row IDs and let it extract the metadata, so it's more easily usable in the server.
- Change ViewFieldRec.valueParser observable to a function createValueParser.

Test Plan: Existing tests.

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D3172
This commit is contained in:
Alex Hall
2021-12-08 00:37:53 +02:00
parent 4164d89b84
commit 6b448567c9
5 changed files with 53 additions and 25 deletions

View File

@@ -2,7 +2,6 @@ import {ColumnRec, DocModel, IRowModel, refRecord, ViewSectionRec} from 'app/cli
import * as modelUtil from 'app/client/models/modelUtil';
import * as UserType from 'app/client/widgets/UserType';
import {DocumentSettings} from 'app/common/DocumentSettings';
import {getReferencedTableId, isFullReferencingType} from 'app/common/gristTypes';
import {BaseFormatter, createFormatter} from 'app/common/ValueFormatter';
import {createParser} from 'app/common/ValueParser';
import * as ko from 'knockout';
@@ -70,7 +69,7 @@ export interface ViewFieldRec extends IRowModel<"_grist_Views_section_field"> {
documentSettings: ko.PureComputed<DocumentSettings>;
valueParser: ko.Computed<(value: string) => any>;
createValueParser(): (value: string) => any;
// Helper which adds/removes/updates field's displayCol to match the formula.
saveDisplayFormula(formula: string): Promise<void>|undefined;
@@ -174,20 +173,10 @@ export function createViewFieldRec(this: ViewFieldRec, docModel: DocModel): void
createFormatter(this.column().type(), this.widgetOptionsJson(), this.documentSettings());
};
this.valueParser = ko.pureComputed(() => {
const docSettings = this.documentSettings();
const type = this.column().type();
const widgetOpts = this.widgetOptionsJson();
if (isFullReferencingType(type)) {
const vcol = this.visibleColModel();
widgetOpts.visibleColId = vcol.colId() || 'id';
widgetOpts.visibleColType = vcol.type();
widgetOpts.visibleColWidgetOpts = vcol.widgetOptionsJson();
widgetOpts.tableData = docModel.docData.getTable(getReferencedTableId(type)!);
}
return createParser(type, widgetOpts, docSettings);
});
this.createValueParser = function() {
const fieldRef = this.useColOptions.peek() ? undefined : this.id.peek();
return createParser(docModel.docData, this.colRef.peek(), fieldRef);
};
// The widgetOptions to read and write: either the column's or the field's own.
this._widgetOptionsStr = modelUtil.savingComputed({