diff --git a/src/components/dialog/edit-widget-control-creator.js b/src/components/dialog/edit-widget-control-creator.js new file mode 100644 index 0000000..9b430e0 --- /dev/null +++ b/src/components/dialog/edit-widget-control-creator.js @@ -0,0 +1,90 @@ + + +import React from 'react'; + +import EditWidgetTextControl from './edit-widget-text-control'; +import EditWidgetColorControl from './edit-widget-color-control'; +import EditWidgetTimeControl from './edit-widget-time-control'; +import EditImageWidgetControl from './edit-widget-image-control'; +import EditWidgetSimulatorControl from './edit-widget-simulator-control'; +import EditWidgetSignalControl from './edit-widget-signal-control'; +import EditWidgetSignalsControl from './edit-widget-signals-control'; +import EditWidgetOrientation from './edit-widget-orientation'; + +export default function createControls(widgetType = null, widget = null, sessionToken = null, files = null, validateForm, simulation, handleChange) { + // Use a list to concatenate the controls according to the widget type + var dialogControls = []; + + switch(widgetType) { + case 'Value': { + let valueBoundOnChange = (e) => { + handleChange([e, {target: {id: 'signal', value: 0}}]); + } + dialogControls.push( + validateForm(id)} simulation={simulation} handleChange={(e) => valueBoundOnChange(e)} />, + validateForm(id)} simulation={simulation} handleChange={(e) => handleChange(e)} /> + ) + } + break; + case 'Plot': { + let plotBoundOnChange = (e) => { + handleChange([e, {target: {id: 'signals', value: []}}]); + } + dialogControls.push( + validateForm(id)} simulation={simulation} handleChange={(e) => handleChange(e)} />, + validateForm(id)} simulation={simulation} handleChange={(e) => plotBoundOnChange(e)} />, + validateForm(id)} simulation={simulation} handleChange={(e) => handleChange(e)} />, + handleChange(e)} />) + } + break; + case 'Table': { + dialogControls.push( + validateForm(id)} simulation={simulation} handleChange={(e) => handleChange(e)} />) + } + break; + case 'Image': { + dialogControls.push( + validateForm(id)} simulation={simulation} handleChange={(e) => handleChange(e)} />) + } + break; + case 'Gauge': { + let gaugeBoundOnChange = (e) => { + handleChange([e, {target: {id: 'signal', value: ''}}]); + } + dialogControls.push( + validateForm(id)} simulation={simulation} handleChange={(e) => gaugeBoundOnChange(e) } />, + validateForm(id)} simulation={simulation} handleChange={(e) => handleChange(e)} />) + } + break; + case 'PlotTable': { + let plotTableBoundOnChange = (e) => { + handleChange([e, {target: {id: 'preselectedSignals', value: []}}]); + } + dialogControls.push( + validateForm(id)} simulation={simulation} handleChange={(e) => plotTableBoundOnChange(e)} />, + validateForm(id)} simulation={simulation} handleChange={(e) => handleChange(e)} />, + handleChange(e)} />) + } + break; + case 'Slider': { + dialogControls.push( + validateForm(id)} simulation={simulation} handleChange={(e) => handleChange(e)} />) + } + break; + case 'Button': { + dialogControls.push( + validateForm(id)} handleChange={(e) => handleChange(e)} />, + validateForm(id)} handleChange={(e) => handleChange(e)} />) + } + break; + case 'Box': { + dialogControls.push( + validateForm(id)} handleChange={(e) => handleChange(e)} />) + } + break; + default: + console.log('Non-valid widget type'); + } + + return dialogControls; +} \ No newline at end of file diff --git a/src/components/dialog/edit-widget.js b/src/components/dialog/edit-widget.js index eeabba3..8c2277f 100644 --- a/src/components/dialog/edit-widget.js +++ b/src/components/dialog/edit-widget.js @@ -24,14 +24,7 @@ import { FormGroup, FormControl, ControlLabel } from 'react-bootstrap'; import Dialog from './dialog'; -import EditWidgetTextControl from './edit-widget-text-control'; -import EditWidgetColorControl from './edit-widget-color-control'; -import EditWidgetTimeControl from './edit-widget-time-control'; -import EditImageWidgetControl from './edit-widget-image-control'; -import EditWidgetSimulatorControl from './edit-widget-simulator-control'; -import EditWidgetSignalControl from './edit-widget-signal-control'; -import EditWidgetSignalsControl from './edit-widget-signals-control'; -import EditWidgetOrientation from './edit-widget-orientation'; +import createControls from './edit-widget-control-creator'; class EditWidgetDialog extends Component { static propTypes = { @@ -62,10 +55,16 @@ class EditWidgetDialog extends Component { } } - handleChange(e, index) { - var update = this.state.temporal; - update[e.target.id] = e.target.value; - this.setState({ temporal: update }); + handleChange(e) { + if (e.constructor === Array) { + // Every property in the array will be updated + let changes = e.reduce( (changesObject, event) => { changesObject[event.target.id] = event.target.value; return changesObject }, {}); + this.setState({ temporal: Object.assign({}, this.state.temporal, changes ) }); + } else { + let changeObject = {}; + changeObject[e.target.id] = e.target.value; + this.setState({ temporal: Object.assign({}, this.state.temporal, changeObject ) }); + } } resetState() { @@ -88,55 +87,17 @@ class EditWidgetDialog extends Component { } render() { - // Use a list to concatenate the controls according to the widget type - var dialogControls = []; - + + let controls = null; if (this.props.widget) { - if (this.props.widget.type === 'Value') { - dialogControls.push( - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} />, - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} /> - ) - } else if (this.props.widget.type === 'Plot') { - dialogControls.push( - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e, index) => this.handleChange(e, index)} />, - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} />, - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} />, - this.handleChange(e)} /> - ) - } else if (this.props.widget.type === 'Table') { - dialogControls.push( - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} /> - ) - } else if (this.props.widget.type === 'Image') { - dialogControls.push( - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e, index) => this.handleChange(e, index)} /> - ) - } else if (this.props.widget.type === 'Gauge') { - dialogControls.push( - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} />, - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} /> - ) - } else if (this.props.widget.type === 'PlotTable') { - dialogControls.push( - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} />, - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} />, - this.handleChange(e)} /> - ) - } else if (this.props.widget.type === 'Slider') { - dialogControls.push( - this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} />, - ) - } else if (this.props.widget.type === 'Button') { - dialogControls.push( - this.validateForm(id)} handleChange={(e, index) => this.handleChange(e, index)} />, - this.validateForm(id)} handleChange={(e, index) => this.handleChange(e, index)} /> - ) - } else if (this.props.widget.type === 'Box') { - dialogControls.push( - this.validateForm(id)} handleChange={(e, index) => this.handleChange(e, index)} /> - ) - } + controls = createControls( + this.props.widget.type, + this.state.temporal, + this.props.sessionToken, + this.props.files, + (id) => this.validateForm(id), + this.props.simulation, + (e) => this.handleChange(e)); } return ( @@ -147,7 +108,7 @@ class EditWidgetDialog extends Component { this.handleChange(e)} /> - { dialogControls } + { controls || '' } );