-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChipViews.js
143 lines (136 loc) · 3.67 KB
/
ChipViews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @jsx React.DOM
*/
"use strict";
var React = require('react');
var _ = require('underscore');
var Colors = require('./Colors');
var ActionStore = require('./ActionStore');
var ChipView = React.createClass({
propTypes: {
color: React.PropTypes.oneOf(_.values(Colors)).isRequired,
highlighted: React.PropTypes.bool,
onChipEnter: React.PropTypes.func,
onChipLeave: React.PropTypes.func,
},
handleMouseEnter: function() {
this.props.onChipEnter && this.props.onChipEnter(this.props.color);
},
handleMouseLeave: function() {
this.props.onChipLeave && this.props.onChipLeave(this.props.color);
},
render: function() {
var class_string = 'chip ' + this.props.color;
if (this.props.highlighted) {
class_string += ' highlighted';
}
return (<span
className={class_string}
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
/>);
},
});
function generate_list_of_chips(chips_by_color) {
return _.reduce(chips_by_color, function(memo, count, color) {
_.times(count, function(i) {
memo.push(<ChipView key={color + i} color={color} />);
});
return memo;
}, []);
}
var ChipListView = React.createClass({
propTypes: {
chips: React.PropTypes.object.isRequired,
},
render: function() {
var chips = generate_list_of_chips(this.props.chips);
return (
<div className="chip-list">
{chips}
</div>
);
},
});
var ChipPileView = React.createClass({
propTypes: {
color: React.PropTypes.oneOf(_.values(Colors)).isRequired,
count: React.PropTypes.number.isRequired,
onChipEnter: React.PropTypes.func,
onChipLeave: React.PropTypes.func,
},
render: function() {
var color = this.props.color;
var style = {
display: 'inline-flex',
flexDirection: 'column',
marginLeft: '10px',
flexWrap: 'nowrap',
alignItems: 'center',
};
return (
<div
key={color}
style={style}
onClick={this.props.onClick} >
<ChipView color={color}
highlighted={this.props.highlighted}
onChipEnter={this.props.onChipEnter}
onChipLeave={this.props.onChipLeave}
/>
<span className="chip-count">{this.props.count}</span>
</div>
);
},
});
var ChipSupplyView = React.createClass({
propTypes: {
game: React.PropTypes.object.isRequired,
},
getInitialState: function() {
return {hoveredColor: null};
},
onChipClick: function(color) {
ActionStore.didClickSupplyChip(color);
},
onChipEnter: function(color) {
this.setState({hoveredColor: color});
},
onChipLeave: function(color) {
this.setState({hoveredColor: null});
},
render: function() {
var selection = ActionStore.getSelection();
var chips = _.map(Colors, function (color) {
var onClickFunc = _.partial(this.onChipClick, color);
var highlight = ActionStore.isActionRequest() &&
(this.state.hoveredColor === color ||
(selection && selection.chips && selection.chips[color] > 0));
return (
<ChipPileView
key={color}
color={color}
highlighted={highlight}
count={this.props.game.chipSupply[color]}
onClick={onClickFunc}
onChipEnter={this.onChipEnter}
onChipLeave={this.onChipLeave}
/>
);
}, this);
return (
<div className="chip-supply">
<span className="chip-supply-title">Chip Supply</span>
<div className="chip-piles">
{chips}
</div>
</div>
);
},
});
module.exports = {
ChipView: ChipView,
ChipListView: ChipListView,
ChipSupplyView: ChipSupplyView,
ChipPileView: ChipPileView,
};