gristlabs_grist-core/app/client/models/ColumnToMap.ts
Jarosław Sadziński b80e56a4e1 (core) Custom Widget column mapping feature.
Summary:
Exposing new API in CustomSectionAPI for column mapping.

The custom widget can call configure method (or use a ready method) with additional parameter "columns".
This parameter is a list of column names that should be mapped by the user.
Mapping configuration is exposed through an additional method in the CustomSectionAPI "mappings". It is also available
through the onRecord(s) event.

This DIFF is connected with PR for grist-widgets repository https://github.com/gristlabs/grist-widget/pull/15

Design document and discussion: https://grist.quip.com/Y2waA8h8Zuzu/Custom-Widget-field-mapping

Test Plan: browser tests

Reviewers: paulfitz

Reviewed By: paulfitz

Differential Revision: https://phab.getgrist.com/D3241
2022-02-08 17:41:04 +01:00

38 lines
1.4 KiB
TypeScript

import * as UserType from 'app/client/widgets/UserType';
import {ColumnToMap} from 'app/plugin/CustomSectionAPI';
/**
* Helper that wraps custom widget's column definition and expands all the defaults.
*/
export class ColumnToMapImpl implements Required<ColumnToMap> {
// Name of the column Custom Widget expects.
public name: string;
// Label to show instead of the name.
public title: string;
// If column is optional (used only on the UI).
public optional: boolean;
// Type of the column that widget expects.
public type: string;
// Description of the type (used to show a placeholder).
public typeDesc: string;
// Allow multiple column assignment (like Series in Charts).
public allowMultiple: boolean;
constructor(def: string|ColumnToMap) {
this.name = typeof def === 'string' ? def : def.name;
this.title = typeof def === 'string' ? def : (def.title ?? def.name);
this.optional = typeof def === 'string' ? false : (def.optional ?? false);
this.type = typeof def === 'string' ? 'Any' : (def.type ?? 'Any');
this.typeDesc = String(UserType.typeDefs[this.type]?.label ?? "any").toLowerCase();
this.allowMultiple = typeof def === 'string' ? false : (def.allowMultiple ?? false);
}
/**
* Does the column type matches this definition.
*/
public canByMapped(pureType: string) {
return pureType === this.type
|| pureType === "Any"
|| this.type === "Any";
}
}