diff --git a/package.json b/package.json index 81c7ea6..8977188 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,10 @@ "react-notification-system": "^0.2.13", "react-rnd": "^4.2.2", "react-router": "^3.0.2", - "superagent": "^3.5.0" + "superagent": "^3.5.0", + "gaugeJS": "^1.3.2", + "d3-scale": "^1.0.5", + "rc-slider": "^7.0.1" }, "devDependencies": { "react-scripts": "0.9.3" diff --git a/src/components/dialog/edit-widget-color-control.js b/src/components/dialog/edit-widget-color-control.js new file mode 100644 index 0000000..1b50f44 --- /dev/null +++ b/src/components/dialog/edit-widget-color-control.js @@ -0,0 +1,70 @@ +/** + * File: edit-widget-color-control.js + * Author: Ricardo Hernandez-Montoya + * Date: 24.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { FormGroup, Col, Row, Radio, ControlLabel } from 'react-bootstrap'; +import classNames from 'classnames'; +import { scaleOrdinal, schemeCategory20 } from 'd3-scale'; + +class EditWidgetColorControl extends Component { + + static get ColorPalette() { + let colorCount = 0; + const colors = []; + const colorScale = scaleOrdinal(schemeCategory20); + while (colorCount < 20) { colors.push(colorScale(colorCount)); colorCount++; } + colors.unshift('#000', '#FFF'); // include black and white + + return colors; + } + + constructor(props) { + super(props); + + this.state = { + widget: {} + }; + } + + componentWillReceiveProps(nextProps) { + // Update state's widget with props + this.setState({ widget: nextProps.widget }); + } + + render() { + + return ( + + + + { this.props.label } + + + { + EditWidgetColorControl.ColorPalette.map( (color, idx ) => { + let colorStyle = { + background: color, + borderColor: color + }; + + let checkedClass = classNames({ + 'checked': idx === this.state.widget[this.props.controlId] + }); + + return ( this.props.handleChange({target: { id: this.props.controlId, value: idx}})} />) + } + ) + } + + + ) + } +} + +export default EditWidgetColorControl; \ No newline at end of file diff --git a/src/components/dialog/edit-widget-image.js b/src/components/dialog/edit-widget-image-control.js similarity index 95% rename from src/components/dialog/edit-widget-image.js rename to src/components/dialog/edit-widget-image-control.js index 20f61e7..199a331 100644 --- a/src/components/dialog/edit-widget-image.js +++ b/src/components/dialog/edit-widget-image-control.js @@ -1,5 +1,5 @@ /** - * File: edit-widget-value.js + * File: edit-widget-image-control.js * Author: Markus Grigull * Date: 04.03.2017 * @@ -24,7 +24,7 @@ import { FormGroup, FormControl, ControlLabel, Button } from 'react-bootstrap'; import AppDispatcher from '../../app-dispatcher'; -class EditImageWidget extends Component { +class EditImageWidgetControl extends Component { constructor(props) { super(props); @@ -80,4 +80,4 @@ class EditImageWidget extends Component { } } -export default EditImageWidget; +export default EditImageWidgetControl; diff --git a/src/components/dialog/edit-widget-orientation.js b/src/components/dialog/edit-widget-orientation.js new file mode 100644 index 0000000..76aa874 --- /dev/null +++ b/src/components/dialog/edit-widget-orientation.js @@ -0,0 +1,61 @@ +/** + * File: edit-widget-orientation.js + * Author: Ricardo Hernandez-Montoya + * Date: 10.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { FormGroup, Col, Row, Radio, ControlLabel } from 'react-bootstrap'; + +import WidgetSlider from '../widget-slider'; + +class EditWidgetOrientation extends Component { + constructor(props) { + super(props); + + this.state = { + widget: {} + }; + } + + componentWillReceiveProps(nextProps) { + // Update state's widget with props + this.setState({ widget: nextProps.widget }); + } + + handleOrientationChange(orientation) { + this.props.handleChange({ target: { id: 'orientation', value: orientation } }); + } + + render() { + + // The tag shouldn't be necessary, but it gives height to the row while combining horizontal and vertical forms + return ( + + + + Orientation + + + { + Object.keys(WidgetSlider.OrientationTypes).map( (type) => { + let value = WidgetSlider.OrientationTypes[type].value; + let name = WidgetSlider.OrientationTypes[type].name; + + return ( + this.handleOrientationChange(value)}> + { name } + ) + }) + } + + + + ); + } +} + +export default EditWidgetOrientation; \ No newline at end of file diff --git a/src/components/dialog/edit-widget-signal-control.js b/src/components/dialog/edit-widget-signal-control.js new file mode 100644 index 0000000..879052b --- /dev/null +++ b/src/components/dialog/edit-widget-signal-control.js @@ -0,0 +1,59 @@ +/** + * File: edit-widget-signal-control.js + * Author: Ricardo Hernandez-Montoya + * Date: 03.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { FormGroup, FormControl, ControlLabel } from 'react-bootstrap'; + +class EditWidgetSignalControl extends Component { + constructor(props) { + super(props); + + this.state = { + widget: { + simulator: '' + } + }; + } + + componentWillReceiveProps(nextProps) { + // Update state's widget with props + this.setState({ widget: nextProps.widget }); + } + + render() { + let signalsToRender = []; + + if (this.props.simulation) { + // get selected simulation model + const simulationModel = this.props.simulation.models.find( model => model.simulation === this.state.widget.simulation ); + + // If simulation model update the signals to render + signalsToRender = simulationModel? simulationModel.mapping : []; + } + + return ( + + Signal + this.props.handleChange(e)}> + { + signalsToRender.length === 0 ? ( + + ) : ( + signalsToRender.map((signal, index) => ( + + )) + ) + } + + + ); + } +} + +export default EditWidgetSignalControl; \ No newline at end of file diff --git a/src/components/dialog/edit-widget-signal-type-control.js b/src/components/dialog/edit-widget-signal-type-control.js new file mode 100644 index 0000000..8978186 --- /dev/null +++ b/src/components/dialog/edit-widget-signal-type-control.js @@ -0,0 +1,64 @@ +/** + * File: edit-widget-signal-type-control.js + * Author: Ricardo Hernandez-Montoya + * Date: 03.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { FormGroup, FormControl, ControlLabel } from 'react-bootstrap'; + +class EditWidgetSignalTypeControl extends Component { + constructor(props) { + super(props); + + this.state = { + widget: {} + }; + } + + componentWillReceiveProps(nextProps) { + // Update state's widget with props + this.setState({ widget: nextProps.widget }); + } + + render() { + // get selected simulation model + var simulationModel = {}; + + if (this.props.simulation) { + this.props.simulation.models.forEach((model) => { + if (model.simulation === this.state.widget.simulation) { + simulationModel = model; + } + }); + } + + // Obtain unique signal types with the help of dictionary keys + var signalTypes = Object.keys(simulationModel.mapping.reduce( (collection, signal) => { + var lower = signal.type.toLowerCase(); + collection[lower] = ''; + return collection; + }, {})); + + var capitalize = (str) => { return str.charAt(0).toUpperCase() + str.slice(1); } + + var selectedValue = signalTypes.includes(this.state.widget.signalType) ? this.state.widget.signalType : ''; + + return ( + + Signal type + this.props.handleChange(e)}> + + {signalTypes.map((type, index) => ( + + ))} + + + ); + } +} + +export default EditWidgetSignalTypeControl; \ No newline at end of file diff --git a/src/components/dialog/edit-widget-signals-control.js b/src/components/dialog/edit-widget-signals-control.js new file mode 100644 index 0000000..b84d930 --- /dev/null +++ b/src/components/dialog/edit-widget-signals-control.js @@ -0,0 +1,72 @@ +/** + * File: edit-widget-signals-control.js + * Author: Ricardo Hernandez-Montoya + * Date: 03.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { FormGroup, Checkbox, ControlLabel, FormControl } from 'react-bootstrap'; + +class EditWidgetSignalsControl extends Component { + constructor(props) { + super(props); + + this.state = { + widget: { + simulator: '' + } + }; + } + + componentWillReceiveProps(nextProps) { + // Update state's widget with props + this.setState({ widget: nextProps.widget }); + } + + handleSignalChange(checked, index) { + var signals = this.state.widget[this.props.controlId]; + var new_signals; + + if (checked) { + // add signal + new_signals = signals.concat(index); + } else { + // remove signal + new_signals = signals.filter( (idx) => idx !== index ); + } + + this.props.handleChange({ target: { id: this.props.controlId, value: new_signals } }); + } + + render() { + let signalsToRender = []; + + if (this.props.simulation) { + // get selected simulation model + const simulationModel = this.props.simulation.models.find( model => model.simulation === this.state.widget.simulation ); + + // If simulation model update the signals to render + signalsToRender = simulationModel? simulationModel.mapping : []; + } + + return ( + + Signals + { + signalsToRender.length === 0 || !this.state.widget.hasOwnProperty(this.props.controlId)? ( + No signals available. + ) : ( + signalsToRender.map((signal, index) => ( + this.handleSignalChange(e.target.checked, index)}>{signal.name} + )) + ) + } + + ); + } +} + +export default EditWidgetSignalsControl; \ No newline at end of file diff --git a/src/components/dialog/edit-widget-simulator-control.js b/src/components/dialog/edit-widget-simulator-control.js new file mode 100644 index 0000000..23de386 --- /dev/null +++ b/src/components/dialog/edit-widget-simulator-control.js @@ -0,0 +1,49 @@ +/** + * File: edit-widget-simulator-control.js + * Author: Ricardo Hernandez-Montoya + * Date: 03.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { FormGroup, FormControl, ControlLabel } from 'react-bootstrap'; + +class EditWidgetSimulatorControl extends Component { + constructor(props) { + super(props); + + this.state = { + widget: { + simulator: '' + } + }; + } + + componentWillReceiveProps(nextProps) { + // Update state's widget with props + this.setState({ widget: nextProps.widget }); + } + + render() { + + return ( + + Simulator + this.props.handleChange(e)}> + { + this.props.simulation.models.length === 0? ( + + ) : ( + this.props.simulation.models.map((model, index) => ( + + ))) + } + + + ); + } +} + +export default EditWidgetSimulatorControl; \ No newline at end of file diff --git a/src/components/dialog/edit-widget-text-control.js b/src/components/dialog/edit-widget-text-control.js new file mode 100644 index 0000000..d4196b4 --- /dev/null +++ b/src/components/dialog/edit-widget-text-control.js @@ -0,0 +1,39 @@ +/** + * File: edit-widget-text-control.js + * Author: Ricardo Hernandez-Montoya + * Date: 21.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { FormGroup, FormControl, ControlLabel } from 'react-bootstrap'; + +class EditWidgetTextControl extends Component { + constructor(props) { + super(props); + + this.state = { + widget: {} + }; + } + + componentWillReceiveProps(nextProps) { + // Update state's widget with props + this.setState({ widget: nextProps.widget }); + } + + render() { + + return ( + + { this.props.label } + this.props.handleChange(e)} /> + + + ); + } +} + +export default EditWidgetTextControl; \ No newline at end of file diff --git a/src/components/dialog/edit-widget-time-control.js b/src/components/dialog/edit-widget-time-control.js new file mode 100644 index 0000000..5b8e234 --- /dev/null +++ b/src/components/dialog/edit-widget-time-control.js @@ -0,0 +1,40 @@ +/** + * File: edit-widget-time-control.js + * Author: Ricardo Hernandez-Montoya + * Date: 13.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { FormGroup, FormControl, ControlLabel, HelpBlock } from 'react-bootstrap'; + +class EditWidgetTimeControl extends Component { + constructor(props) { + super(props); + + this.state = { + widget: { + time: 0 + } + }; + } + + componentWillReceiveProps(nextProps) { + this.setState({ widget: nextProps.widget }); + } + + render() { + + return ( + + Time + this.props.handleChange(e)} /> + Time in seconds + + ); + } +} + +export default EditWidgetTimeControl; diff --git a/src/components/dialog/edit-widget.js b/src/components/dialog/edit-widget.js index 561d387..a6a1523 100644 --- a/src/components/dialog/edit-widget.js +++ b/src/components/dialog/edit-widget.js @@ -24,10 +24,14 @@ import { FormGroup, FormControl, ControlLabel } from 'react-bootstrap'; import Dialog from './dialog'; -import EditValueWidget from './edit-widget-value'; -import EditPlotWidget from './edit-widget-plot'; -import EditTableWidget from './edit-widget-table'; -import EditImageWidget from './edit-widget-image'; +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'; class EditWidgetDialog extends Component { static propTypes = { @@ -61,8 +65,6 @@ class EditWidgetDialog extends Component { var update = this.state.temporal; update[e.target.id] = e.target.value; this.setState({ temporal: update }); - - //console.log(this.state.widget); } resetState() { @@ -85,18 +87,54 @@ class EditWidgetDialog extends Component { } render() { - // get widget part - var widgetDialog = null; + // Use a list to concatenate the controls according to the widget type + var dialogControls = []; if (this.props.widget) { if (this.props.widget.type === 'Value') { - widgetDialog = this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} />; + 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') { - widgetDialog = this.validateForm(id)} simulation={this.props.simulation} handleChange={(e, index) => this.handleChange(e, index)} />; + 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') { - widgetDialog = this.validateForm(id)} simulation={this.props.simulation} handleChange={(e, index) => this.handleChange(e, index)} />; + dialogControls.push( + this.validateForm(id)} simulation={this.props.simulation} handleChange={(e) => this.handleChange(e)} /> + ) } else if (this.props.widget.type === 'Image') { - widgetDialog = this.validateForm(id)} simulation={this.props.simulation} handleChange={(e, index) => this.handleChange(e, index)} />; + 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)} /> + ) } } @@ -108,8 +146,7 @@ class EditWidgetDialog extends Component { this.handleChange(e)} /> - - {widgetDialog} + { dialogControls } ); diff --git a/src/components/dropzone.js b/src/components/dropzone.js index 2c3172c..5f8bf0d 100644 --- a/src/components/dropzone.js +++ b/src/components/dropzone.js @@ -31,6 +31,19 @@ const dropzoneTarget = { position.x -= dropzoneRect.left; position.y -= dropzoneRect.top; + // Z-Index is one more the top most children + let foundZ = props.children.reduce( (maxZ, currentChildren) => { + // Is there a simpler way? Is not easy to expose a getter in a Container.create(Component) + let widget = currentChildren.props.data; + if (widget && widget.z) { + if (widget.z > maxZ) { + return widget.z; + } + } + return maxZ; + }, 0); + position.z = foundZ >= 100? foundZ : ++foundZ; + props.onDrop(monitor.getItem(), position); } }; diff --git a/src/components/widget-box.js b/src/components/widget-box.js new file mode 100644 index 0000000..b917b65 --- /dev/null +++ b/src/components/widget-box.js @@ -0,0 +1,33 @@ +/** + * File: widget-box.js + * Author: Ricardo Hernandez-Montoya + * Date: 25.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; + +import EditWidgetColorControl from './dialog/edit-widget-color-control'; + +class WidgetBox extends Component { + render() { + + let colors = EditWidgetColorControl.ColorPalette; + + let colorStyle = { + borderColor: colors[this.props.widget.border_color] + } + + return ( +
+
+ { } +
+
+ ); + } +} + +export default WidgetBox; diff --git a/src/components/widget-button.js b/src/components/widget-button.js new file mode 100644 index 0000000..98ced0d --- /dev/null +++ b/src/components/widget-button.js @@ -0,0 +1,44 @@ +/** + * File: widget-button.js + * Author: Ricardo Hernandez-Montoya + * Date: 29.03.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; + +import EditWidgetColorControl from './dialog/edit-widget-color-control'; + +class WidgetButton extends Component { + + action(e) { + e.target.blur(); // Remove focus + console.log('Button widget action'); + } + + render() { + + let colors = EditWidgetColorControl.ColorPalette; + + let colorStyle = { + background: colors[this.props.widget.background_color], + color: colors[this.props.widget.font_color], + borderColor: colors[this.props.widget.font_color] + } + + return ( +
+ { this.props.editing ? ( + + ) : ( + + ) + } +
+ ); + } +} + +export default WidgetButton; \ No newline at end of file diff --git a/src/components/widget-factory.js b/src/components/widget-factory.js new file mode 100644 index 0000000..364562a --- /dev/null +++ b/src/components/widget-factory.js @@ -0,0 +1,120 @@ +/** + * File: widget-factory.js + * Description: A factory to create and pre-configure widgets + * Author: Ricardo Hernandez-Montoya + * Date: 02.03.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import WidgetSlider from './widget-slider'; + +class WidgetFactory { + + static createWidgetOfType(type, position, defaultSimulator = null) { + + let widget = { + name: 'Name', + type: type, + width: 100, + height: 100, + x: position.x, + y: position.y, + z: position.z + }; + + // set type specific properties + switch(type) { + case 'Value': + widget.simulator = defaultSimulator; + widget.signal = 0; + widget.minWidth = 70; + widget.minHeight = 20; + widget.width = 120; + widget.height = 70; + break; + case 'Plot': + widget.simulator = defaultSimulator; + widget.signals = [ 0 ]; + widget.ylabel = ''; + widget.time = 60; + widget.minWidth = 400; + widget.minHeight = 200; + widget.width = 400; + widget.height = 200; + break; + case 'Table': + widget.simulator = defaultSimulator; + widget.minWidth = 300; + widget.minHeight = 200; + widget.width = 400; + widget.height = 200; + break; + case 'Label': + widget.minWidth = 70; + widget.minHeight = 20; + break; + case 'PlotTable': + widget.simulator = defaultSimulator; + widget.preselectedSignals = []; + widget.signals = []; // initialize selected signals + widget.ylabel = ''; + widget.minWidth = 400; + widget.minHeight = 300; + widget.width = 500; + widget.height = 500; + widget.time = 60; + break; + case 'Image': + widget.minWidth = 100; + widget.minHeight = 100; + widget.width = 200; + widget.height = 200; + break; + case 'Button': + widget.minWidth = 100; + widget.minHeight = 50; + widget.width = 100; + widget.height = 100; + widget.background_color = 1; + widget.font_color = 0; + break; + case 'NumberInput': + widget.minWidth = 200; + widget.minHeight = 50; + widget.width = 200; + widget.height = 50; + break; + case 'Slider': + widget.minWidth = 380; + widget.minHeight = 30; + widget.width = 400; + widget.height = 50; + widget.orientation = WidgetSlider.OrientationTypes.HORIZONTAL.value; // Assign default orientation + break; + case 'Gauge': + widget.simulator = defaultSimulator; + widget.signal = 0; + widget.minWidth = 200; + widget.minHeight = 150; + widget.width = 200; + widget.height = 150; + break; + case 'Box': + widget.minWidth = 50; + widget.minHeight = 50; + widget.width = 100; + widget.height = 100; + widget.border_color = 0; + widget.z = 0; + break; + default: + widget.width = 100; + widget.height = 100; + } + return widget; + } +} + +export default WidgetFactory; \ No newline at end of file diff --git a/src/components/widget-gauge.js b/src/components/widget-gauge.js new file mode 100644 index 0000000..b4365fe --- /dev/null +++ b/src/components/widget-gauge.js @@ -0,0 +1,119 @@ +/** + * File: widget-gauge.js + * Author: Ricardo Hernandez-Montoya + * Date: 31.03.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { Gauge } from 'gaugeJS'; + +class WidgetGauge extends Component { + constructor(props) { + super(props); + + this.gaugeCanvas = null; + this.gauge = null; + + this.state = { + value: 0 + }; + } + + staticLabels(widget_height) { + let label_font_size = Math.floor(widget_height * 0.055); // font scaling factor, integer for performance + return { + font: label_font_size + 'px "Helvetica Neue"', + labels: [0.0, 0.1, 0.5, 0.9, 1.0], + color: "#000000", + fractionDigits: 1 + } + } + + computeGaugeOptions(widget_height) { + return { + angle: -0.25, + lineWidth: 0.2, + pointer: { + length: 0.6, + strokeWidth: 0.035 + }, + radiusScale: 0.9, + colorStart: '#6EA2B0', + colorStop: '#6EA2B0', + strokeColor: '#E0E0E0', + highDpiSupport: true, + staticLabels: this.staticLabels(widget_height) + }; + } + + componentDidMount() { + const opts = this.computeGaugeOptions(this.props.widget.height); + this.gauge = new Gauge(this.gaugeCanvas).setOptions(opts); + this.gauge.maxValue = 1; + this.gauge.setMinValue(0); + this.gauge.animationSpeed = 30; + this.gauge.set(this.state.value); + } + + shouldComponentUpdate(nextProps, nextState) { + + // Check if size changed, resize labels if it did (the canvas itself is scaled with css) + if (this.props.widget.height !== nextProps.widget.height) { + this.updateAfterResize(nextProps.widget.height); + } + + // signal component update only if the value changed + return this.state.value !== nextState.value; + } + + componentWillReceiveProps(nextProps) { + // update value + const simulator = nextProps.widget.simulator; + + if (nextProps.data == null || nextProps.data[simulator] == null || nextProps.data[simulator].values == null) { + this.setState({ value: 0 }); + return; + } + + // check if value has changed + const signal = nextProps.data[simulator].values[nextProps.widget.signal]; + // Take just 3 decimal positions + // Note: Favor this method over Number.toFixed(n) in order to avoid a type conversion, since it returns a String + const new_value = Math.round( signal[signal.length - 1].y * 1e3 ) / 1e3; + if (this.state.value !== new_value) { + this.setState({ value: new_value }); + + // update gauge's value + this.gauge.set(new_value); + } + } + + updateAfterResize(newHeight) { + // Update labels after resize + this.gauge.setOptions({ staticLabels: this.staticLabels(newHeight) }); + } + + render() { + var componentClass = this.props.editing ? "gauge-widget editing" : "gauge-widget"; + var signalType = null; + + if (this.props.simulation) { + var simulationModel = this.props.simulation.models.filter((model) => model.simulator === this.props.widget.simulator)[0]; + signalType = simulationModel && simulationModel.length > 0? simulationModel.mapping[this.props.widget.signal].type : ''; + } + + return ( +
+
{ this.props.widget.name }
+ this.gaugeCanvas = node } /> +
{ signalType }
+
{ this.state.value }
+
+ ); + } +} + +export default WidgetGauge; diff --git a/src/components/widget-label.js b/src/components/widget-label.js index d9cd5cc..5c81a84 100644 --- a/src/components/widget-label.js +++ b/src/components/widget-label.js @@ -24,7 +24,9 @@ import React, { Component } from 'react'; class WidgetLabel extends Component { render() { return ( -

{this.props.widget.name}

+
+

{this.props.widget.name}

+
); } } diff --git a/src/components/widget-number-input.js b/src/components/widget-number-input.js new file mode 100644 index 0000000..13061bb --- /dev/null +++ b/src/components/widget-number-input.js @@ -0,0 +1,69 @@ +/** + * File: widget-number-input.js + * Author: Ricardo Hernandez-Montoya + * Date: 29.03.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { Form, FormGroup, Col, ControlLabel, FormControl } from 'react-bootstrap'; + +class WidgetNumberInput extends Component { + + static whichValidationStateIs( condition ) { + switch(condition) { + case 'ok': return null; + case 'error': return 'error'; + default: return 'error'; + } + } + + constructor(props) { + super(props); + + this.state = { + value: '', + validationState: WidgetNumberInput.whichValidationStateIs('ok') + }; + } + + validateInput(e) { + if (e.target.value === '' || e.target.value.endsWith('.')) { + this.setState({ + validationState: WidgetNumberInput.whichValidationStateIs('ok'), + value: e.target.value }); + } else { + var num = Number(e.target.value); + if (Number.isNaN(num)) { + this.setState({ validationState: WidgetNumberInput.whichValidationStateIs('error'), + value: e.target.value }); + } else { + this.setState({ + validationState: WidgetNumberInput.whichValidationStateIs('ok'), + value: num }); + } + } + } + + render() { + return ( +
+
+ + + {this.props.widget.name} + + + this.validateInput(e) } placeholder="Enter value" value={ this.state.value } /> + + + +
+
+ ); + } +} + +export default WidgetNumberInput; \ No newline at end of file diff --git a/src/components/widget-plot-table.js b/src/components/widget-plot-table.js index 71ea8bf..0507b3f 100644 --- a/src/components/widget-plot-table.js +++ b/src/components/widget-plot-table.js @@ -20,109 +20,138 @@ ******************************************************************************/ import React, { Component } from 'react'; -import { LineChart } from 'rd3'; +import classNames from 'classnames'; +import { FormGroup, Checkbox } from 'react-bootstrap'; -import { ButtonGroup, Button } from 'react-bootstrap'; +import Plot from './widget-plot/plot'; +import PlotLegend from './widget-plot/plot-legend'; class WidgetPlotTable extends Component { constructor(props) { super(props); this.state = { - size: { w: 0, h: 0 }, - signal: 0, - firstTimestamp: 0, - latestTimestamp: 0, - sequence: null, - rows: [], - values: [] + preselectedSignals: [], + signals: [] }; } componentWillReceiveProps(nextProps) { - // check data + + // Update internal selected signals state with props (different array objects) + if (this.props.widget.signals !== nextProps.widget.signals) { + this.setState( {signals: nextProps.widget.signals}); + } + + // Identify if there was a change in the preselected signals + if (nextProps.simulation && (JSON.stringify(nextProps.widget.preselectedSignals) !== JSON.stringify(this.props.widget.preselectedSignals) || this.state.preselectedSignals.length === 0)) { + + // Update the currently selected signals by intersecting with the preselected signals + // Do the same with the plot values + var intersection = this.computeIntersection(nextProps.widget.preselectedSignals, nextProps.widget.signals); + this.setState({ signals: intersection }); + + this.updatePreselectedSignalsState(nextProps); + return; + } + + } + + // Perform the intersection of the lists, alternatively could be done with Sets ensuring unique values + computeIntersection(preselectedSignals, selectedSignals) { + return preselectedSignals.filter( s => selectedSignals.includes(s)); + } + + updatePreselectedSignalsState(nextProps) { const simulator = nextProps.widget.simulator; - // plot size - this.setState({ size: { w: this.props.widget.width - 100, h: this.props.widget.height - 20 }}); - - if (nextProps.simulation == null || nextProps.data == null || nextProps.data[simulator] == null || nextProps.data[simulator].length === 0 || nextProps.data[simulator].values[0].length === 0) { - // clear values - this.setState({ values: [], sequence: null, rows: [] }); - return; - } - - // check if new data, otherwise skip - if (this.state.sequence >= nextProps.data[simulator].sequence) { - return; - } - // get simulation model const simulationModel = nextProps.simulation.models.find((model) => { return (model.simulator === simulator); }); - // get rows - var rows = []; - - simulationModel.mapping.forEach((signal) => { - rows.push({ name: signal.name }) - }); - - // get timestamps - var latestTimestamp = nextProps.data[simulator].values[0][nextProps.data[simulator].values[0].length - 1].x; - var firstTimestamp = latestTimestamp - nextProps.widget.time * 1000; - var firstIndex; - - if (nextProps.data[simulator].values[0][0].x < firstTimestamp) { - // find element index representing firstTimestamp - firstIndex = nextProps.data[simulator].values[0].findIndex((value) => { - return value.x >= firstTimestamp; - }); - } else { - firstIndex = 0; - firstTimestamp = nextProps.data[simulator].values[0][0].x; - latestTimestamp = firstTimestamp + nextProps.widget.time * 1000; + let preselectedSignals = []; + // Proceed if a simulation model is available + if (simulationModel) { + // Create checkboxes using the signal indices from simulation model + preselectedSignals = simulationModel.mapping.reduce( + // Loop through simulation model signals + (accum, model_signal, signal_index) => { + // Append them if they belong to the current selected type + if (nextProps.widget.preselectedSignals.indexOf(signal_index) > -1) { + accum.push( + { + index: signal_index, + name: model_signal.name + } + ) + } + return accum; + }, []); } + + this.setState({ preselectedSignals: preselectedSignals }); + } - // copy all values for each signal in time region - var values = [{ - values: nextProps.data[simulator].values[this.state.signal].slice(firstIndex, nextProps.data[simulator].values[this.state.signal].length - 1) - }]; - - this.setState({ values: values, firstTimestamp: firstTimestamp, latestTimestamp: latestTimestamp, sequence: nextProps.data[simulator].sequence, rows: rows }); + updateSignalSelection(signal_index, checked) { + // Update the selected signals and propagate to parent component + var new_widget = Object.assign({}, this.props.widget, { + signals: checked? this.state.signals.concat(signal_index) : this.state.signals.filter( (idx) => idx !== signal_index ) + }); + this.props.onWidgetChange(new_widget); } render() { - console.log("Signal: " + this.state.signal); + var checkBoxes = []; + + // Data passed to plot + let simulator = this.props.widget.simulator; + let simulatorData = this.props.data[simulator]; + + if (this.state.preselectedSignals && this.state.preselectedSignals.length > 0) { + // Create checkboxes using the signal indices from simulation model + checkBoxes = this.state.preselectedSignals.map( (signal) => { + var checked = this.state.signals.indexOf(signal.index) > -1; + var chkBxClasses = classNames({ + 'btn': true, + 'btn-default': true, + 'active': checked + }); + return this.updateSignalSelection(signal.index, e.target.checked) } > { signal.name } + }); + } + + // Prepare an array with the signals to show in the legend + var legendSignals = this.state.preselectedSignals.reduce( (accum, signal, i) => { + if (this.state.signals.includes(signal.index)) { + accum.push({ + index: signal.index, + name: signal.name + }) + } + return accum; + }, []); + return ( -
+

{this.props.widget.name}

-
- - { this.state.rows.map( (row, index) => ( - - )) +
+
+ { checkBoxes.length > 0 ? ( + + { checkBoxes } + + ) : ( No signal has been pre-selected. ) } - -
+
-
- {this.state.sequence && - { if (d != null) { return new Date(d.x); } }} - hoverAnimation={false} - circleRadius={0} - domain={{ x: [this.state.firstTimestamp, this.state.latestTimestamp] }} - /> - } +
+ +
+
); diff --git a/src/components/widget-plot.js b/src/components/widget-plot.js index aa455a6..ff0b1b1 100644 --- a/src/components/widget-plot.js +++ b/src/components/widget-plot.js @@ -20,81 +20,44 @@ ******************************************************************************/ import React, { Component } from 'react'; -import { LineChart } from 'rd3'; + +import Plot from './widget-plot/plot'; +import PlotLegend from './widget-plot/plot-legend'; class WidgetPlot extends Component { - constructor(props) { - super(props); - - this.state = { - values: [], - firstTimestamp: 0, - latestTimestamp: 0, - sequence: null - }; - } - - componentWillReceiveProps(nextProps) { - // check data - const simulator = nextProps.widget.simulator; - - if (nextProps.simulation == null || nextProps.data == null || nextProps.data[simulator] == null || nextProps.data[simulator].length === 0 || nextProps.data[simulator].values[0].length === 0) { - // clear values - this.setState({ values: [], sequence: null }); - return; - } - - // check if new data, otherwise skip - if (this.state.sequence >= nextProps.data[simulator].sequence) { - return; - } - - // get timestamps - var latestTimestamp = nextProps.data[simulator].values[0][nextProps.data[simulator].values[0].length - 1].x; - var firstTimestamp = latestTimestamp - nextProps.widget.time * 1000; - var firstIndex; - - if (nextProps.data[simulator].values[0][0].x < firstTimestamp) { - // find element index representing firstTimestamp - firstIndex = nextProps.data[simulator].values[0].findIndex((value) => { - return value.x >= firstTimestamp; - }); - } else { - firstIndex = 0; - firstTimestamp = nextProps.data[simulator].values[0][0].x; - latestTimestamp = firstTimestamp + nextProps.widget.time * 1000; - } - - // copy all values for each signal in time region - var values = []; - - nextProps.widget.signals.forEach((signal) => { - values.push({ - values: nextProps.data[simulator].values[signal].slice(firstIndex, nextProps.data[simulator].values[signal].length - 1) - }); - }); - - this.setState({ values: values, firstTimestamp: firstTimestamp, latestTimestamp: latestTimestamp, sequence: nextProps.data[simulator].sequence }); - } render() { - if (this.state.sequence == null) { - return (
Empty
); + + const simulator = this.props.widget.simulator; + const simulation = this.props.simulation; + let legendSignals = []; + let simulatorData = []; + + // Proceed if a simulation with models and a simulator are available + if (simulator && simulation && simulation.models.length > 0) { + + const model = simulation.models.find( (model) => model.simulator === simulator ); + const chosenSignals = this.props.widget.signals; + + simulatorData = this.props.data[simulator]; + + // Query the signals that will be displayed in the legend + legendSignals = model.mapping.reduce( (accum, model_signal, signal_index) => { + if (chosenSignals.includes(signal_index)) { + accum.push({ index: signal_index, name: model_signal.name }); + } + return accum; + }, []); } return ( -
- { if (d != null) { return new Date(d.x); } }} - hoverAnimation={false} - circleRadius={0} - domain={{ x: [this.state.firstTimestamp, this.state.latestTimestamp] }} - /> +
+

{this.props.widget.name}

+ +
+ +
+
); } diff --git a/src/components/widget-plot/plot-legend.js b/src/components/widget-plot/plot-legend.js new file mode 100644 index 0000000..3771a5c --- /dev/null +++ b/src/components/widget-plot/plot-legend.js @@ -0,0 +1,31 @@ +/** + * File: plot-legend.js + * Author: Ricardo Hernandez-Montoya + * Date: 10.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { scaleOrdinal, schemeCategory10 } from 'd3-scale'; + +class PlotLegend extends Component { + // constructor(props) { + // super(props); + // } + + render() { + var colorScale = scaleOrdinal(schemeCategory10); + + return ( +
+ { this.props.signals.map( (signal) => +
   {signal.name}
) + } +
+ ); + } +} + +export default PlotLegend; \ No newline at end of file diff --git a/src/components/widget-plot/plot.js b/src/components/widget-plot/plot.js new file mode 100644 index 0000000..6bae9d0 --- /dev/null +++ b/src/components/widget-plot/plot.js @@ -0,0 +1,141 @@ +/** + * File: plot.js + * Author: Ricardo Hernandez-Montoya + * Date: 10.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import { LineChart } from 'rd3'; +import { scaleOrdinal, schemeCategory10 } from 'd3-scale'; + +class Plot extends Component { + constructor(props) { + super(props); + + this.chartWrapper = null; + + // Initialize plot size and data + this.state = Object.assign( + { size: { w: 0, h: 0 } }, + this.getPlotInitData(true) + ); + } + + // Get an object with 'invisible' init data for the last minute. + // Include start/end timestamps if required. + getPlotInitData(withRangeTimestamps = false) { + + const initSecondTime = Date.now(); + const initFirstTime = initSecondTime - 1000 * 60; // Decrease 1 min + const values = [{ values: [{x: initFirstTime, y: 0}], strokeWidth: 0 }]; + + let output = withRangeTimestamps? + { sequence: 0, values: values, firstTimestamp: initFirstTime, latestTimestamp: initSecondTime, } : + { sequence: 0, values: values }; + + return output; + } + + componentWillReceiveProps(nextProps) { + let nextData = nextProps.simulatorData; + + // handle plot size + const w = this.chartWrapper.offsetWidth - 20; + const h = this.chartWrapper.offsetHeight - 20; + const currentSize = this.state.size; + if (w !== currentSize.w || h !== currentSize.h) { + this.setState({size: { w, h } }); + } + + // If signals were cleared, clear the plot (triggers a new state) + if (this.signalsWereJustCleared(nextProps)) { this.clearPlot(); return; } + + // If no signals have been selected, just leave + if (nextProps.signals == null || nextProps.signals.length === 0) { return; } + + // Identify simulation reset + if (nextData == null || nextData.length === 0 || nextData.values[0].length === 0) { this.clearPlot(); return; } + + // check if new data, otherwise skip + if (this.state.sequence >= nextData.sequence) { return; } + + this.updatePlotData(nextProps); + + } + + signalsWereJustCleared(nextProps) { + + return this.props.signals && + nextProps.signals && + this.props.signals.length > 0 && + nextProps.signals.length === 0; + } + + clearPlot() { + this.setState( this.getPlotInitData(false) ); + } + + updatePlotData(nextProps) { + let nextData = nextProps.simulatorData; + + // get timestamps + var latestTimestamp = nextData.values[0][nextData.values[0].length - 1].x; + var firstTimestamp = latestTimestamp - nextProps.time * 1000; + var firstIndex; + + if (nextData.values[0][0].x < firstTimestamp) { + // find element index representing firstTimestamp + firstIndex = nextData.values[0].findIndex((value) => { + return value.x >= firstTimestamp; + }); + } else { + firstIndex = 0; + firstTimestamp = nextData.values[0][0].x; + latestTimestamp = firstTimestamp + nextProps.time * 1000; + } + + // copy all values for each signal in time region + var values = []; + nextProps.signals.forEach((signal_index, i, arr) => ( + // Include signal index, useful to relate them to the signal selection + values.push( + { + index: signal_index, + values: nextData.values[signal_index].slice(firstIndex, nextData.values[signal_index].length - 1)}) + )); + + this.setState({ values: values, firstTimestamp: firstTimestamp, latestTimestamp: latestTimestamp, sequence: nextData.sequence }); + } + + render() { + // Make tick count proportional to the plot width using a rough scale ratio + var tickCount = Math.round(this.state.size.w / 80); + + return ( +
this.chartWrapper = domNode }> + {this.state.sequence != null && + { if (d != null) { return new Date(d.x); } }} + xAxisTickCount={ tickCount } + yAxisLabel={ this.props.yAxisLabel } + hoverAnimation={false} + circleRadius={0} + domain={{ x: [this.state.firstTimestamp, this.state.latestTimestamp] }} + /> + } +
+ ); + } + +} + +export default Plot; \ No newline at end of file diff --git a/src/components/widget-slider.js b/src/components/widget-slider.js new file mode 100644 index 0000000..2ea720c --- /dev/null +++ b/src/components/widget-slider.js @@ -0,0 +1,100 @@ +/** + * File: widget-slider.js + * Author: Ricardo Hernandez-Montoya + * Date: 30.03.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +import React, { Component } from 'react'; +import classNames from 'classnames'; +import Slider from 'rc-slider'; +import 'rc-slider/assets/index.css'; + +class WidgetSlider extends Component { + + static get OrientationTypes() { + return ({ + HORIZONTAL: {value: 0, name: 'Horizontal'}, + VERTICAL: {value: 1, name: 'Vertical'} + }) + } + + constructor(props) { + super(props); + + this.state = { + value: 50 + }; + } + + componentWillReceiveProps(nextProps) { + // Update value + if (nextProps.widget.value && this.state.value !== nextProps.widget.value) { + this.setState({ value: nextProps.widget.value }) + } + // Check if the orientation changed, update the size if it did + if (this.props.widget.orientation !== nextProps.widget.orientation) { + let baseWidget = nextProps.widget; + // Exchange dimensions and constraints + let newWidget = Object.assign({}, baseWidget, { + width: baseWidget.height, + height: baseWidget.width, + minWidth: baseWidget.minHeight, + minHeight: baseWidget.minWidth, + maxWidth: baseWidget.maxHeight, + maxHeight: baseWidget.maxWidth + }); + nextProps.onWidgetChange(newWidget); + } + } + + valueIsChanging(newValue) { + this.setState({ value: newValue }); + } + + valueChanged(newValue) { + // Enable to propagate action + // let newWidget = Object.assign({}, this.props.widget, { + // value: newValue + // }); + // this.props.onWidgetChange(newWidget); + } + + render() { + + let isVertical = this.props.widget.orientation === WidgetSlider.OrientationTypes.VERTICAL.value; + + let fields = { + 'name': this.props.widget.name, + 'control': this.valueIsChanging(v) } onAfterChange={ (v) => this.valueChanged(v) }/>, + 'value': this.state.value + } + + var widgetClasses = classNames({ + 'slider-widget': true, + 'full': true, + 'vertical': isVertical, + 'horizontal': !isVertical + }); + + return ( + this.props.widget.orientation === WidgetSlider.OrientationTypes.HORIZONTAL.value? ( +
+ +
{ fields.control }
+ { fields.value } +
+ ) : ( +
+ + { fields.control } + { fields.value } +
+ ) + ); + } +} + +export default WidgetSlider; \ No newline at end of file diff --git a/src/components/widget-table.js b/src/components/widget-table.js index 9de95d7..9c48a00 100644 --- a/src/components/widget-table.js +++ b/src/components/widget-table.js @@ -60,7 +60,7 @@ class WidgetTable extends Component { nextProps.data[simulator].values.forEach((signal, index) => { rows.push({ name: simulationModel.mapping[index].name, - value: signal[signal.length - 1].y + value: signal[signal.length - 1].y.toFixed(3) }) }); @@ -69,7 +69,7 @@ class WidgetTable extends Component { render() { return ( -
+

{this.props.widget.name}

diff --git a/src/components/widget-value.js b/src/components/widget-value.js index f4b9ac4..eeceeed 100644 --- a/src/components/widget-value.js +++ b/src/components/widget-value.js @@ -47,9 +47,10 @@ class WidgetValue extends Component { } render() { + var value_to_render = Number(this.state.value); return ( -
- {this.props.widget.name}: {this.state.value} +
+ {this.props.widget.name} { Number.isNaN(value_to_render)? NaN : value_to_render.toFixed(3) }
); } diff --git a/src/containers/project.js b/src/containers/project.js index 988dbdf..33099a4 100644 --- a/src/containers/project.js +++ b/src/containers/project.js @@ -156,7 +156,7 @@ class Visualizations extends Component { } return ( -
+

{this.state.project.name}

diff --git a/src/containers/projects.js b/src/containers/projects.js index e04d34f..6dd97b8 100644 --- a/src/containers/projects.js +++ b/src/containers/projects.js @@ -102,7 +102,7 @@ class Projects extends Component { render() { return ( -
+

Projects

diff --git a/src/containers/simulation.js b/src/containers/simulation.js index 20f17bd..59e37c5 100644 --- a/src/containers/simulation.js +++ b/src/containers/simulation.js @@ -131,7 +131,7 @@ class Simulation extends Component { render() { return ( -
+

{this.state.simulation.name}

diff --git a/src/containers/simulations.js b/src/containers/simulations.js index 62ef433..5c445d0 100644 --- a/src/containers/simulations.js +++ b/src/containers/simulations.js @@ -112,7 +112,7 @@ class Simulations extends Component { render() { return ( -
+

Simulations

diff --git a/src/containers/simulators.js b/src/containers/simulators.js index 489aac4..e9a2af8 100644 --- a/src/containers/simulators.js +++ b/src/containers/simulators.js @@ -96,7 +96,7 @@ class Simulators extends Component { render() { return ( -
+

Simulators

diff --git a/src/containers/visualization.js b/src/containers/visualization.js index 3f9ea9c..00fc336 100644 --- a/src/containers/visualization.js +++ b/src/containers/visualization.js @@ -24,6 +24,7 @@ import { Container } from 'flux/utils'; import { Button } from 'react-bootstrap'; import { ContextMenu, MenuItem } from 'react-contextmenu'; +import WidgetFactory from '../components/widget-factory'; import ToolboxItem from '../components/toolbox-item'; import Dropzone from '../components/dropzone'; import Widget from './widget'; @@ -34,6 +35,8 @@ import ProjectStore from '../stores/project-store'; import SimulationStore from '../stores/simulation-store'; import FileStore from '../stores/file-store'; import AppDispatcher from '../app-dispatcher'; +import NotificationsDataManager from '../data-managers/notifications-data-manager'; +import NotificationsFactory from '../data-managers/notifications-factory'; class Visualization extends Component { static getStores() { @@ -60,11 +63,13 @@ class Visualization extends Component { editModal: prevState.editModal || false, modalData: prevState.modalData || null, modalIndex: prevState.modalIndex || null, - + + maxWidgetHeight: prevState.maxWidgetHeight || 0, + dropZoneHeight: prevState.dropZoneHeight || 0, last_widget_key: prevState.last_widget_key || 0 }; } - + componentWillMount() { AppDispatcher.dispatch({ type: 'visualizations/start-load' @@ -126,6 +131,8 @@ class Visualization extends Component { widgets: tempVisualization.widgets? this.transformToWidgetsDict(tempVisualization.widgets) : {} }); + this.computeHeightWithWidgets(visualization.widgets); + this.setState({ visualization: visualization, project: null }); AppDispatcher.dispatch({ @@ -137,54 +144,19 @@ class Visualization extends Component { } handleDrop(item, position) { - // add new widget - var widget = { - name: 'Name', - type: item.name, - width: 100, - height: 100, - x: position.x, - y: position.y, - z: 0 - }; - // set type specific properties - if (item.name === 'Value') { - widget.simulator = this.state.simulation.models[0].simulator; - widget.signal = 0; - widget.minWidth = 70; - widget.minHeight = 20; - } else if (item.name === 'Plot') { - widget.simulator = this.state.simulation.models[0].simulator; - widget.signals = [ 0 ]; - widget.time = 60; - widget.minWidth = 400; - widget.minHeight = 200; - widget.width = 400; - widget.height = 200; - } else if (item.name === 'Table') { - widget.simulator = this.state.simulation.models[0].simulator; - widget.minWidth = 300; - widget.minHeight = 200; - widget.width = 400; - widget.height = 200; - } else if (item.name === 'Label') { - widget.minWidth = 70; - widget.minHeight = 20; - } else if (item.name === 'PlotTable') { - widget.simulator = this.state.simulation.models[0].simulator; - widget.minWidth = 400; - widget.minHeight = 200; - widget.width = 500; - widget.height = 400; - widget.time = 60 - } else if (item.name === 'Image') { - widget.minWidth = 100; - widget.minHeight = 100; - widget.width = 200; - widget.height = 200; + let widget = null; + let defaultSimulator = null; + + if (this.state.simulation.models && this.state.simulation.models.length === 0) { + NotificationsDataManager.addNotification(NotificationsFactory.NO_SIM_MODEL_AVAILABLE); + } else { + defaultSimulator = this.state.simulation.models[0].simulator; } + // create new widget + widget = WidgetFactory.createWidgetOfType(item.name, position, defaultSimulator); + var new_widgets = this.state.visualization.widgets; var widget_key = this.getNewWidgetKey(); @@ -193,11 +165,17 @@ class Visualization extends Component { var visualization = Object.assign({}, this.state.visualization, { widgets: new_widgets }); + + this.increaseHeightWithWidget(widget); this.setState({ visualization: visualization }); } - widgetChange(updated_widget, key) { + widgetStatusChange(updated_widget, key) { + // Widget changed internally, make changes effective then save them + this.widgetChange(updated_widget, key, this.saveChanges); + } + widgetChange(updated_widget, key, callback = null) { var widgets_update = {}; widgets_update[key] = updated_widget; var new_widgets = Object.assign({}, this.state.visualization.widgets, widgets_update); @@ -205,7 +183,46 @@ class Visualization extends Component { var visualization = Object.assign({}, this.state.visualization, { widgets: new_widgets }); - this.setState({ visualization: visualization }); + + // Check if the height needs to be increased, the section may have shrunk if not + if (!this.increaseHeightWithWidget(updated_widget)) { + this.computeHeightWithWidgets(visualization.widgets); + } + this.setState({ visualization: visualization }, callback); + } + + /* + * Set the initial height state based on the existing widgets + */ + computeHeightWithWidgets(widgets) { + // Compute max height from widgets + let maxHeight = Object.keys(widgets).reduce( (maxHeightSoFar, widgetKey) => { + let thisWidget = widgets[widgetKey]; + let thisWidgetHeight = thisWidget.y + thisWidget.height; + + return thisWidgetHeight > maxHeightSoFar? thisWidgetHeight : maxHeightSoFar; + }, 0); + + this.setState({ + maxWidgetHeight: maxHeight, + dropZoneHeight: maxHeight + 40 + }); + } + /* + * Adapt the area's height with the position of the new widget. + * Return true if the height increased, otherwise false. + */ + increaseHeightWithWidget(widget) { + let increased = false; + let thisWidgetHeight = widget.y + widget.height; + if (thisWidgetHeight > this.state.maxWidgetHeight) { + increased = true; + this.setState({ + maxWidgetHeight: thisWidgetHeight, + dropZoneHeight: thisWidgetHeight + 40 + }); + } + return increased; } editWidget(e, data) { @@ -238,6 +255,11 @@ class Visualization extends Component { this.setState({ visualization: visualization }); } + stopEditing() { + // Provide the callback so it can be called when state change is applied + this.setState({ editing: false }, this.saveChanges ); + } + saveChanges() { // Transform to a list var visualization = Object.assign({}, this.state.visualization, { @@ -248,8 +270,6 @@ class Visualization extends Component { type: 'visualizations/start-edit', data: visualization }); - - this.setState({ editing: false }); } discardChanges() { @@ -299,25 +319,10 @@ class Visualization extends Component { } render() { - // calculate widget area height - var height = 0; - var current_widgets = this.state.visualization.widgets; - if (current_widgets) { - Object.keys(current_widgets).forEach( (widget_key) => { - var widget = current_widgets[widget_key]; - if (widget.y + widget.height > height) { - height = widget.y + widget.height; - } - }); - - // add padding - height += 40; - } - return ( -
+
@@ -326,7 +331,7 @@ class Visualization extends Component {
{this.state.editing ? (
-
} - this.handleDrop(item, position)} editing={this.state.editing}> + this.handleDrop(item, position)} editing={this.state.editing}> {current_widgets != null && Object.keys(current_widgets).map( (widget_key) => ( - this.widgetChange(w, k)} editing={this.state.editing} index={widget_key} grid={this.state.grid} /> + this.widgetChange(w, k)} onWidgetStatusChange={(w, k) => this.widgetStatusChange(w, k)} editing={this.state.editing} index={widget_key} grid={this.state.grid} /> ))} diff --git a/src/containers/widget.js b/src/containers/widget.js index e14a47b..1f61e9b 100644 --- a/src/containers/widget.js +++ b/src/containers/widget.js @@ -23,6 +23,7 @@ import React, { Component } from 'react'; import { Container } from 'flux/utils'; import { ContextMenuTrigger } from 'react-contextmenu'; import Rnd from 'react-rnd'; +import classNames from 'classnames'; import AppDispatcher from '../app-dispatcher'; import SimulatorDataStore from '../stores/simulator-data-store'; @@ -34,6 +35,11 @@ import WidgetTable from '../components/widget-table'; import WidgetLabel from '../components/widget-label'; import WidgetPlotTable from '../components/widget-plot-table'; import WidgetImage from '../components/widget-image'; +import WidgetButton from '../components/widget-button'; +import WidgetNumberInput from '../components/widget-number-input'; +import WidgetSlider from '../components/widget-slider'; +import WidgetGauge from '../components/widget-gauge'; +import WidgetBox from '../components/widget-box'; import '../styles/widgets.css'; @@ -84,7 +90,7 @@ class Widget extends Component { resizeStop(direction, styleSize, clientSize, delta) { // update widget - var widget = this.props.data; + let widget = Object.assign({}, this.props.data); // resize depends on direction if (direction === 'left' || direction === 'topLeft' || direction === 'bottomLeft') { @@ -125,6 +131,7 @@ class Widget extends Component { // get widget element const widget = this.props.data; + var borderedWidget = false; var element = null; // dummy is passed to widgets to keep updating them while in edit mode @@ -132,15 +139,35 @@ class Widget extends Component { element = } else if (widget.type === 'Plot') { element = + borderedWidget = true; } else if (widget.type === 'Table') { element = } else if (widget.type === 'Label') { element = + borderedWidget = true; } else if (widget.type === 'PlotTable') { - element = + element = this.props.onWidgetStatusChange(w, this.props.index) } /> + borderedWidget = true; } else if (widget.type === 'Image') { element = + borderedWidget = true; + } else if (widget.type === 'Button') { + element = + } else if (widget.type === 'NumberInput') { + element = + } else if (widget.type === 'Slider') { + element = this.props.onWidgetStatusChange(w, this.props.index) } /> + } else if (widget.type === 'Gauge') { + element = + } else if (widget.type === 'Box') { + element = } + + let widgetClasses = classNames({ + 'widget': !this.props.editing, + 'editing-widget': this.props.editing, + 'border': borderedWidget + }); if (this.props.editing) { return ( @@ -149,9 +176,10 @@ class Widget extends Component { initial={{ x: Number(widget.x), y: Number(widget.y), width: widget.width, height: widget.height }} minWidth={ widget.minWidth } minHeight={ widget.minHeight } + lockAspectRatio={ widget.lockedAspectRatio } bounds={'parent'} - className="editing-widget" - onResizeStart={ (direction, styleSize, clientSize, event) => this.borderWasClicked(event) } + className={ widgetClasses } + onResizeStart={ (direction, styleSize, clientSize, event) => this.borderWasClicked(event) } onResizeStop={(direction, styleSize, clientSize, delta) => this.resizeStop(direction, styleSize, clientSize, delta)} onDragStop={(event, ui) => this.dragStop(event, ui)} moveGrid={grid} @@ -165,7 +193,7 @@ class Widget extends Component { ); } else { return ( -
+
{element}
); diff --git a/src/data-managers/notifications-factory.js b/src/data-managers/notifications-factory.js new file mode 100644 index 0000000..56c095c --- /dev/null +++ b/src/data-managers/notifications-factory.js @@ -0,0 +1,24 @@ +/** + * File: notifications-factory.js + * Description: An unique source of pre-defined notifications that are displayed + * throughout the application. + * Author: Ricardo Hernandez-Montoya + * Date: 13.04.2017 + * Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + **********************************************************************************/ + +class NotificationsFactory { + + static get NO_SIM_MODEL_AVAILABLE() { + return { + title: 'No simulation model available', + message: 'Consider defining a simulation model in the simulators section.', + level: 'warning' + }; + } + +} + +export default NotificationsFactory; \ No newline at end of file diff --git a/src/data-managers/simulators-data-manager.js b/src/data-managers/simulators-data-manager.js index f9dfaa6..535f1e2 100644 --- a/src/data-managers/simulators-data-manager.js +++ b/src/data-managers/simulators-data-manager.js @@ -39,7 +39,7 @@ function isRunning(simulator) { // check if simulator is running simulator.running = false; - if (response.id == body.id) { + if (response.id === body.id) { response.response.forEach(sim => { if (sim.name === name) { simulator.running = true; diff --git a/src/styles/app.css b/src/styles/app.css index d02e36f..33b3280 100644 --- a/src/styles/app.css +++ b/src/styles/app.css @@ -57,8 +57,8 @@ body { .app-footer { width: 100%; height: 60px; - position: absolute; - bottom: 0px; + /* Float below body */ + float: right; padding-top: 20px; text-align: center; @@ -69,24 +69,21 @@ body { .app-body { /* Let sidebar grow and content occupy rest of the space */ display: flex; - position: absolute; - top: 60px; - bottom: 60px; - right: 0px; - left: 0px; + float: right; + width: 100%; + /* Fit between header and footer */ + min-height: calc(100vh - 140px); padding: 15px 5px 0px 5px; } -.app-body div { +.app-body > div { margin-left: 7px; margin-right: 7px; } .app-content { flex: 1 1 auto; - min-height: 400px; - height: 100%; padding: 15px 20px; background-color: #fff; @@ -117,7 +114,6 @@ body { .active { font-weight: bold; - /*text-decoration:none;*/ } .menu-sidebar ul { @@ -242,7 +238,8 @@ body { } .section { - height: 100%; + min-height: 100%; + width: 100%; } .section-header div { diff --git a/src/styles/widgets.css b/src/styles/widgets.css index dcf9957..7e72ad0 100644 --- a/src/styles/widgets.css +++ b/src/styles/widgets.css @@ -20,14 +20,27 @@ ******************************************************************************/ .widget { - border: 1px solid lightgray; - padding: 3px 6px; - background-color: #fff; + background-color: #fff; + overflow: auto; +} + +.border { + border: 1px solid lightgray; } .editing-widget { - border: 1px solid lightgray; - background-color: #fff; + background-color: #fff; +} + +/* Firefox-specific rules */ +@-moz-document url-prefix() { + .editing-widget:not(.border):hover { + outline-offset: -10px; + } +} + +.editing-widget:not(.border):hover { + outline: 1px solid lightgray; } /* Area to trigger the context menu */ @@ -37,7 +50,7 @@ position: absolute; top: 0px; left: 0px; - padding: 3px 6px; + overflow: auto; } .react-contextmenu { @@ -108,21 +121,15 @@ right: 7px; } -.plot-table-widget .content { - display: -webkit-flex; - display: flex; -} - -.plot-table-widget .widget-table { - min-width: 100px; - display: flex; - flex-direction: column; - justify-content: center; -} - /* Reset Bootstrap styles to "disable" while editing */ -.plot-table-widget .widget-table .btn[disabled] { +div[class*="-widget"] .btn[disabled], .btn.disabled, div[class*="-widget"] input[disabled], .form-control[disabled], .checkbox.disabled label { cursor: inherit; + pointer-events: none; +} + +.editing-widget .btn-default.active { + background-color: #abcfd8; + border-color: #ccc; } .btn-default[disabled]:hover { @@ -136,6 +143,332 @@ } /* End reset */ + /* Match Bootstrap's them to VILLAS */ +.widget .btn-default.active { + background-color: #abcfd8; +} + +.widget .btn-default.active:hover, .widget .btn-default.active:hover { + background-color: #89b3bd; +} + +.widget .btn-default:hover { + background-color: #abcfd8; +} +/* End match */ + +/* Begin edit menu: Colors */ +.color-control input[type="radio"] { + display: none; +} + +.color-control .radio-inline.checked { + border-color: #000 !important; +} + +.color-control .radio-inline { + height: 24px; + flex: 1 1 auto; + border: 2px solid; + /* Reset bootstrap padding */ + padding-left: 0px; +} + +.color-control .radio-inline + .radio-inline { + /* Reset bootstrap margin */ + margin-left: 0px; +} + +.color-control .radio-inline:hover { + border-color: #444 !important; +} + +.color-control div[class*="colors-column-"] { + display: flex; + padding: 2px 20px; +} + +/* End edit menu: Colors */ + +/* PlotTable widget */ +.plot-table-widget, .plot-widget, .value-widget, .image-widget, .label-widget { + width: 100%; + height: 100%; + padding: 3px 6px; +} + +.plot-table-widget { + display: -webkit-flex; + display: flex; + flex-direction: column; +} + +.plot-table-widget .content { + -webkit-flex: 1 0 auto; + flex: 1 0 auto; + display: -webkit-flex; + display: flex; + flex-direction: column; +} + +.table-plot-row { + -webkit-flex: 1 0 auto; + flex: 1 0 auto; + display: -webkit-flex; + display: flex; +} + +.plot-table-widget .widget-table { + -webkit-flex: 1 0 auto; + flex: 1 0 auto; + flex-basis: 90px; + max-width: 50%; + display: flex; + flex-direction: column; + justify-content: center; +} + +.plot-table-widget small { + text-align: center; +} + +.plot-table-widget .checkbox label { + height: 100%; + width: 100%; + padding: 6px 12px; + overflow-x: hidden; +} + +.plot-table-widget .btn { + padding: 0px; +} + +.plot-table-widget input[type="checkbox"] { + display: none; +} +/* End PlotTable Widget */ + +/* Plot Widget */ +.plot-widget { + display: -webkit-flex; + display: flex; + flex-direction: column; +} +/* End Plot Widget */ + +/* Plots */ +/* The plot container, in order to avoid 100% height/width */ +.widget-plot { + display: flex; + -webkit-flex: 1 1 auto; + flex: 1 1 auto; +} + +.chart-wrapper { + width: 100%; + padding-left: 10px; +} + +.plot-legend { + display: -webkit-flex; + display: flex; + flex-basis: 20px; /* Enough to allocate one row */ + max-height: 40px; /* Allocate up to two rows */ + flex-shrink: 0; + flex-wrap: wrap; + justify-content: space-around; + margin-bottom: 5px; +} + +.signal-legend { + font-size: 0.8em; + font-weight: 700; + padding-left: 5px; + padding-right: 5px; + overflow: hidden; +} + +.legend-color { + height: 50%; + display: inline-block; + vertical-align: middle; +} + +/* End Plots */ + +.single-value-widget { + width: 100%; + height: 100%; + display: flex; + justify-content: center; + align-items: center; +} + +.single-value-widget > * { + width: 50%; + float: left; + margin: 5px; + font-size: 1vw; +} + +/* Button widget styling */ +.button-widget button { + border-radius: 25px; + border-style: double; + border-width: 4px; + overflow-x: hidden; + font-weight: 500; + font-size: 1.2em; +} + +.button-widget button:hover { + border-style: double; +} +/* End button widget styling */ + +.full { + width: 100%; + height: 100%; +} + +/* Number input widget */ +div[class*="-widget"] label { + cursor: inherit; +} + +.number-input-widget { + display: flex; + flex-direction: column; + justify-content: center; +} + +.number-input-widget .form-horizontal .form-group { + margin: 0px; +} +/* End number input widget */ + +/* Begin Slider widget */ +.slider-widget { + display: flex; + align-items: center; + justify-content: space-around; +} + +.slider-widget label { + flex: 0 0 auto; + text-align: center; +} + +.slider-widget span { + text-align: center; + font-size: 1.5em; + font-weight: 600; +} + +.slider-widget.horizontal .slider { + flex: 1 1 auto; +} + +.slider-widget.horizontal span { + flex: 0 0 50pt; +} + +.slider-widget.horizontal label { + padding-right: 10px; +} + +.slider-widget.vertical { + flex-direction: column; +} + +.slider-widget.vertical span { + padding-top: 10px; +} + +.slider-widget.vertical label { + padding-bottom: 10px; +} + +/* Begin Slider customization */ +.rc-slider-track { + background-color: #6EA2B0; +} + +.rc-slider-handle, .rc-slider-handle:hover { + border-color: #6EA2B0; +} + +.rc-slider-disabled { + background-color: inherit; +} + +.rc-slider-disabled .rc-slider-handle { + cursor: inherit; +} + +.rc-slider-disabled .rc-slider-handle:hover { + border-color:#ccc; +} +/* End Slider customization */ + +/* End slider widget */ + +/* Gauge widget */ +.gauge-widget { + width: 100%; + height: 100%; +} + +.gauge-widget canvas { + width: 100%; + height: 87%; +} + +.gauge-name { + height: 10%; + width: 100%; + text-align: center; + font-weight: bold; +} + +.gauge-unit { + position: absolute; + width: 100%; + font-size: 1.0em; + bottom: 25%; + text-align: center; +} + +.gauge-value { + position: absolute; + width: 100%; + font-weight: bold; + font-size: 1.5em; + bottom: 10%; + text-align: center; +} +/* End gauge widget */ + +/* Begin label widget */ +.label-widget { + text-align: center; +} +/* End label widget */ + +/* Begin table widget */ +.table-widget td, .table-widget th { + text-align: center; +} +/* End table widget*/ + +/* Begin box widget */ +.box-widget .border { + width: 100%; + height: 100%; + border: 2px solid; +} +/* End box widget */ + .plot-table-widget .widget-plot { -webkit-flex: 1 0 auto; flex: 1 0 auto;