/** * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal * @author Alexander Rose */ import { Vec2, Vec3 } from '../../mol-math/linear-algebra'; import { Color } from '../../mol-util/color'; import { ColorListName, getColorListFromName } from '../../mol-util/color/lists'; import { memoize1 } from '../../mol-util/memoize'; import { ParamDefinition as PD } from '../../mol-util/param-definition'; import { camelCaseToWords } from '../../mol-util/string'; import * as React from 'react'; import LineGraphComponent from './line-graph/line-graph-component'; import { Slider, Slider2 } from './slider'; import { NumericInput, IconButton, ControlGroup } from './common'; import { _Props, _State } from '../base'; import { legendFor } from './legend'; import { Legend as LegendData } from '../../mol-util/legend'; import { CombinedColorControl, ColorValueOption, ColorOptions } from './color'; import { getPrecision } from '../../mol-util/number'; export interface ParameterControlsProps

{ params: P, values: any, onChange: ParamOnChange, isDisabled?: boolean, onEnter?: () => void } export class ParameterControls

extends React.PureComponent, {}> { render() { const params = this.props.params; const values = this.props.values; const keys = Object.keys(params); if (keys.length === 0 || values === undefined) return null; return <> {keys.map(key => { const param = params[key]; if (param.isHidden) return null; const Control = controlFor(param); if (!Control) return null; return })} ; } } function controlFor(param: PD.Any): ParamControl | undefined { switch (param.type) { case 'value': return void 0; case 'boolean': return BoolControl; case 'number': return typeof param.min !== 'undefined' && typeof param.max !== 'undefined' ? NumberRangeControl : NumberInputControl; case 'converted': return ConvertedControl; case 'conditioned': return ConditionedControl; case 'multi-select': return MultiSelectControl; case 'color': return CombinedColorControl; case 'color-list': return ColorListControl; case 'vec3': return Vec3Control; case 'file': return FileControl; case 'file-list': return FileListControl; case 'select': return SelectControl; case 'text': return TextControl; case 'interval': return typeof param.min !== 'undefined' && typeof param.max !== 'undefined' ? BoundedIntervalControl : IntervalControl; case 'group': return GroupControl; case 'mapped': return MappedControl; case 'line-graph': return LineGraphControl; case 'script': return ScriptControl; case 'object-list': return ObjectListControl; default: const _: never = param; console.warn(`${_} has no associated UI component`); return void 0; } } export class ParamHelp extends React.PureComponent<{ legend?: L, description?: string }> { render() { const { legend, description } = this.props const Legend = legend && legendFor(legend) return

{description}
{Legend &&
}
} } export type ParamOnChange = (params: { param: PD.Base, name: string, value: any }) => void export interface ParamProps

= PD.Base> { name: string, value: P['defaultValue'], param: P, isDisabled?: boolean, onChange: ParamOnChange, onEnter?: () => void } export type ParamControl = React.ComponentClass> export abstract class SimpleParam

extends React.PureComponent, { isExpanded: boolean }> { state = { isExpanded: false }; protected update(value: P['defaultValue']) { this.props.onChange({ param: this.props.param, name: this.props.name, value }); } abstract renderControl(): JSX.Element; private get className() { const className = ['msp-control-row']; if (this.props.param.shortLabel) className.push('msp-control-label-short') if (this.props.param.twoColumns) className.push('msp-control-col-2') return className.join(' ') } toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded }); render() { const label = this.props.param.label || camelCaseToWords(this.props.name); const help = this.props.param.help ? this.props.param.help(this.props.value) : { description: this.props.param.description, legend: this.props.param.legend } const desc = this.props.param.description; const hasHelp = help.description || help.legend return <>

{label} {hasHelp && }
{this.renderControl()}
{hasHelp && this.state.isExpanded &&
} ; } } export class BoolControl extends SimpleParam { onClick = (e: React.MouseEvent) => { this.update(!this.props.value); e.currentTarget.blur(); } renderControl() { return ; } } export class LineGraphControl extends React.PureComponent, { isExpanded: boolean, isOverPoint: boolean, message: string }> { state = { isExpanded: false, isOverPoint: false, message: `${this.props.param.defaultValue.length} points`, } onHover = (point?: Vec2) => { this.setState({ isOverPoint: !this.state.isOverPoint }); if (point) { this.setState({ message: `(${point[0].toFixed(2)}, ${point[1].toFixed(2)})` }); return; } this.setState({ message: `${this.props.value.length} points` }); } onDrag = (point: Vec2) => { this.setState({ message: `(${point[0].toFixed(2)}, ${point[1].toFixed(2)})` }); } onChange = (value: PD.LineGraph['defaultValue']) => { this.props.onChange({ name: this.props.name, param: this.props.param, value: value }); } toggleExpanded = (e: React.MouseEvent) => { this.setState({ isExpanded: !this.state.isExpanded }); e.currentTarget.blur(); } render() { const label = this.props.param.label || camelCaseToWords(this.props.name); return <>
{label}
; } } export class NumberInputControl extends React.PureComponent> { state = { value: '0' }; update = (value: number) => { const p = getPrecision(this.props.param.step || 0.01) value = parseFloat(value.toFixed(p)) this.props.onChange({ param: this.props.param, name: this.props.name, value }); } render() { const placeholder = this.props.param.label || camelCaseToWords(this.props.name); const label = this.props.param.label || camelCaseToWords(this.props.name); const p = getPrecision(this.props.param.step || 0.01) return
{label}
; } } export class NumberRangeControl extends SimpleParam { onChange = (v: number) => { this.update(v); } renderControl() { const value = typeof this.props.value === 'undefined' ? this.props.param.defaultValue : this.props.value; return } } export class TextControl extends SimpleParam { onChange = (e: React.ChangeEvent) => { const value = e.target.value; if (value !== this.props.value) { this.update(value); } } onKeyPress = (e: React.KeyboardEvent) => { if ((e.keyCode === 13 || e.charCode === 13)) { if (this.props.onEnter) this.props.onEnter(); } e.stopPropagation(); } renderControl() { const placeholder = this.props.param.label || camelCaseToWords(this.props.name); return ; } } export class PureSelectControl extends React.PureComponent> & { title?: string }> { protected update(value: string | number) { this.props.onChange({ param: this.props.param, name: this.props.name, value }); } onChange = (e: React.ChangeEvent) => { if (typeof this.props.param.defaultValue === 'number') { this.update(parseInt(e.target.value, 10)); } else { this.update(e.target.value); } } render() { const isInvalid = this.props.value !== void 0 && !this.props.param.options.some(e => e[0] === this.props.value); return ; } } export class SelectControl extends SimpleParam> { onChange = (e: React.ChangeEvent) => { if (typeof this.props.param.defaultValue === 'number') { this.update(parseInt(e.target.value, 10)); } else { this.update(e.target.value); } } renderControl() { const isInvalid = this.props.value !== void 0 && !this.props.param.options.some(e => e[0] === this.props.value); return ; } } export class IntervalControl extends React.PureComponent, { isExpanded: boolean }> { state = { isExpanded: false } components = { 0: PD.Numeric(0, { step: this.props.param.step }, { label: 'Min' }), 1: PD.Numeric(0, { step: this.props.param.step }, { label: 'Max' }) } change(value: PD.MultiSelect['defaultValue']) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } componentChange: ParamOnChange = ({ name, value }) => { const v = [...this.props.value]; v[+name] = value; this.change(v); } toggleExpanded = (e: React.MouseEvent) => { this.setState({ isExpanded: !this.state.isExpanded }); e.currentTarget.blur(); } render() { const v = this.props.value; const label = this.props.param.label || camelCaseToWords(this.props.name); const p = getPrecision(this.props.param.step || 0.01) const value = `[${v[0].toFixed(p)}, ${v[1].toFixed(p)}]`; return <>
{label}
; } } export class BoundedIntervalControl extends SimpleParam { onChange = (v: [number, number]) => { this.update(v); } renderControl() { return ; } } export class ColorControl extends SimpleParam { onChange = (e: React.ChangeEvent) => { this.update(Color(parseInt(e.target.value))); } stripStyle(): React.CSSProperties { return { background: Color.toStyle(this.props.value), position: 'absolute', bottom: '0', height: '4px', right: '0', left: '0' }; } renderControl() { return
; } } const colorGradientInterpolated = memoize1((colors: Color[]) => { const styles = colors.map(c => Color.toStyle(c)) return `linear-gradient(to right, ${styles.join(', ')})` }); const colorGradientBanded = memoize1((colors: Color[]) => { const n = colors.length const styles: string[] = [`${Color.toStyle(colors[0])} ${100 * (1 / n)}%`] for (let i = 1, il = n - 1; i < il; ++i) { styles.push( `${Color.toStyle(colors[i])} ${100 * (i / n)}%`, `${Color.toStyle(colors[i])} ${100 * ((i + 1) / n)}%` ) } styles.push(`${Color.toStyle(colors[n - 1])} ${100 * ((n - 1) / n)}%`) return `linear-gradient(to right, ${styles.join(', ')})` }); function colorGradient(name: ColorListName, banded: boolean) { const { list, type } = getColorListFromName(name) return type === 'qualitative' ? colorGradientBanded(list) : colorGradientInterpolated(list) } export class ColorListControl extends SimpleParam> { onChange = (e: React.ChangeEvent) => { this.update(e.target.value); } stripStyle(): React.CSSProperties { return { background: colorGradient(this.props.value, true), position: 'absolute', bottom: '0', height: '4px', right: '0', left: '0' }; } renderControl() { return
; } } export class Vec3Control extends React.PureComponent, { isExpanded: boolean }> { state = { isExpanded: false } components = { 0: PD.Numeric(0, { step: this.props.param.step }, { label: (this.props.param.fieldLabels && this.props.param.fieldLabels.x) || 'X' }), 1: PD.Numeric(0, { step: this.props.param.step }, { label: (this.props.param.fieldLabels && this.props.param.fieldLabels.y) || 'Y' }), 2: PD.Numeric(0, { step: this.props.param.step }, { label: (this.props.param.fieldLabels && this.props.param.fieldLabels.z) || 'Z' }) } change(value: PD.MultiSelect['defaultValue']) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } componentChange: ParamOnChange = ({ name, value }) => { const v = Vec3.copy(Vec3.zero(), this.props.value); v[+name] = value; this.change(v); } toggleExpanded = (e: React.MouseEvent) => { this.setState({ isExpanded: !this.state.isExpanded }); e.currentTarget.blur(); } render() { const v = this.props.value; const label = this.props.param.label || camelCaseToWords(this.props.name); const p = getPrecision(this.props.param.step || 0.01) const value = `[${v[0].toFixed(p)}, ${v[1].toFixed(p)}, ${v[2].toFixed(p)}]`; return <>
{label}
; } } export class FileControl extends React.PureComponent> { change(value: File) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } onChangeFile = (e: React.ChangeEvent) => { this.change(e.target.files![0]); } render() { const value = this.props.value; return
{value ? value.name : 'Select a file...'}
} } export class FileListControl extends React.PureComponent> { change(value: FileList) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } onChangeFileList = (e: React.ChangeEvent) => { this.change(e.target.files!); } render() { const value = this.props.value; const names: string[] = [] if (value) { for (let i = 0, il = value.length; i < il; ++i) { names.push(value[i].name) } } const label = names.length === 0 ? 'Select files...' : names.length === 1 ? names[0] : `${names.length} files selected` return
{label}
} } export class MultiSelectControl extends React.PureComponent>, { isExpanded: boolean }> { state = { isExpanded: false } change(value: PD.MultiSelect['defaultValue']) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } toggle(key: string) { return (e: React.MouseEvent) => { if (this.props.value.indexOf(key) < 0) this.change(this.props.value.concat(key)); else this.change(this.props.value.filter(v => v !== key)); e.currentTarget.blur(); } } toggleExpanded = (e: React.MouseEvent) => { this.setState({ isExpanded: !this.state.isExpanded }); e.currentTarget.blur(); } render() { const current = this.props.value; const label = this.props.param.label || camelCaseToWords(this.props.name); return <>
{label}
{this.props.param.options.map(([value, label]) => { const sel = current.indexOf(value) >= 0; return
})}
; } } export class GroupControl extends React.PureComponent>, { isExpanded: boolean }> { state = { isExpanded: !!this.props.param.isExpanded } change(value: any) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } onChangeParam: ParamOnChange = e => { this.change({ ...this.props.value, [e.name]: e.value }); } toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded }); render() { const params = this.props.param.params; // Do not show if there are no params. if (Object.keys(params).length === 0) return null; const label = this.props.param.label || camelCaseToWords(this.props.name); const controls = ; if (this.props.param.isFlat) { return controls; } return
{this.state.isExpanded &&
{controls}
}
} } export class MappedControl extends React.PureComponent>> { private valuesCache: { [name: string]: PD.Values } = {} private setValues(name: string, values: PD.Values) { this.valuesCache[name] = values } private getValues(name: string) { if (name in this.valuesCache) { return this.valuesCache[name] } else { return this.props.param.map(name).defaultValue } } change(value: PD.Mapped['defaultValue']) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } onChangeName: ParamOnChange = e => { this.change({ name: e.value, params: this.getValues(e.value) }); } onChangeParam: ParamOnChange = e => { this.setValues(this.props.value.name, e.value) this.change({ name: this.props.value.name, params: e.value }); } render() { const value: PD.Mapped['defaultValue'] = this.props.value; const param = this.props.param.map(value.name); const label = this.props.param.label || camelCaseToWords(this.props.name); const Mapped = controlFor(param); const help = this.props.param.help const select = help ? { ...this.props.param.select, help: (name: any) => help({ name, params: this.getValues(name) }) } : this.props.param.select const Select = if (!Mapped) { return Select; } return <> {Select} } } class ObjectListEditor extends React.PureComponent<{ params: PD.Params, value: object, isUpdate?: boolean, apply: (value: any) => void, isDisabled?: boolean }, { params: PD.Params, value: object, current: object }> { state = { params: {}, value: void 0 as any, current: void 0 as any }; onChangeParam: ParamOnChange = e => { this.setState({ current: { ...this.state.current, [e.name]: e.value } }); } apply = () => { this.props.apply(this.state.current); } static getDerivedStateFromProps(props: _Props, state: _State): _State | null { if (props.params === state.params && props.value === state.value) return null; return { params: props.params, value: props.value, current: props.value }; } render() { return <> ; } } class ObjectListItem extends React.PureComponent<{ param: PD.ObjectList, value: object, index: number, actions: ObjectListControl['actions'], isDisabled?: boolean }, { isExpanded: boolean }> { state = { isExpanded: false }; update = (v: object) => { this.setState({ isExpanded: false }); this.props.actions.update(v, this.props.index); } moveUp = () => { this.props.actions.move(this.props.index, -1); }; moveDown = () => { this.props.actions.move(this.props.index, 1); }; remove = () => { this.setState({ isExpanded: false }); this.props.actions.remove(this.props.index); }; toggleExpanded = (e: React.MouseEvent) => { this.setState({ isExpanded: !this.state.isExpanded }); e.currentTarget.blur(); }; static getDerivedStateFromProps(props: _Props, state: _State): _State | null { if (props.params === state.params && props.value === state.value) return null; return { params: props.params, value: props.value, current: props.value }; } render() { return <>
{this.state.isExpanded &&
} ; } } export class ObjectListControl extends React.PureComponent, { isExpanded: boolean }> { state = { isExpanded: false } change(value: any) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } add = (v: object) => { this.change([...this.props.value, v]); }; actions = { update: (v: object, i: number) => { const value = this.props.value.slice(0); value[i] = v; this.change(value); }, move: (i: number, dir: -1 | 1) => { let xs = this.props.value; if (xs.length === 1) return; let j = (i + dir) % xs.length; if (j < 0) j += xs.length; xs = xs.slice(0); const t = xs[i]; xs[i] = xs[j]; xs[j] = t; this.change(xs); }, remove: (i: number) => { const xs = this.props.value; const update: object[] = []; for (let j = 0; j < xs.length; j++) { if (i !== j) update.push(xs[j]); } this.change(update); } } toggleExpanded = (e: React.MouseEvent) => { this.setState({ isExpanded: !this.state.isExpanded }); e.currentTarget.blur(); }; render() { const v = this.props.value; const label = this.props.param.label || camelCaseToWords(this.props.name); const value = `${v.length} item${v.length !== 1 ? 's' : ''}`; return <>
{label}
{this.state.isExpanded &&
{this.props.value.map((v, i) => )}
} ; } } export class ConditionedControl extends React.PureComponent>> { change(value: PD.Conditioned['defaultValue']) { this.props.onChange({ name: this.props.name, param: this.props.param, value }); } onChangeCondition: ParamOnChange = e => { this.change(this.props.param.conditionedValue(this.props.value, e.value)); } onChangeParam: ParamOnChange = e => { this.change(e.value); } render() { const value = this.props.value; const condition = this.props.param.conditionForValue(value) as string const param = this.props.param.conditionParams[condition]; const label = this.props.param.label || camelCaseToWords(this.props.name); const Conditioned = controlFor(param); const select = if (!Conditioned) { return select; } return <> {select} } } export class ConvertedControl extends React.PureComponent>> { onChange: ParamOnChange = e => { this.props.onChange({ name: this.props.name, param: this.props.param, value: this.props.param.toValue(e.value) }); } render() { const value = this.props.param.fromValue(this.props.value); const Converted = controlFor(this.props.param.converted); if (!Converted) return null; return } } export class ScriptControl extends SimpleParam { onChange = (e: React.ChangeEvent) => { const value = e.target.value; if (value !== this.props.value.expression) { this.update({ language: this.props.value.language, expression: value }); } } onKeyPress = (e: React.KeyboardEvent) => { if ((e.keyCode === 13 || e.charCode === 13)) { if (this.props.onEnter) this.props.onEnter(); } e.stopPropagation(); } renderControl() { // TODO: improve! const placeholder = this.props.param.label || camelCaseToWords(this.props.name); return ; } }