Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Refactor BrainBtnPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
supermario committed Apr 27, 2016
1 parent 6d7bdd4 commit b75d71c
Showing 1 changed file with 8 additions and 40 deletions.
48 changes: 8 additions & 40 deletions src/scripts/brain_btn_panel.cjsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,25 @@ ApiBrain = require './api_brain'
LocoBrain = require './loco_brain'

BrainBtnMixin =
componentDidMount: ->
@subscribe 'brain-selected', @onBrainSelected
@subscribe 'brain-deselected', @onBrainSelected

componentWillUnmount: ->
@unsubscribe 'brain-selected'
@unsubscribe 'brain-deselected'

onBrainSelected: (player) ->
return unless player is @props.player

if @props.player.brain?.constructor is @brainConstructor()
@setState(selected: true)
else
@setState(selected: false)

onClick: ->
if @props.player.brain
delete @props.player.brain
MessageBus.publish 'brain-deselected', @props.player
@setState(selected: false)

else
return unless brain = @newBrain()
@props.player.implant( brain )
MessageBus.publish 'brain-selected', @props.player
@setState(selected: true)

getInitialState: ->
selected: false



ApiBrainBtn = React.createClass
mixins: [
MessageBusMixin
BrainBtnMixin
]

brainConstructor: ->
ApiBrain
mixins: [ BrainBtnMixin ]

newBrain: ->
return unless url = document.querySelector(".api-url.#{@props.player.name}").value
new ApiBrain(url)
new ApiBrain(@refs.url.value)

render: ->
btnStyle =
Expand All @@ -58,19 +34,13 @@ ApiBrainBtn = React.createClass
className += " selected" if @state.selected

<div style={ btnStyle } className={ className }>
<input placeholder="Enter API url..." className="api-url #{@props.player.name}" />
<input ref="url" placeholder="Enter API url..." className="api-url" />
<img src="./images/brain.png" style={imgStyle} onClick={ @onClick } />
</div>


LocoBrainBtn = React.createClass
mixins: [
MessageBusMixin
BrainBtnMixin
]

brainConstructor: ->
LocoBrain
mixins: [ BrainBtnMixin ]

newBrain: ->
new LocoBrain
Expand All @@ -90,9 +60,7 @@ LocoBrainBtn = React.createClass

PlayerBrainPanel = React.createClass
render: ->
className = "player #{@props.player.name}"

<div className={className}>
<div className="player">
<ApiBrainBtn player={@props.player}></ApiBrainBtn>
<LocoBrainBtn player={@props.player}></LocoBrainBtn>
</div>
Expand All @@ -112,4 +80,4 @@ BrainBtnPanel = React.createClass

<div style={style}>{btns}</div>

module.exports = BrainBtnPanel
module.exports = BrainBtnPanel

0 comments on commit b75d71c

Please sign in to comment.