2022-02-08 15:23:14 +00:00
|
|
|
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;
|
2022-08-26 12:25:34 +00:00
|
|
|
// Human description of the column.
|
|
|
|
public description: string;
|
2022-02-08 15:23:14 +00:00
|
|
|
// If column is optional (used only on the UI).
|
|
|
|
public optional: boolean;
|
2023-09-13 10:29:36 +00:00
|
|
|
// Type of the column that widget expects. Might be a single or a comma separated list of types.
|
2023-10-09 09:00:40 +00:00
|
|
|
// "Any" means that any type is allowed (unless strictType is true).
|
2022-02-08 15:23:14 +00:00
|
|
|
public type: string;
|
2023-10-09 09:00:40 +00:00
|
|
|
// If true, the column type is strict and cannot be any type.
|
|
|
|
public strictType: boolean;
|
2022-02-08 15:23:14 +00:00
|
|
|
// 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);
|
2022-08-26 12:25:34 +00:00
|
|
|
this.description = typeof def === 'string' ? '' : (def.description ?? '');
|
2022-02-08 15:23:14 +00:00
|
|
|
this.optional = typeof def === 'string' ? false : (def.optional ?? false);
|
|
|
|
this.type = typeof def === 'string' ? 'Any' : (def.type ?? 'Any');
|
2023-10-06 09:11:37 +00:00
|
|
|
this.type = this.type.split(',').map(t => t.trim()).filter(Boolean).join(',');
|
|
|
|
this.typeDesc = this.type.split(',')
|
|
|
|
.map(t => String(UserType.typeDefs[t]?.label ?? "any").toLowerCase()).join(', ');
|
2022-02-08 15:23:14 +00:00
|
|
|
this.allowMultiple = typeof def === 'string' ? false : (def.allowMultiple ?? false);
|
2023-10-09 09:00:40 +00:00
|
|
|
this.strictType = typeof def === 'string' ? false : (def.strictType ?? false);
|
2022-02-08 15:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the column type matches this definition.
|
2023-10-09 09:00:40 +00:00
|
|
|
*
|
|
|
|
* Here are use case examples, for better understanding (Any is treated as a star):
|
|
|
|
* 1. Widget sets "Text", user can map to "Text" or "Any".
|
|
|
|
* 2. Widget sets "Any", user can map to "Int", "Toggle", "Any" and any other type.
|
|
|
|
* 3. Widget sets "Text,Int", user can map to "Text", "Int", "Any"
|
|
|
|
*
|
|
|
|
* With strictType, the Any in the widget is treated as Any, not a star.
|
|
|
|
* 1. Widget sets "Text", user can map to "Text".
|
|
|
|
* 2. Widget sets "Any", user can map to "Any". Not to "Text", "Int", etc. NOTICE: here Any in widget is not a star,
|
|
|
|
* widget expects Any as a type so "Toggle" column won't be allowed.
|
|
|
|
* 3. Widget sets "Text,Int", user can only map to "Text", "Int".
|
|
|
|
* 4. Widget sets "Text,Any", user can only map to "Text", "Any".
|
2022-02-08 15:23:14 +00:00
|
|
|
*/
|
|
|
|
public canByMapped(pureType: string) {
|
2023-10-09 09:00:40 +00:00
|
|
|
const isAny = pureType === "Any" || this.type === "Any";
|
|
|
|
return this.type.split(',').includes(pureType) || (isAny && !this.strictType);
|
2022-02-08 15:23:14 +00:00
|
|
|
}
|
|
|
|
}
|