From 497395ae3912b91d4fee5c2b45965d5ab3a3bf40 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sun, 2 Nov 2014 22:39:52 +0100 Subject: [PATCH] GUI for createing strategies with parametersTemplate #34 --- .../js/components/strategy/CreateStrategy.jsx | 64 --------- .../components/strategy/StrategyComponent.jsx | 13 +- .../js/components/strategy/StrategyForm.jsx | 132 ++++++++++++++++++ webpack.config.js | 2 +- 4 files changed, 144 insertions(+), 67 deletions(-) delete mode 100644 public/js/components/strategy/CreateStrategy.jsx create mode 100644 public/js/components/strategy/StrategyForm.jsx diff --git a/public/js/components/strategy/CreateStrategy.jsx b/public/js/components/strategy/CreateStrategy.jsx deleted file mode 100644 index 3e61f4e6f7..0000000000 --- a/public/js/components/strategy/CreateStrategy.jsx +++ /dev/null @@ -1,64 +0,0 @@ -var React = require('react'); - -var CreateStrategy = React.createClass({ - - getInitialState: function() { - return { - parameters: [] - }; - }, - - - onSubmit: function(event) { - event.preventDefault(); - console.log(event); - }, - - handleAddParam: function(event) { - event.preventDefault(); - var id = this.state.parameters.length + 1; - var params = this.state.parameters.concat([{id:id, name: "param_" + id, label: "Parameter " +id}]); - this.setState({parameters: params}); - }, - - render: function() { - var parameters = (this.state.parameters.map(function(param) { - return
- -
- -
-
- })); - - return ( -
-
-
-
- Create strategy -
- -
- -
-
- - {parameters} - - -
- - -
-
-
-
-
- ); - } -}); - -module.exports = CreateStrategy; \ No newline at end of file diff --git a/public/js/components/strategy/StrategyComponent.jsx b/public/js/components/strategy/StrategyComponent.jsx index b72a38fbd1..2549f4328d 100644 --- a/public/js/components/strategy/StrategyComponent.jsx +++ b/public/js/components/strategy/StrategyComponent.jsx @@ -1,6 +1,6 @@ var React = require('react'), StrategyList = require('./StrategyList'), - CreateStrategy = require('./CreateStrategy'), + StrategyForm = require('./StrategyForm'), strategyStore = require('../../stores/StrategyStore'), ErrorMessages = require('../ErrorMessages'); @@ -40,6 +40,15 @@ var StrategyComponent = React.createClass({ this.setState({createView: false}); }, + handleSave: function(strategy) { + var strategies = this.state.strategies.concat([strategy]); + this.setState({ + createView: false, + strategies: strategies + }); + console.log("Saved strategy: ", strategy); + }, + render: function() { return (
@@ -55,7 +64,7 @@ var StrategyComponent = React.createClass({ }, renderCreateView: function() { - return () + return () }, renderCreateButton: function() { diff --git a/public/js/components/strategy/StrategyForm.jsx b/public/js/components/strategy/StrategyForm.jsx new file mode 100644 index 0000000000..d153c0d339 --- /dev/null +++ b/public/js/components/strategy/StrategyForm.jsx @@ -0,0 +1,132 @@ +var React = require('react'); + +var StrategyForm = React.createClass({ + + getDefaultProps: function() { + return { + maxParams: 4 + }; + }, + + getInitialState: function() { + return { + parameters: [] + }; + }, + + onSubmit: function(event) { + event.preventDefault(); + + var strategy = {}; + strategy.name = this.refs.strategy_name.getDOMNode().value.trim(); + strategy.parametersTemplate = {}; + + var that = this; + + this.state.parameters.forEach(function(parameter) { + var name = that.refs[parameter.name].getDOMNode().value.trim(); + if(name) { + strategy.parametersTemplate[name] = "string"; + } + }); + + this.props.handleSave(strategy); + }, + + handleAddParam: function(event) { + event.preventDefault(); + var id = this.state.parameters.length + 1; + var params = this.state.parameters.concat([{id:id, name: "param_" + id, label: "Parameter " +id}]); + this.setState({parameters: params}); + }, + + handleRemoveParam: function(event) { + event.preventDefault(); + var id = this.state.parameters.length + 1; + var params = this.state.parameters.slice(0, -1); + + this.setState({parameters: params}); + }, + + render: function() { + return ( +
+
+
+
+ Create strategy +
+ +
+ +
+
+ + {this.renderParameters()} + {this.renderRemoveLink()} + + +
+ + + {this.renderAddLink()} +
+
+
+
+
+ ); + }, + + renderParameters: function() { + return this.state.parameters.map(function(param) { + return ( +
+ +
+
+ +
+ +
+ +
+ +
+
+
+
+ ) + }); + }, + + renderAddLink: function() { + if(this.state.parameters.length < this.props.maxParams) { + return + Add required parameter; + } + }, + renderRemoveLink: function() { + if(this.state.parameters.length > 0) { + return ( +
+ - Remove parameter +
+ ); + } + } +}); + +module.exports = StrategyForm; \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 59f9d36289..4d256a7045 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -17,7 +17,7 @@ module.exports = { module: { loaders: [ - { test: /\.jsx$/, loader: 'jsx' } + { test: /\.jsx$/, loader: 'jsx?harmony' } ] },