Created
March 1, 2015 11:09
-
-
Save antris/a39554e01ccdc243f17c to your computer and use it in GitHub Desktop.
History browsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react') | |
var Immutable = require('immutable') | |
var Bacon = require('baconjs') | |
var NextPiecesView = require('./views/nextPiecesView') | |
var {worldStream} = require('./playField') | |
var PlayField = require('./views/playFieldView') | |
var input = require('./input') | |
var history = Immutable.List.of() | |
var worldStreamWithHistory = worldStream.map(Immutable.Map).map(function(world) { | |
history = history.push(world) | |
return Immutable.Map({ | |
world, | |
history | |
}) | |
}) | |
var Main = React.createClass({ | |
getInitialState: function() { return { selectedWorldFromHistory: undefined } }, | |
onSlide: function() { | |
var historyNth = Number(this.refs.historySlider.getDOMNode().value) | |
var world = this.props.history.get(historyNth) | |
this.setState({ selectedWorldFromHistory: world }) | |
}, | |
render: function() { | |
var world = this.state.selectedWorldFromHistory ? this.state.selectedWorldFromHistory : this.props.world | |
return <div> | |
<p>History size: {this.props.history.size}</p> | |
<p><input type="range" min="0" max={this.props.history.size} onChange={this.onSlide} ref="historySlider" /></p> | |
<NextPiecesView pieces={world.get('nextPieces')} /> | |
<PlayField world={world} /> | |
</div> | |
} | |
}) | |
var Initializer = React.createClass({ | |
componentDidMount: function() { | |
var component = this | |
this.props.worldStreamWithHistory.onValue(function(x) { | |
component.setState({ | |
world: x.get('world'), history: x.get('history') }) | |
} | |
) | |
}, | |
render: function() { | |
return this.state && this.state.world ? <Main world={this.state.world} history={this.state.history} /> : <div /> | |
} | |
}) | |
React.render( | |
<Initializer worldStreamWithHistory={worldStreamWithHistory} />, | |
document.getElementById('main') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment