2020-07-21 13:20:51 +00:00
|
|
|
// tslint:disable:max-classes-per-file
|
|
|
|
|
|
|
|
import {CellValue} from 'app/common/DocActions';
|
|
|
|
import * as gristTypes from 'app/common/gristTypes';
|
|
|
|
import * as gutil from 'app/common/gutil';
|
|
|
|
import {buildNumberFormat, NumberFormatOptions} from 'app/common/NumberFormat';
|
2020-08-21 21:14:42 +00:00
|
|
|
import {decodeObject, GristDateTime} from 'app/plugin/objtypes';
|
|
|
|
import isPlainObject = require('lodash/isPlainObject');
|
2020-07-21 13:20:51 +00:00
|
|
|
import * as moment from 'moment-timezone';
|
2021-08-26 16:35:11 +00:00
|
|
|
import {DocumentSettings} from 'app/common/DocumentSettings';
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2020-08-21 21:14:42 +00:00
|
|
|
export {PENDING_DATA_PLACEHOLDER} from 'app/plugin/objtypes';
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2021-08-26 16:35:11 +00:00
|
|
|
export interface FormatOptions {
|
|
|
|
[option: string]: any;
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
/**
|
2020-08-21 21:14:42 +00:00
|
|
|
* Formats a value of any type generically (with no type-specific options).
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
2020-08-21 21:14:42 +00:00
|
|
|
export function formatUnknown(value: CellValue): string {
|
|
|
|
return formatHelper(decodeObject(value));
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-21 21:14:42 +00:00
|
|
|
* Formats a decoded Grist value for displaying it. For top-level values, formats them the way we
|
|
|
|
* like to see them in a cell or in, say, CSV export. For lists and objects, nested values are
|
|
|
|
* formatted slighly differently, with quoted strings and ISO format for dates.
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
2020-08-21 21:14:42 +00:00
|
|
|
function formatHelper(value: unknown, isTopLevel: boolean = true): string {
|
|
|
|
if (typeof value === 'object' && value) {
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
return '[' + value.map(v => formatHelper(v, false)).join(', ') + ']';
|
|
|
|
} else if (isPlainObject(value)) {
|
|
|
|
const obj: any = value;
|
|
|
|
const items = Object.keys(obj).map(k => `${JSON.stringify(k)}: ${formatHelper(obj[k], false)}`);
|
|
|
|
return '{' + items.join(', ') + '}';
|
|
|
|
} else if (isTopLevel && value instanceof GristDateTime) {
|
|
|
|
return moment(value).tz(value.timezone).format("YYYY-MM-DD HH:mm:ssZ");
|
|
|
|
}
|
|
|
|
return String(value);
|
|
|
|
}
|
|
|
|
if (isTopLevel) {
|
|
|
|
return (value == null ? "" : String(value));
|
|
|
|
}
|
|
|
|
return JSON.stringify(value);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type IsRightTypeFunc = (value: CellValue) => boolean;
|
|
|
|
|
|
|
|
export class BaseFormatter {
|
|
|
|
public readonly isRightType: IsRightTypeFunc;
|
|
|
|
|
2021-08-26 16:35:11 +00:00
|
|
|
constructor(public type: string, public widgetOpts: object, public docSettings: DocumentSettings) {
|
2020-07-21 13:20:51 +00:00
|
|
|
this.isRightType = gristTypes.isRightType(gristTypes.extractTypeFromColType(type)) ||
|
|
|
|
gristTypes.isRightType('Any')!;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a value that matches the type of this formatter. This should be overridden by derived
|
|
|
|
* classes to handle values in formatter-specific ways.
|
|
|
|
*/
|
|
|
|
public format(value: any): string {
|
2020-11-12 20:01:11 +00:00
|
|
|
return String(value);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats using this.format() if a value is of the right type for this formatter, or using
|
|
|
|
* AnyFormatter otherwise. This method the recommended API. There is no need to override it.
|
|
|
|
*/
|
|
|
|
public formatAny(value: any): string {
|
|
|
|
return this.isRightType(value) ? this.format(value) : formatUnknown(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AnyFormatter extends BaseFormatter {
|
|
|
|
public format(value: any): string {
|
|
|
|
return formatUnknown(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class NumericFormatter extends BaseFormatter {
|
|
|
|
private _numFormat: Intl.NumberFormat;
|
|
|
|
private _formatter: (val: number) => string;
|
|
|
|
|
2021-08-26 16:35:11 +00:00
|
|
|
constructor(type: string, options: NumberFormatOptions, docSettings: DocumentSettings) {
|
|
|
|
super(type, options, docSettings);
|
|
|
|
this._numFormat = buildNumberFormat(options, docSettings);
|
2020-07-21 13:20:51 +00:00
|
|
|
this._formatter = (options.numSign === 'parens') ? this._formatParens : this._formatPlain;
|
|
|
|
}
|
|
|
|
|
|
|
|
public format(value: any): string {
|
|
|
|
return value === null ? '' : this._formatter(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public _formatPlain(value: number): string {
|
|
|
|
return this._numFormat.format(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public _formatParens(value: number): string {
|
|
|
|
// Surround positive numbers with spaces to align them visually to parenthesized numbers.
|
|
|
|
return (value >= 0) ?
|
|
|
|
` ${this._numFormat.format(value)} ` :
|
|
|
|
`(${this._numFormat.format(-value)})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class IntFormatter extends NumericFormatter {
|
2021-08-26 16:35:11 +00:00
|
|
|
constructor(type: string, opts: FormatOptions, docSettings: DocumentSettings) {
|
|
|
|
super(type, {decimals: 0, ...opts}, docSettings);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:50:49 +00:00
|
|
|
export interface DateFormatOptions {
|
2021-08-26 16:35:11 +00:00
|
|
|
dateFormat?: string;
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
class DateFormatter extends BaseFormatter {
|
|
|
|
private _dateTimeFormat: string;
|
|
|
|
private _timezone: string;
|
|
|
|
|
2021-08-26 16:35:11 +00:00
|
|
|
constructor(type: string, widgetOpts: DateFormatOptions, docSettings: DocumentSettings, timezone: string = 'UTC') {
|
|
|
|
super(type, widgetOpts, docSettings);
|
|
|
|
this._dateTimeFormat = widgetOpts.dateFormat || 'YYYY-MM-DD';
|
2020-07-21 13:20:51 +00:00
|
|
|
this._timezone = timezone;
|
|
|
|
}
|
|
|
|
|
|
|
|
public format(value: any): string {
|
|
|
|
if (value === null) { return ''; }
|
|
|
|
const time = moment.tz(value * 1000, this._timezone);
|
|
|
|
return time.format(this._dateTimeFormat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:50:49 +00:00
|
|
|
export interface DateTimeFormatOptions extends DateFormatOptions {
|
2021-08-26 16:35:11 +00:00
|
|
|
timeFormat?: string;
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
class DateTimeFormatter extends DateFormatter {
|
2021-08-26 16:35:11 +00:00
|
|
|
constructor(type: string, widgetOpts: DateTimeFormatOptions, docSettings: DocumentSettings) {
|
2020-07-21 13:20:51 +00:00
|
|
|
const timezone = gutil.removePrefix(type, "DateTime:") || '';
|
2021-08-26 16:35:11 +00:00
|
|
|
const timeFormat = widgetOpts.timeFormat === undefined ? 'h:mma' : widgetOpts.timeFormat;
|
|
|
|
const dateFormat = (widgetOpts.dateFormat || 'YYYY-MM-DD') + " " + timeFormat;
|
|
|
|
super(type, {dateFormat}, docSettings, timezone);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const formatters: {[name: string]: typeof BaseFormatter} = {
|
|
|
|
Numeric: NumericFormatter,
|
|
|
|
Int: IntFormatter,
|
|
|
|
Bool: BaseFormatter,
|
|
|
|
Date: DateFormatter,
|
|
|
|
DateTime: DateTimeFormatter,
|
|
|
|
// We don't list anything that maps to AnyFormatter, since that's the default.
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-08-26 16:35:11 +00:00
|
|
|
* Takes column type, widget options and document settings, and returns a constructor
|
|
|
|
* with a format function that can properly convert a value passed to it into the
|
|
|
|
* right format for that column.
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
2021-08-26 16:35:11 +00:00
|
|
|
export function createFormatter(type: string, widgetOpts: FormatOptions, docSettings: DocumentSettings): BaseFormatter {
|
2020-07-21 13:20:51 +00:00
|
|
|
const ctor = formatters[gristTypes.extractTypeFromColType(type)] || AnyFormatter;
|
2021-08-26 16:35:11 +00:00
|
|
|
return new ctor(type, widgetOpts, docSettings);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|