Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist settings between reloads #49

Open
wants to merge 5 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
factorio-calc
=============

For all your obsessive optimizing needs
For all your obsessive optimizing needs.

https://rubyruy.github.io/factorio-calc/

# Contribute a data source #

Expand All @@ -18,4 +20,4 @@ Additional data sources are welcome - the calculator should work just fine with

## See Also ##

[Forum Thread](http://www.factorioforums.com/forum/viewtopic.php?f=5&t=4553)
[Forum Thread](http://www.factorioforums.com/forum/viewtopic.php?f=5&t=4553)
30 changes: 18 additions & 12 deletions calc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var Bulk = App.Bulk;

class Calc extends React.Component {
state = {
input: {recipe: "", ipm: 1 },
additionalInputs: [],
options: {
// Restore the inputs and options from persistent storage.
input: JSON.parse(localStorage.getItem("input")) || { recipe: "", ipm: 1 },
additionalInputs: JSON.parse(localStorage.getItem("additionalInputs")) || [],
options: JSON.parse(localStorage.getItem("options")) || {
asslvl: "0.5",
smeltlvl: "1",
smeltlvl:"1",
beltlvl: "5.7",
difficulty: "normal",
alwaysShowDecimals: false
Expand All @@ -37,6 +38,19 @@ class Calc extends React.Component {
});
var result = Calculator.calculateAndAnalyze(inputs, this.state.options);
this.setState({result: result});

// Write the inputs and options to persistent storage.
localStorage.setItem("input", JSON.stringify(this.state.input));
localStorage.setItem("additionalInputs", JSON.stringify(this.state.additionalInputs));
localStorage.setItem("options", JSON.stringify(this.state.options));
}

getInputs() {
if (this.state.input.recipe) {
return _.union([this.state.input], this.state.additionalInputs);
} else {
return this.state.additionalInputs;
}
}

setInput = (input) => {
Expand Down Expand Up @@ -144,12 +158,4 @@ class Calc extends React.Component {
</div>
);
}

getInputs() {
if (this.state.input.recipe) {
return _.union([this.state.input], this.state.additionalInputs);
} else {
return this.state.additionalInputs;
}
}
};
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<script src="https://unpkg.com/react@15/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.1/babel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-modal/1.7.7/react-modal.js" integrity="sha256-wQ9go2p9/RzvdqC0RzS2djz4aujaK10VA5UnV9A5eXI=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-modal/1.9.7/react-modal.min.js" integrity="sha256-1X9+oPwWzI4TxGrBeDiMTewsUZIe4gqha8D8hL9+fK8=" crossorigin="anonymous"></script>
<script src="bower_components/d3/d3.min.js"></script>
<script src="bower_components/lodash/lodash.js"></script>
<script src="bower_components/graphlib/dist/graphlib.core.js"></script>
Expand Down
84 changes: 42 additions & 42 deletions input.jsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
/* global React,App*/
/* global React,App*/

App.Input = class Input extends React.Component {
componentWillMount() {
this.recipes = App.getData.call();
}
componentWillMount() {
this.recipes = App.getData.call();
}

render() {
var self = this;
var onChangeRecipe = function(event) {
self.props.onChange({recipe: event.target.value, ipm: self.props.input.ipm});
};
var onChangeIpm = function(event) {
self.props.onChange({recipe: self.props.input.recipe, ipm: parseFloat(event.target.value)});
};
render() {
var self = this;
var onChangeRecipe = function (event) {
self.props.onChange({ recipe: event.target.value, ipm: self.props.input.ipm });
};
var onChangeIpm = function (event) {
self.props.onChange({ recipe: self.props.input.recipe, ipm: parseFloat(event.target.value) });
};

return (
<div>
<p>
Calculate the requirements for
return (
<div>
<p>
Calculate the requirements for
<input
id="recipe"
type="text"
list="recipes"
placeholder="recipe name"
value={this.props.input.recipe}
onChange={onChangeRecipe}/>
<datalist id="recipes">
{Object.keys(this.recipes).map(function(recipe) {
return <option key={recipe}>{recipe}</option>;
})}
</datalist>
producing at a rate of
id="recipe"
type="text"
list="recipes"
placeholder="recipe name"
value={this.props.input.recipe}
onChange={onChangeRecipe} />
<datalist id="recipes">
{Object.keys(this.recipes).map(function (recipe) {
return <option key={recipe}>{recipe}</option>;
})}
</datalist>
producing at a rate of
<input
id="ipm"
type="number"
min="0"
step="any"
value={this.props.input.ipm}
onChange={onChangeIpm}/>
item(s) / minute.
<button id="addAnother" onClick={ this.props.onAddAnother }>Add Another</button>
<button id="clear" onClick={ this.props.onClear }>Clear</button>
<button id="bulk" onClick={ this.props.onBulk }>Bulk</button>
</p>
</div>
);
}
id="ipm"
type="number"
min="0"
step="any"
value={this.props.input.ipm}
onChange={onChangeIpm} />
item(s) / minute.
<button id="addAnother" onClick={this.props.onAddAnother}>Add Another</button>
<button id="clear" onClick={this.props.onClear}>Clear</button>
<button id="bulk" onClick={this.props.onBulk}>Bulk</button>
</p>
</div>
);
}
};
1 change: 0 additions & 1 deletion options.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ App.Options = class Options extends React.Component {
<span className="glyphicon glyphicon-expand"></span>
Show options
</a>

</p>
);
}
Expand Down
4 changes: 4 additions & 0 deletions serve-github-pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
# Pass these values as command line arguments to httpd.
ARGV.replace [File.expand_path('.', __dir__), '--port=8080']

# Opens the GitHub Pages in the system default browser.
# This has no effect when explorer.exe is not available, such as on Linux.
system('explorer.exe', 'http://localhost:8080')

# Start the http server.
httpd