From 32eea44827008ee7b8c762d8848d81780395fce3 Mon Sep 17 00:00:00 2001 From: Markus Grigull Date: Thu, 12 Apr 2018 14:48:54 +0200 Subject: [PATCH] Add simulator-action component --- src/components/simulator-action.js | 68 ++++++++++++++++++++++++++ src/containers/simulation.js | 76 +++++------------------------- src/containers/simulations.js | 76 +++++------------------------- src/containers/simulators.js | 49 ++++--------------- src/stores/simulator-store.js | 4 +- 5 files changed, 104 insertions(+), 169 deletions(-) create mode 100644 src/components/simulator-action.js diff --git a/src/components/simulator-action.js b/src/components/simulator-action.js new file mode 100644 index 0000000..aef6625 --- /dev/null +++ b/src/components/simulator-action.js @@ -0,0 +1,68 @@ +/** + * File: simulator-actionm.js + * Author: Markus Grigull + * Date: 12.04.2018 + * + * This file is part of VILLASweb. + * + * VILLASweb is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * VILLASweb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with VILLASweb. If not, see . + ******************************************************************************/ + +import React from 'react'; +import { Button, DropdownButton, MenuItem } from 'react-bootstrap'; + +class SimulatorAction extends React.Component { + constructor(props) { + super(props); + + this.state = { + selectedAction: null + }; + } + + componentWillReceiveProps(nextProps) { + if (this.state.selectedAction == null) { + if (nextProps.actions != null && nextProps.actions.length > 0) { + this.setState({ selectedAction: nextProps.actions[0] }); + } + } + } + + setAction = id => { + // search action + for (let action of this.props.actions) { + if (action.id === id) { + this.setState({ selectedAction: action }); + } + } + } + + render() { + const actionList = this.props.actions.map(action => ( + + {action.title} + + )); + + return
+ + {actionList} + + + +
; + } +} + +export default SimulatorAction; diff --git a/src/containers/simulation.js b/src/containers/simulation.js index 1d40cf4..00a6916 100644 --- a/src/containers/simulation.js +++ b/src/containers/simulation.js @@ -21,7 +21,7 @@ import React from 'react'; import { Container } from 'flux/utils'; -import { Button, Modal, Glyphicon, DropdownButton, MenuItem } from 'react-bootstrap'; +import { Button, Modal, Glyphicon } from 'react-bootstrap'; import FileSaver from 'file-saver'; import SimulationStore from '../stores/simulation-store'; @@ -34,6 +34,7 @@ import TableColumn from '../components/table-column'; import NewSimulationModelDialog from '../components/dialog/new-simulation-model'; import EditSimulationModelDialog from '../components/dialog/edit-simulation-model'; import ImportSimulationModelDialog from '../components/dialog/import-simulation-model'; +import SimulatorAction from '../components/simulator-action'; class Simulation extends React.Component { static getStores() { @@ -55,8 +56,6 @@ class Simulation extends React.Component { simulation: {}, - runAction: 0, - runTitle: 'Start', selectedSimulationModels: [] } } @@ -202,57 +201,7 @@ class Simulation extends React.Component { this.setState({ selectedSimulationModels }); } - setRunAction(index) { - let runTitle = ''; - switch (index) { - case '0': - runTitle = 'Start'; - break; - - case '1': - runTitle = 'Stop'; - break; - - case '2': - runTitle = 'Pause'; - break; - - case '3': - runTitle = 'Resume'; - break; - - default: - console.log('Unknown index ' + index); - break; - } - - this.setState({ runAction: index, runTitle }); - } - - runAction() { - let data; - switch (this.state.runAction) { - case '0': - data = { action: 'start' }; - break; - - case '1': - data = { action: 'stop' }; - break; - - case '2': - data = { action: 'pause' }; - break; - - case '3': - data = { action: 'resume' }; - break; - - default: - console.warn('Unknown simulator action: ' + this.state.runAction); - return; - } - + runAction = action => { for (let index of this.state.selectedSimulationModels) { // get simulator for model let simulator = null; @@ -269,7 +218,7 @@ class Simulation extends React.Component { AppDispatcher.dispatch({ type: 'simulators/start-action', simulator, - data, + data: action.data, token: this.state.sessionToken }); } @@ -298,14 +247,15 @@ class Simulation extends React.Component {
- this.setRunAction(index)}> - Start - Stop - Pause - Resume - - - +
diff --git a/src/containers/simulations.js b/src/containers/simulations.js index 08cd731..d762bb9 100644 --- a/src/containers/simulations.js +++ b/src/containers/simulations.js @@ -21,7 +21,7 @@ import React, { Component } from 'react'; import { Container } from 'flux/utils'; -import { Button, Modal, Glyphicon, DropdownButton, MenuItem } from 'react-bootstrap'; +import { Button, Modal, Glyphicon } from 'react-bootstrap'; import FileSaver from 'file-saver'; import AppDispatcher from '../app-dispatcher'; @@ -34,6 +34,7 @@ import TableColumn from '../components/table-column'; import NewSimulationDialog from '../components/dialog/new-simulation'; import EditSimulationDialog from '../components/dialog/edit-simulation'; import ImportSimulationDialog from '../components/dialog/import-simulation'; +import SimulatorAction from '../components/simulator-action'; class Simulations extends Component { static getStores() { @@ -52,8 +53,6 @@ class Simulations extends Component { importModal: false, modalSimulation: {}, - runAction: 0, - runTitle: 'Start', selectedSimulations: [] }; } @@ -187,57 +186,7 @@ class Simulations extends Component { this.setState({ selectedSimulations }); } - setRunAction(index) { - let runTitle = ''; - switch (index) { - case '0': - runTitle = 'Start'; - break; - - case '1': - runTitle = 'Stop'; - break; - - case '2': - runTitle = 'Pause'; - break; - - case '3': - runTitle = 'Resume'; - break; - - default: - console.log('Unknown index ' + index); - break; - } - - this.setState({ runAction: index, runTitle }); - } - - runAction() { - let data; - switch (this.state.runAction) { - case '0': - data = { action: 'start' }; - break; - - case '1': - data = { action: 'stop' }; - break; - - case '2': - data = { action: 'pause' }; - break; - - case '3': - data = { action: 'resume' }; - break; - - default: - console.warn('Unknown simulator action: ' + this.state.runAction); - return; - } - + runAction = action => { for (let index of this.state.selectedSimulations) { for (let model of this.state.simulations[index].models) { // get simulator for model @@ -255,7 +204,7 @@ class Simulations extends Component { AppDispatcher.dispatch({ type: 'simulators/start-action', simulator, - data, + data: action.data, token: this.state.sessionToken }); } @@ -282,14 +231,15 @@ class Simulations extends Component {
- this.setRunAction(index)}> - Start - Stop - Pause - Resume - - - +
diff --git a/src/containers/simulators.js b/src/containers/simulators.js index 00e1ce9..36f21dd 100644 --- a/src/containers/simulators.js +++ b/src/containers/simulators.js @@ -21,7 +21,7 @@ import React, { Component } from 'react'; import { Container } from 'flux/utils'; -import { Button, Modal, Glyphicon, DropdownButton, MenuItem } from 'react-bootstrap'; +import { Button, Modal, Glyphicon } from 'react-bootstrap'; import FileSaver from 'file-saver'; import _ from 'lodash'; @@ -34,6 +34,7 @@ import TableColumn from '../components/table-column'; import NewSimulatorDialog from '../components/dialog/new-simulator'; import EditSimulatorDialog from '../components/dialog/edit-simulator'; import ImportSimulatorDialog from '../components/dialog/import-simulator'; +import SimulatorAction from '../components/simulator-action'; class Simulators extends Component { static getStores() { @@ -48,8 +49,6 @@ class Simulators extends Component { modalSimulator: {}, deleteModal: false, - runAction: 0, - runTitle: 'Reset', selectedSimulators: [] }; } @@ -146,42 +145,12 @@ class Simulators extends Component { this.setState({ selectedSimulators }); } - setRunAction(index) { - let runTitle = ''; - switch (index) { - case '0': - runTitle = 'Reset'; - break; - - case '1': - runTitle = 'Shutdown'; - break; - - default: - console.log('Unknown index ' + index); - break; - } - - this.setState({ runAction: index, runTitle }); - } - - runAction() { + runAction = action => { for (let index of this.state.selectedSimulators) { - let data; - switch (this.state.runAction) { - case '0': - data = { action: 'reset' }; - break; - - case '1': - data = { action: 'shutdown' }; - break; - } - AppDispatcher.dispatch({ type: 'simulators/start-action', simulator: this.state.simulators[index], - data, + data: action.data, token: this.state.sessionToken }); } @@ -212,12 +181,10 @@ class Simulators extends Component {
- this.setRunAction(index)}> - Reset - Shutdown - - - +
diff --git a/src/stores/simulator-store.js b/src/stores/simulator-store.js index 75623c0..58bc3e6 100644 --- a/src/stores/simulator-store.js +++ b/src/stores/simulator-store.js @@ -38,8 +38,8 @@ class SimulatorStore extends ArrayStore { } else if (simulator.rawProperties != null && 'endpoint' in simulator.rawProperties) { SimulatorDataDataManager.open(simulator.rawProperties.endpoint, simulator._id); } else { - console.warn('Endpoint not found for simulator'); - console.log(simulator); + // console.warn('Endpoint not found for simulator'); + // console.log(simulator); } }