|
@@ -7,38 +7,34 @@
|
|
|
|
|
|
import * as React from 'react'
|
|
|
import { ParamDefinition as PD } from 'mol-util/param-definition';
|
|
|
+import { camelCaseToWords } from 'mol-util/string';
|
|
|
|
|
|
export interface ParameterControlsProps<P extends PD.Params = PD.Params> {
|
|
|
params: P,
|
|
|
values: any,
|
|
|
onChange: ParamOnChange,
|
|
|
- isEnabled?: boolean,
|
|
|
+ isDisabled?: boolean,
|
|
|
onEnter?: () => void
|
|
|
}
|
|
|
|
|
|
export class ParameterControls<P extends PD.Params> extends React.PureComponent<ParameterControlsProps<P>, {}> {
|
|
|
render() {
|
|
|
- const common = {
|
|
|
- onChange: this.props.onChange,
|
|
|
- isEnabled: this.props.isEnabled,
|
|
|
- onEnter: this.props.onEnter,
|
|
|
- }
|
|
|
const params = this.props.params;
|
|
|
const values = this.props.values;
|
|
|
return <div style={{ width: '100%' }}>
|
|
|
{Object.keys(params).map(key => {
|
|
|
const param = params[key];
|
|
|
- if (param.type === 'value') return null;
|
|
|
- if (param.type === 'mapped') return <MappedControl param={param} key={key} {...common} name={key} value={values[key]} />
|
|
|
- if (param.type === 'group') return <GroupControl param={param} key={key} {...common} name={key} value={values[key]} />
|
|
|
- return <ParamWrapper control={controlFor(param)} param={param} key={key} {...common} name={key} value={values[key]} />
|
|
|
+ const Control = controlFor(param);
|
|
|
+ if (!Control) return null;
|
|
|
+ return <Control param={param} key={key} onChange={this.props.onChange} onEnter={this.props.onEnter} isDisabled={this.props.isDisabled} name={key} value={values[key]} />
|
|
|
})}
|
|
|
</div>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function controlFor(param: PD.Any): ValueControl {
|
|
|
+function controlFor(param: PD.Any): ParamControl | undefined {
|
|
|
switch (param.type) {
|
|
|
+ case 'value': return void 0;
|
|
|
case 'boolean': return BoolControl;
|
|
|
case 'number': return NumberControl;
|
|
|
case 'multi-select': return MultiSelectControl;
|
|
@@ -46,66 +42,57 @@ function controlFor(param: PD.Any): ValueControl {
|
|
|
case 'select': return SelectControl;
|
|
|
case 'text': return TextControl;
|
|
|
case 'interval': return IntervalControl;
|
|
|
- case 'group': throw Error('Must be handled separately');
|
|
|
- case 'mapped': throw Error('Must be handled separately');
|
|
|
+ case 'group': return GroupControl;
|
|
|
+ case 'mapped': return MappedControl;
|
|
|
+ case 'line-graph': return void 0;
|
|
|
}
|
|
|
throw new Error('not supported');
|
|
|
}
|
|
|
|
|
|
-type ParamWrapperProps = { name: string, value: any, param: PD.Base<any>, onChange: ParamOnChange, control: ValueControl, onEnter?: () => void, isEnabled?: boolean }
|
|
|
+// type ParamWrapperProps = { name: string, value: any, param: PD.Base<any>, onChange: ParamOnChange, control: ValueControl, onEnter?: () => void, isEnabled?: boolean }
|
|
|
+
|
|
|
export type ParamOnChange = (params: { param: PD.Base<any>, name: string, value: any }) => void
|
|
|
-type ValueControlProps<P extends PD.Base<any> = PD.Base<any>> = { value: any, param: P, isEnabled?: boolean, onChange: (v: any) => void, onEnter?: () => void }
|
|
|
-type ValueControl = React.ComponentClass<ValueControlProps<any>>
|
|
|
+export interface ParamProps<P extends PD.Base<any> = PD.Base<any>> { name: string, value: P['defaultValue'], param: P, isDisabled?: boolean, onChange: ParamOnChange, onEnter?: () => void }
|
|
|
+export type ParamControl = React.ComponentClass<ParamProps<any>>
|
|
|
|
|
|
-export class ParamWrapper extends React.PureComponent<ParamWrapperProps> {
|
|
|
- onChange = (value: any) => {
|
|
|
+export abstract class SimpleParam<P extends PD.Any> extends React.PureComponent<ParamProps<P>> {
|
|
|
+ protected update(value: any) {
|
|
|
this.props.onChange({ param: this.props.param, name: this.props.name, value });
|
|
|
}
|
|
|
|
|
|
+ abstract renderControl(): JSX.Element;
|
|
|
+
|
|
|
render() {
|
|
|
+ const label = this.props.param.label || camelCaseToWords(this.props.name);
|
|
|
return <div style={{ padding: '0 3px', borderBottom: '1px solid #ccc' }}>
|
|
|
- <div style={{ lineHeight: '20px', float: 'left' }} title={this.props.param.description}>{this.props.param.label}</div>
|
|
|
+ <div style={{ lineHeight: '20px', float: 'left' }} title={this.props.param.description}>{label}</div>
|
|
|
<div style={{ float: 'left', marginLeft: '5px' }}>
|
|
|
- <this.props.control value={this.props.value} param={this.props.param} onChange={this.onChange} onEnter={this.props.onEnter} isEnabled={this.props.isEnabled} />
|
|
|
+ {this.renderControl()}
|
|
|
</div>
|
|
|
<div style={{ clear: 'both' }} />
|
|
|
</div>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export class BoolControl extends React.PureComponent<ValueControlProps> {
|
|
|
- onClick = () => {
|
|
|
- this.props.onChange(!this.props.value);
|
|
|
- }
|
|
|
-
|
|
|
- render() {
|
|
|
- return <button onClick={this.onClick} disabled={!this.props.isEnabled}>{this.props.value ? '✓ On' : '✗ Off'}</button>;
|
|
|
+export class BoolControl extends SimpleParam<PD.Boolean> {
|
|
|
+ onClick = () => { this.update(!this.props.value); }
|
|
|
+ renderControl() {
|
|
|
+ return <button onClick={this.onClick} disabled={this.props.isDisabled}>{this.props.value ? '✓ On' : '✗ Off'}</button>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export class NumberControl extends React.PureComponent<ValueControlProps<PD.Numeric>, { value: string }> {
|
|
|
- // state = { value: this.props.value }
|
|
|
- onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
- this.props.onChange(+e.target.value);
|
|
|
- // this.setState({ value: e.target.value });
|
|
|
- }
|
|
|
-
|
|
|
- render() {
|
|
|
- return <input type='range'
|
|
|
- value={'' + this.props.value}
|
|
|
- min={this.props.param.min}
|
|
|
- max={this.props.param.max}
|
|
|
- step={this.props.param.step}
|
|
|
- onChange={this.onChange}
|
|
|
- />;
|
|
|
+export class NumberControl extends SimpleParam<PD.Numeric> {
|
|
|
+ onChange = (e: React.ChangeEvent<HTMLInputElement>) => { this.update(+e.target.value); }
|
|
|
+ renderControl() {
|
|
|
+ return <input type='range' value={'' + this.props.value} min={this.props.param.min} max={this.props.param.max} step={this.props.param.step} onChange={this.onChange} disabled={this.props.isDisabled} />;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export class TextControl extends React.PureComponent<ValueControlProps<PD.Text>> {
|
|
|
+export class TextControl extends SimpleParam<PD.Text> {
|
|
|
onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
const value = e.target.value;
|
|
|
if (value !== this.props.value) {
|
|
|
- this.props.onChange(value);
|
|
|
+ this.update(value);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -116,67 +103,60 @@ export class TextControl extends React.PureComponent<ValueControlProps<PD.Text>>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- render() {
|
|
|
+ renderControl() {
|
|
|
return <input type='text'
|
|
|
value={this.props.value || ''}
|
|
|
onChange={this.onChange}
|
|
|
onKeyPress={this.props.onEnter ? this.onKeyPress : void 0}
|
|
|
+ disabled={this.props.isDisabled}
|
|
|
/>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export class SelectControl extends React.PureComponent<ValueControlProps<PD.Select<any>>> {
|
|
|
- onChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
- this.setState({ value: e.target.value });
|
|
|
- this.props.onChange(e.target.value);
|
|
|
- }
|
|
|
-
|
|
|
- render() {
|
|
|
- return <select value={this.props.value || ''} onChange={this.onChange}>
|
|
|
+export class SelectControl extends SimpleParam<PD.Select<any>> {
|
|
|
+ onChange = (e: React.ChangeEvent<HTMLSelectElement>) => { this.update(e.target.value); }
|
|
|
+ renderControl() {
|
|
|
+ return <select value={this.props.value || ''} onChange={this.onChange} disabled={this.props.isDisabled}>
|
|
|
{this.props.param.options.map(([value, label]) => <option key={value} value={value}>{label}</option>)}
|
|
|
</select>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-export class MultiSelectControl extends React.PureComponent<ValueControlProps<PD.MultiSelect<any>>> {
|
|
|
+export class MultiSelectControl extends SimpleParam<PD.MultiSelect<any>> {
|
|
|
// onChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
// this.setState({ value: e.target.value });
|
|
|
// this.props.onChange(e.target.value);
|
|
|
// }
|
|
|
|
|
|
- render() {
|
|
|
+ renderControl() {
|
|
|
return <span>multiselect TODO</span>;
|
|
|
- // return <select value={this.props.value || ''} onChange={this.onChange}>
|
|
|
- // {this.props.param.options.map(([value, label]) => <option key={label} value={value}>{label}</option>)}
|
|
|
- // </select>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export class IntervalControl extends React.PureComponent<ValueControlProps<PD.Interval>> {
|
|
|
+export class IntervalControl extends SimpleParam<PD.Interval> {
|
|
|
// onChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
// this.setState({ value: e.target.value });
|
|
|
// this.props.onChange(e.target.value);
|
|
|
// }
|
|
|
|
|
|
- render() {
|
|
|
+ renderControl() {
|
|
|
return <span>interval TODO</span>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export class ColorControl extends React.PureComponent<ValueControlProps<PD.Color>> {
|
|
|
+export class ColorControl extends SimpleParam<PD.Color> {
|
|
|
// onChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
// this.setState({ value: e.target.value });
|
|
|
// this.props.onChange(e.target.value);
|
|
|
// }
|
|
|
|
|
|
- render() {
|
|
|
+ renderControl() {
|
|
|
return <span>color TODO</span>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type GroupWrapperProps = { name: string, value: PD.Group<any>['defaultValue'], param: PD.Group<any>, onChange: ParamOnChange, onEnter?: () => void, isEnabled?: boolean }
|
|
|
-export class GroupControl extends React.PureComponent<GroupWrapperProps> {
|
|
|
+export class GroupControl extends React.PureComponent<ParamProps<PD.Group<any>>> {
|
|
|
change(value: PD.Mapped<any>['defaultValue'] ) {
|
|
|
this.props.onChange({ name: this.props.name, param: this.props.param, value });
|
|
|
}
|
|
@@ -191,13 +171,12 @@ export class GroupControl extends React.PureComponent<GroupWrapperProps> {
|
|
|
const params = this.props.param.params;
|
|
|
|
|
|
return <div>
|
|
|
- <ParameterControls params={params} onChange={this.onChangeParam} values={value.params} onEnter={this.props.onEnter} isEnabled={this.props.isEnabled} />
|
|
|
+ <ParameterControls params={params} onChange={this.onChangeParam} values={value.params} onEnter={this.props.onEnter} isDisabled={this.props.isDisabled} />
|
|
|
</div>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type MappedWrapperProps = { name: string, value: PD.Mapped<any>['defaultValue'], param: PD.Mapped<any>, onChange: ParamOnChange, onEnter?: () => void, isEnabled?: boolean }
|
|
|
-export class MappedControl extends React.PureComponent<MappedWrapperProps> {
|
|
|
+export class MappedControl extends React.PureComponent<ParamProps<PD.Mapped<any>>> {
|
|
|
change(value: PD.Mapped<any>['defaultValue'] ) {
|
|
|
this.props.onChange({ name: this.props.name, param: this.props.param, value });
|
|
|
}
|
|
@@ -214,16 +193,20 @@ export class MappedControl extends React.PureComponent<MappedWrapperProps> {
|
|
|
render() {
|
|
|
const value: PD.Mapped<any>['defaultValue'] = this.props.value;
|
|
|
const param = this.props.param.map(value.name);
|
|
|
+ const Mapped = controlFor(param);
|
|
|
+
|
|
|
+ const select = <SelectControl param={this.props.param.select}
|
|
|
+ isDisabled={this.props.isDisabled} onChange={this.onChangeName} onEnter={this.props.onEnter}
|
|
|
+ name={'name'} value={value.name} />
|
|
|
+
|
|
|
+ if (!Mapped) {
|
|
|
+ return select;
|
|
|
+ }
|
|
|
|
|
|
return <div>
|
|
|
- <ParamWrapper control={SelectControl} param={this.props.param.select}
|
|
|
- isEnabled={this.props.isEnabled} onChange={this.onChangeName} onEnter={this.props.onEnter}
|
|
|
- name={'name'} value={value.name} />
|
|
|
+ {select}
|
|
|
<div style={{ borderLeft: '5px solid #777', paddingLeft: '5px' }}>
|
|
|
- {param.type === 'group'
|
|
|
- ? <GroupControl param={param} value={value} name='param' onChange={this.onChangeParam} onEnter={this.props.onEnter} isEnabled={this.props.isEnabled} />
|
|
|
- : param.type === 'mapped' || param.type === 'value' ? null
|
|
|
- : <ParamWrapper control={controlFor(param)} param={param} onChange={this.onChangeParam} onEnter={this.props.onEnter} isEnabled={this.props.isEnabled} name={'value'} value={value} />}
|
|
|
+ <Mapped param={param} value={value} name='param' onChange={this.onChangeParam} onEnter={this.props.onEnter} isDisabled={this.props.isDisabled} />
|
|
|
</div>
|
|
|
</div>
|
|
|
}
|