/** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose * @author David Sehnal */ import { Color as ColorData } from './color'; import { shallowEqual } from './index'; import { Vec2 as Vec2Data, Vec3 as Vec3Data } from '../mol-math/linear-algebra'; import { deepClone } from './object'; import { Script as ScriptData } from '../mol-script/script'; import { Legend } from './legend'; export namespace ParamDefinition { export interface Info { label?: string, description?: string, legend?: Legend, fieldLabels?: { [name: string]: string }, isHidden?: boolean, shortLabel?: boolean, twoColumns?: boolean, help?: (value: any) => { description?: string, legend?: Legend } } 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.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: [T, string][] } export function Select(defaultValue: T, options: [T, string][], info?: Info): Select { return setInfo>({ type: 'select', defaultValue, options }, info) } export interface ColorList extends Base { type: 'color-list' /** array of (value, label) tuples */ options: [T, string][] } export function ColorList(defaultValue: T, options: [T, string][], info?: Info): ColorList { return setInfo>({ type: 'color-list', defaultValue, options }, info) } export interface MultiSelect extends Base { type: 'multi-select' /** array of (value, label) tuples */ options: [E, string][] } export function MultiSelect(defaultValue: T, options: [E, string][], info?: Info): MultiSelect { return setInfo>({ type: 'multi-select', defaultValue, options }, info) } 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' } export function Text(defaultValue: string = '', info?: Info): Text { return setInfo>({ type: 'text', defaultValue: defaultValue as any }, info) } export interface Color extends Base { type: 'color' } export function Color(defaultValue: ColorData, info?: Info): Color { return setInfo({ type: 'color', defaultValue }, info) } export interface Vec3 extends Base { type: 'vec3' } export function Vec3(defaultValue: Vec3Data, info?: Info): Vec3 { return setInfo({ type: 'vec3', defaultValue }, info) } export interface FileParam extends Base { type: 'file', accept?: string } export function File(info?: Info & { accept?: string }): FileParam { const ret = setInfo({ type: 'file', defaultValue: void 0 as any }, info); if (info && 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' } export function Numeric(defaultValue: number, range?: { min?: number, max?: number, step?: number }, info?: Info): Numeric { return setInfo(setRange({ type: 'number', defaultValue }, range), info) } 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' } export function LineGraph(defaultValue: Vec2Data[], info?: Info): LineGraph { return setInfo({ type: 'line-graph', defaultValue }, info) } export interface Group extends Base { type: 'group', params: Params, isExpanded?: boolean, isFlat?: boolean } export function Group(params: For, info?: Info & { isExpanded?: boolean, isFlat?: boolean }): Group> { const ret = setInfo>>({ type: 'group', defaultValue: getDefaultValues(params as any as Params) as any, params: params as any as Params }, info); if (info && info.isExpanded) ret.isExpanded = info.isExpanded; if (info && info.isFlat) ret.isFlat = info.isFlat; return ret; } 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][], map: (name: string) => Any, info?: Info): Mapped> { return setInfo>>({ type: 'mapped', defaultValue: { name: defaultKey, params: map(defaultKey).defaultValue as any }, select: Select(defaultKey, names, info), map }, info); } export function MappedStatic(defaultKey: keyof C, map: C, info?: Info & { options?: [keyof C, string][] }): Mapped> { const options: [string, string][] = info && info.options ? info.options as [string, string][] : Object.keys(map).map(k => [k, k]) as [string, string][]; return setInfo>>({ type: 'mapped', defaultValue: { name: defaultKey, params: map[defaultKey].defaultValue } as any, select: Select(defaultKey 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 && info.defaultValue) || [] }, info); } function _defaultObjectListCtor(this: ObjectList) { return getDefaultValues(this.element) as any; } 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 { type: 'converted', defaultValue: toValue(converted.defaultValue), converted, fromValue, toValue }; } 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 = { [k: string]: P }>(defaultValue: T, conditionParams: C, conditionForValue: (v: T) => keyof C, conditionedValue: (v: T, condition: keyof C) => T): Conditioned { const options = Object.keys(conditionParams).map(k => [k, k]) as [string, string][]; return { type: 'conditioned', select: Select(conditionForValue(defaultValue) as string, options), defaultValue, conditionParams, conditionForValue, conditionedValue }; } export interface Script extends Base { type: 'script' } export function Script(defaultValue: Script['defaultValue'], info?: Info): Script { return setInfo