/** * Copyright (c) 2018-2024 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose * @author David Sehnal */ import { Color as ColorData } from './color'; import { shallowEqualObjects } from './index'; import { Vec2 as Vec2Data, Vec3 as Vec3Data, Mat4 as Mat4Data, EPSILON } from '../mol-math/linear-algebra'; import { deepClone } from './object'; import { Script as ScriptData } from '../mol-script/script'; import { Legend } from './legend'; import { stringToWords } from './string'; import { getColorListFromName, ColorListName } from './color/lists'; import { Asset } from './assets'; import { ColorListEntry } from './color/color'; export namespace ParamDefinition { export interface Info { label?: string, description?: string, legend?: Legend, fieldLabels?: { [name: string]: string }, isHidden?: boolean, shortLabel?: boolean, twoColumns?: boolean, isEssential?: boolean, category?: string, hideIf?: (currentGroup: any) => boolean, help?: (value: any) => { description?: string, legend?: Legend } } export const Essential = { isEssential: true }; function setInfo>(param: T, info?: Info): T { if (!info) return param; if (info.label) param.label = info.label; if (info.description) param.description = info.description; if (info.legend) param.legend = info.legend; if (info.fieldLabels) param.fieldLabels = info.fieldLabels; if (info.isHidden) param.isHidden = info.isHidden; if (info.shortLabel) param.shortLabel = info.shortLabel; if (info.twoColumns) param.twoColumns = info.twoColumns; if (info.isEssential) param.isEssential = info.isEssential; if (info.category) param.category = info.category; if (info.hideIf) param.hideIf = info.hideIf; if (info.help) param.help = info.help; return param; } export interface Base extends Info { isOptional?: boolean, defaultValue: T } export interface Optional extends Base { type: T['type'] } export function Optional(p: Base): Base { const ret = { ...p }; ret.isOptional = true; return ret; } export interface Value extends Base { type: 'value' } export function Value(defaultValue: T, info?: Info): Value { return setInfo>({ type: 'value', defaultValue }, info); } export interface Select extends Base { type: 'select' /** array of (value, label) tuples */ options: readonly (readonly [T, string] | readonly [T, string, string | undefined])[] cycle?: boolean } export function Select(defaultValue: T, options: readonly (readonly [T, string] | readonly [T, string, string | undefined])[], info?: Info & { cycle?: boolean }): Select { return setInfo>({ type: 'select', defaultValue: checkDefaultKey(defaultValue, options), options, cycle: info?.cycle }, info); } export interface MultiSelect extends Base { type: 'multi-select' /** array of (value, label) tuples */ options: readonly (readonly [E, string])[], emptyValue?: string } export function MultiSelect(defaultValue: E[], options: readonly (readonly [E, string])[], info?: Info & { emptyValue?: string }): MultiSelect { // TODO: check if default value is a subset of options? const ret = setInfo>({ type: 'multi-select', defaultValue, options }, info); if (info?.emptyValue) ret.emptyValue = info.emptyValue; return ret; } export interface BooleanParam extends Base { type: 'boolean' } export function Boolean(defaultValue: boolean, info?: Info): BooleanParam { return setInfo({ type: 'boolean', defaultValue }, info); } export interface Text extends Base { type: 'text', multiline?: boolean, placeholder?: string, disableInteractiveUpdates?: boolean } export function Text(defaultValue: string = '', info?: Info & { multiline?: boolean, placeholder?: string, disableInteractiveUpdates?: boolean }): Text { return setInfo>({ type: 'text', defaultValue: defaultValue as any, multiline: info?.multiline, placeholder: info?.placeholder, disableInteractiveUpdates: info?.disableInteractiveUpdates }, info); } export interface Color extends Base { type: 'color' isExpanded?: boolean } export function Color(defaultValue: ColorData, info?: Info & { isExpanded?: boolean }): Color { const ret = setInfo({ type: 'color', defaultValue }, info); if (info?.isExpanded) ret.isExpanded = info.isExpanded; return ret; } export interface ColorList extends Base<{ kind: 'interpolate' | 'set', colors: ColorListEntry[] }> { type: 'color-list' offsets: boolean presetKind: 'all' | 'scale' | 'set' } export function ColorList(defaultValue: { kind: 'interpolate' | 'set', colors: ColorListEntry[] } | ColorListName, info?: Info & { presetKind?: ColorList['presetKind'], offsets?: boolean }): ColorList { let def: ColorList['defaultValue']; if (typeof defaultValue === 'string') { const colors = getColorListFromName(defaultValue); def = { kind: colors.type !== 'qualitative' ? 'interpolate' : 'set', colors: colors.list }; } else { def = defaultValue; } return setInfo({ type: 'color-list', presetKind: info?.presetKind || 'all', defaultValue: def, offsets: !!info?.offsets }, info); } export interface Vec3 extends Base, Range { type: 'vec3' } export function Vec3(defaultValue: Vec3Data, range?: { min?: number, max?: number, step?: number }, info?: Info): Vec3 { return setInfo(setRange({ type: 'vec3', defaultValue }, range), info); } export interface Mat4 extends Base { type: 'mat4' } export function Mat4(defaultValue: Mat4Data, info?: Info): Mat4 { return setInfo({ type: 'mat4', defaultValue }, info); } export interface UrlParam extends Base { type: 'url' } export function Url(url: string | { url: string, body?: string }, info?: Info): UrlParam { const defaultValue = typeof url === 'string' ? Asset.Url(url) : Asset.Url(url.url, { body: url.body }); const ret = setInfo({ type: 'url', defaultValue }, info); return ret; } export interface FileParam extends Base { type: 'file' accept?: string } export function File(info?: Info & { accept?: string, multiple?: boolean }): FileParam { const ret = setInfo({ type: 'file', defaultValue: null }, info); if (info?.accept) ret.accept = info.accept; return ret; } export interface FileListParam extends Base { type: 'file-list' accept?: string } export function FileList(info?: Info & { accept?: string, multiple?: boolean }): FileListParam { const ret = setInfo({ type: 'file-list', defaultValue: null }, info); if (info?.accept) ret.accept = info.accept; return ret; } export interface Range { /** If given treat as a range. */ min?: number /** If given treat as a range. */ max?: number /** * If given treat as a range. * If an `integer` parse value with parseInt, otherwise use parseFloat. */ step?: number } function setRange(p: T, range?: { min?: number, max?: number, step?: number }) { if (!range) return p; if (typeof range.min !== 'undefined') p.min = range.min; if (typeof range.max !== 'undefined') p.max = range.max; if (typeof range.step !== 'undefined') p.step = range.step; return p; } export interface Numeric extends Base, Range { type: 'number', immediateUpdate?: boolean } export function Numeric(defaultValue: number, range?: { min?: number, max?: number, step?: number }, info?: Info & { immediateUpdate?: boolean }): Numeric { const ret = setInfo(setRange({ type: 'number', defaultValue }, range), info); if (info?.immediateUpdate) ret.immediateUpdate = true; return ret; } export interface Interval extends Base<[number, number]>, Range { type: 'interval' } export function Interval(defaultValue: [number, number], range?: { min?: number, max?: number, step?: number }, info?: Info): Interval { return setInfo(setRange({ type: 'interval', defaultValue }, range), info); } export interface LineGraph extends Base { type: 'line-graph', getVolume?: () => unknown } export function LineGraph(defaultValue: Vec2Data[], info?: Info & { getVolume?: (binCount?: number) => unknown }): LineGraph { const ret = setInfo({ type: 'line-graph', defaultValue }, info); if (info?.getVolume) ret.getVolume = info.getVolume; return ret; } export interface Group extends Base { type: 'group', params: Params, presets?: Select['options'], isExpanded?: boolean, isFlat?: boolean, pivot?: keyof T } export function Group(params: For, info?: Info & { isExpanded?: boolean, isFlat?: boolean, customDefault?: any, pivot?: keyof T, presets?: Select['options'] }): Group> { const ret = setInfo>>({ type: 'group', defaultValue: info?.customDefault || getDefaultValues(params as any as Params) as any, params: params as any as Params }, info); if (info?.presets) ret.presets = info.presets; if (info?.isExpanded) ret.isExpanded = info.isExpanded; if (info?.isFlat) ret.isFlat = info.isFlat; if (info?.pivot) ret.pivot = info.pivot as any; return ret; } export function EmptyGroup(info?: Info) { return Group({}, info); } export interface NamedParams { name: K, params: T } export type NamedParamUnion

= K extends any ? NamedParams : never export interface Mapped> extends Base { type: 'mapped', select: Select, map(name: string): Any } export function Mapped(defaultKey: string, names: ([string, string] | [string, string, string])[], map: (name: string) => Any, info?: Info & { cycle?: boolean }): Mapped> { const name = checkDefaultKey(defaultKey, names); return setInfo>>({ type: 'mapped', defaultValue: { name, params: map(name).defaultValue as any }, select: Select(name, names, info), map }, info); } export function MappedStatic(defaultKey: keyof C, map: C, info?: Info & { options?: [keyof C, string][], cycle?: boolean }): Mapped> { const options: [string, string][] = info?.options ? info.options as [string, string][] : Object.keys(map).map(k => [k, map[k].label || stringToWords(k)]) as [string, string][]; const name = checkDefaultKey(defaultKey, options); return setInfo>>({ type: 'mapped', defaultValue: { name, params: map[name].defaultValue } as any, select: Select(name as string, options, info), map: key => map[key] }, info); } export interface ObjectList extends Base { type: 'object-list', element: Params, ctor(): T, getLabel(t: T): string } export function ObjectList(element: For, getLabel: (e: T) => string, info?: Info & { defaultValue?: T[], ctor?: () => T }): ObjectList> { return setInfo>>({ type: 'object-list', element: element as any as Params, getLabel, ctor: _defaultObjectListCtor, defaultValue: (info?.defaultValue) || [] }, info); } function _defaultObjectListCtor(this: ObjectList) { return getDefaultValues(this.element) as any; } function unsetGetValue() { throw new Error('getValue not set. Fix runtime.'); } // getValue needs to be assigned by a runtime because it might not be serializable export interface ValueRef extends Base<{ ref: string, getValue: () => T }> { type: 'value-ref', resolveRef: (ref: string, getData: (ref: string) => any) => T, // a provider because the list changes over time getOptions: (ctx: any) => Select['options'], } export function ValueRef(getOptions: ValueRef['getOptions'], resolveRef: ValueRef['resolveRef'], info?: Info & { defaultRef?: string }) { return setInfo>({ type: 'value-ref', defaultValue: { ref: info?.defaultRef ?? '', getValue: unsetGetValue as any }, getOptions, resolveRef }, info); } export interface DataRef extends Base<{ ref: string, getValue: () => T }> { type: 'data-ref' } export function DataRef(info?: Info & { defaultRef?: string }) { return setInfo>({ type: 'data-ref', defaultValue: { ref: info?.defaultRef ?? '', getValue: unsetGetValue as any } }, info); } export interface Converted extends Base { type: 'converted', converted: Any, /** converts from prop value to display value */ fromValue(v: T): C, /** converts from display value to prop value */ toValue(v: C): T } export function Converted(fromValue: (v: T) => C['defaultValue'], toValue: (v: C['defaultValue']) => T, converted: C): Converted { return setInfo({ type: 'converted', defaultValue: toValue(converted.defaultValue), converted, fromValue, toValue }, converted); } export interface Conditioned, C = { [k: string]: P }> extends Base { type: 'conditioned', select: Select, conditionParams: C conditionForValue(v: T): keyof C conditionedValue(v: T, condition: keyof C): T, } export function Conditioned, C extends {} = { [k: string]: P }>(defaultValue: T, conditionParams: C, conditionForValue: (v: T) => keyof C, conditionedValue: (v: T, condition: keyof C) => T, info?: Info): Conditioned { const options = Object.keys(conditionParams).map(k => [k, k]) as [string, string][]; return setInfo({ type: 'conditioned', select: Select(conditionForValue(defaultValue) as string, options, info), defaultValue, conditionParams, conditionForValue, conditionedValue }, info); } export interface Script extends Base { type: 'script' } export function Script(defaultValue: Script['defaultValue'], info?: Info): Script { return setInfo