2021-11-19 18:54:42 +00:00
|
|
|
import {csvDecodeRow} from 'app/common/csvFormat';
|
|
|
|
import {DocumentSettings} from 'app/common/DocumentSettings';
|
2021-10-21 18:50:49 +00:00
|
|
|
import * as gristTypes from 'app/common/gristTypes';
|
|
|
|
import * as gutil from 'app/common/gutil';
|
2021-11-19 18:54:42 +00:00
|
|
|
import {safeJsonParse} from 'app/common/gutil';
|
|
|
|
import {getCurrency, NumberFormatOptions} from 'app/common/NumberFormat';
|
2021-10-21 18:50:49 +00:00
|
|
|
import NumberParse from 'app/common/NumberParse';
|
2021-11-24 22:48:37 +00:00
|
|
|
import {parseDateStrict, parseDateTime} from 'app/common/parseDate';
|
2021-12-06 12:07:52 +00:00
|
|
|
import {TableData} from 'app/common/TableData';
|
2021-11-19 18:54:42 +00:00
|
|
|
import {DateFormatOptions, DateTimeFormatOptions, formatDecoded, FormatOptions} from 'app/common/ValueFormatter';
|
|
|
|
import flatMap = require('lodash/flatMap');
|
2021-10-21 18:50:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
export class ValueParser {
|
2021-12-06 12:07:52 +00:00
|
|
|
constructor(public type: string, public widgetOpts: FormatOptions, public docSettings: DocumentSettings) {
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public cleanParse(value: string): any {
|
|
|
|
if (!value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return this.parse(value) ?? value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public parse(value: string): any {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class NumericParser extends ValueParser {
|
|
|
|
private _parse: NumberParse;
|
|
|
|
|
|
|
|
constructor(type: string, options: NumberFormatOptions, docSettings: DocumentSettings) {
|
|
|
|
super(type, options, docSettings);
|
|
|
|
this._parse = new NumberParse(docSettings.locale, getCurrency(options, docSettings));
|
|
|
|
}
|
|
|
|
|
|
|
|
public parse(value: string): number | null {
|
|
|
|
return this._parse.parse(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DateParser extends ValueParser {
|
|
|
|
public parse(value: string): any {
|
2021-10-25 17:42:06 +00:00
|
|
|
return parseDateStrict(value, (this.widgetOpts as DateFormatOptions).dateFormat!);
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 22:48:37 +00:00
|
|
|
class DateTimeParser extends ValueParser {
|
2021-10-21 18:50:49 +00:00
|
|
|
constructor(type: string, widgetOpts: DateTimeFormatOptions, docSettings: DocumentSettings) {
|
|
|
|
super(type, widgetOpts, docSettings);
|
|
|
|
const timezone = gutil.removePrefix(type, "DateTime:") || '';
|
|
|
|
this.widgetOpts = {...widgetOpts, timezone};
|
|
|
|
}
|
2021-11-24 22:48:37 +00:00
|
|
|
|
|
|
|
public parse(value: string): any {
|
|
|
|
return parseDateTime(value, this.widgetOpts);
|
|
|
|
}
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 18:54:42 +00:00
|
|
|
|
|
|
|
class ChoiceListParser extends ValueParser {
|
|
|
|
public cleanParse(value: string): string[] | null {
|
|
|
|
value = value.trim();
|
|
|
|
const result = (
|
|
|
|
this._parseJson(value) ||
|
|
|
|
this._parseCsv(value)
|
|
|
|
).map(v => v.trim())
|
|
|
|
.filter(v => v);
|
|
|
|
if (!result.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return ["L", ...result];
|
|
|
|
}
|
|
|
|
|
|
|
|
private _parseJson(value: string): string[] | undefined {
|
|
|
|
// Don't parse JSON non-arrays
|
|
|
|
if (value[0] === "[") {
|
|
|
|
const arr: unknown[] | null = safeJsonParse(value, null);
|
|
|
|
return arr
|
|
|
|
// Remove nulls and empty strings
|
|
|
|
?.filter(v => v || v === 0)
|
|
|
|
// Convert values to strings, formatting nested JSON objects/arrays as JSON
|
|
|
|
.map(v => formatDecoded(v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _parseCsv(value: string): string[] {
|
|
|
|
// Split everything on newlines which are not allowed by the choice editor.
|
|
|
|
return flatMap(value.split(/[\n\r]+/), row => {
|
|
|
|
return csvDecodeRow(row)
|
|
|
|
.map(v => v.trim());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:07:52 +00:00
|
|
|
/**
|
|
|
|
* This is different from other widget options which are simple JSON
|
|
|
|
* stored on the field. These have to be specially derived
|
|
|
|
* for referencing columns. See ViewFieldRec.valueParser for an example.
|
|
|
|
*/
|
|
|
|
interface ReferenceParsingOptions {
|
|
|
|
visibleColId: string;
|
|
|
|
visibleColType: string;
|
|
|
|
visibleColWidgetOpts: FormatOptions;
|
|
|
|
|
|
|
|
// If this is provided and loaded, the ValueParser will look up values directly.
|
|
|
|
// Otherwise an encoded lookup will be produced for the data engine to handle.
|
|
|
|
tableData?: TableData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ReferenceParser extends ValueParser {
|
|
|
|
public widgetOpts: ReferenceParsingOptions;
|
|
|
|
|
|
|
|
protected _visibleColId = this.widgetOpts.visibleColId;
|
|
|
|
protected _tableData = this.widgetOpts.tableData;
|
|
|
|
protected _visibleColParser = createParser(
|
|
|
|
this.widgetOpts.visibleColType,
|
|
|
|
this.widgetOpts.visibleColWidgetOpts,
|
|
|
|
this.docSettings,
|
|
|
|
);
|
|
|
|
|
|
|
|
public parse(raw: string): any {
|
|
|
|
let value = this._visibleColParser(raw);
|
|
|
|
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;
|
|
|
|
// Don't return yet because we need to check that this row ID exists
|
|
|
|
} 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._tableData.findMatchingRowId({[this._visibleColId]: value}) || raw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ReferenceListParser extends ReferenceParser {
|
|
|
|
public parse(raw: string): any {
|
|
|
|
let values: any[] | null;
|
|
|
|
try {
|
|
|
|
values = JSON.parse(raw);
|
|
|
|
} catch {
|
|
|
|
values = null;
|
|
|
|
}
|
|
|
|
if (!Array.isArray(values)) {
|
|
|
|
// csvDecodeRow should never raise an exception
|
|
|
|
values = csvDecodeRow(raw);
|
|
|
|
}
|
|
|
|
values = values.map(v => typeof v === "string" ? this._visibleColParser(v) : v);
|
|
|
|
|
|
|
|
if (!values.length || !raw) {
|
|
|
|
return null; // null is the default value for a reference list column
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._visibleColId === 'id') {
|
|
|
|
const numbers = values.map(Number);
|
|
|
|
if (numbers.every(Number.isInteger)) {
|
|
|
|
values = numbers;
|
|
|
|
// Don't return yet because we need to check that these row IDs exist
|
|
|
|
} 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 rowId = this._tableData.findMatchingRowId({[this._visibleColId]: value});
|
|
|
|
if (rowId) {
|
|
|
|
rowIds.push(rowId);
|
|
|
|
} 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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const valueParserClasses: { [type: string]: typeof ValueParser } = {
|
2021-10-21 18:50:49 +00:00
|
|
|
Numeric: NumericParser,
|
|
|
|
Int: NumericParser,
|
|
|
|
Date: DateParser,
|
|
|
|
DateTime: DateTimeParser,
|
2021-11-19 18:54:42 +00:00
|
|
|
ChoiceList: ChoiceListParser,
|
2021-12-06 12:07:52 +00:00
|
|
|
Ref: ReferenceParser,
|
|
|
|
RefList: ReferenceListParser,
|
2021-10-21 18:50:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-12-06 12:07:52 +00:00
|
|
|
/**
|
|
|
|
* Returns a function which can parse strings into values appropriate for
|
|
|
|
* a specific widget field or table column.
|
|
|
|
* widgetOpts is usually the field/column's widgetOptions JSON
|
|
|
|
* but referencing columns need more than that, see ReferenceParsingOptions above.
|
|
|
|
*/
|
2021-10-21 18:50:49 +00:00
|
|
|
export function createParser(
|
|
|
|
type: string, widgetOpts: FormatOptions, docSettings: DocumentSettings
|
2021-11-01 15:48:08 +00:00
|
|
|
): (value: string) => any {
|
2021-12-06 12:07:52 +00:00
|
|
|
const cls = valueParserClasses[gristTypes.extractTypeFromColType(type)];
|
2021-10-21 18:50:49 +00:00
|
|
|
if (cls) {
|
|
|
|
const parser = new cls(type, widgetOpts, docSettings);
|
|
|
|
return parser.cleanParse.bind(parser);
|
|
|
|
}
|
2021-11-01 15:48:08 +00:00
|
|
|
return value => value;
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|