mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Move most of the reference parsing code into common so that the server can use it
Summary: Refactoring in preparation for parsing strings from the API. The plan is that the API code will only need to do a server-side version of the code in ViewFieldRec.valueParser (minus ReferenceUtils) which is quite minimal. Test Plan: Nothing extra here, I don't think it's needed. This stuff will get tested more in a future diff which changes the API. Reviewers: dsagal Reviewed By: dsagal Differential Revision: https://phab.getgrist.com/D3164
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { DocData } from 'app/client/models/DocData';
|
||||
import {DocData} from 'app/client/models/DocData';
|
||||
import {ColumnRec} from 'app/client/models/entities/ColumnRec';
|
||||
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
||||
import {SearchFunc, TableData} from 'app/client/models/TableData';
|
||||
import {TableData} from 'app/client/models/TableData';
|
||||
import {getReferencedTableId, isRefListType} from 'app/common/gristTypes';
|
||||
import {BaseFormatter} from 'app/common/ValueFormatter';
|
||||
import isEqual = require('lodash/isEqual');
|
||||
|
||||
/**
|
||||
* Utilities for common operations involving Ref[List] fields.
|
||||
@@ -40,80 +39,6 @@ export class ReferenceUtils {
|
||||
this.isRefList = isRefListType(colType);
|
||||
}
|
||||
|
||||
public parseReference(
|
||||
raw: string, value: unknown
|
||||
): number | string | ['l', unknown, {raw?: string, column: string}] {
|
||||
if (!value || !raw) {
|
||||
return 0; // default value for a reference column
|
||||
}
|
||||
|
||||
if (this.visibleColId === 'id') {
|
||||
const n = Number(value);
|
||||
if (Number.isInteger(n)) {
|
||||
value = n;
|
||||
} else {
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.tableData.isLoaded) {
|
||||
const options: {column: string, raw?: string} = {column: this.visibleColId};
|
||||
if (value !== raw) {
|
||||
options.raw = raw;
|
||||
}
|
||||
return ['l', value, options];
|
||||
}
|
||||
|
||||
const searchFunc: SearchFunc = (v: any) => isEqual(v, value);
|
||||
const matches = this.tableData.columnSearch(this.visibleColId, searchFunc, 1);
|
||||
if (matches.length > 0) {
|
||||
return matches[0];
|
||||
} else {
|
||||
// There's no matching value in the visible column, i.e. this is not a valid reference.
|
||||
// We need to return a string which will become AltText.
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
public parseReferenceList(
|
||||
raw: string, values: unknown[]
|
||||
): ['L', ...number[]] | null | string | ['l', unknown[], {raw?: string, column: string}] {
|
||||
if (!values.length || !raw) {
|
||||
return null; // default value for a reference list column
|
||||
}
|
||||
|
||||
if (this.visibleColId === 'id') {
|
||||
const numbers = values.map(Number);
|
||||
if (numbers.every(Number.isInteger)) {
|
||||
values = numbers;
|
||||
} else {
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.tableData.isLoaded) {
|
||||
const options: {column: string, raw?: string} = {column: this.visibleColId};
|
||||
if (!(values.length === 1 && values[0] === raw)) {
|
||||
options.raw = raw;
|
||||
}
|
||||
return ['l', values, options];
|
||||
}
|
||||
|
||||
const rowIds: number[] = [];
|
||||
for (const value of values) {
|
||||
const searchFunc: SearchFunc = (v: any) => isEqual(v, value);
|
||||
const matches = this.tableData.columnSearch(this.visibleColId, searchFunc, 1);
|
||||
if (matches.length > 0) {
|
||||
rowIds.push(matches[0]);
|
||||
} else {
|
||||
// There's no matching value in the visible column, i.e. this is not a valid reference.
|
||||
// We need to return a string which will become AltText.
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
return ['L', ...rowIds];
|
||||
}
|
||||
|
||||
public idToText(value: unknown) {
|
||||
if (typeof value === 'number') {
|
||||
return this.formatter.formatAny(this.tableData.getValue(value, this.visibleColId));
|
||||
|
||||
Reference in New Issue
Block a user