-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrow.js
222 lines (192 loc) · 6.26 KB
/
row.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var React = window.React || require('react/addons');
var _ = require('lodash');
var Estimator = require('../estimator');
var Status = require('../status');
var Tags = require('../tags');
var TagEditor = require('../tag_editor');
var moment = require('moment');
/*
* Renders a single table row for displaying item data.
* Row accepts expanded prop for controlling whether the row appears
* condensed (default) or optionally expanded (if using row with the sortable TableHeader.
* Toggling between the two states happens via the Expander element in TableHeader.)
* TODO(fw): reorg styles so don't have to calculate here.
*/
var abbreviateUsername = function (user) {
return user.last_name ? user.first_name + ' ' + user.last_name[0] + '.' :
user.first_name;
};
var TableRow = React.createClass({
propTypes: {
model: React.PropTypes.object.isRequired,
columns: React.PropTypes.arrayOf(React.PropTypes.string).isRequired,
expanded: React.PropTypes.bool,
baseUrl: React.PropTypes.string,
modelChangerUtilities: React.PropTypes.object,
navigatorUtility: React.PropTypes.object,
isBulkEditable: React.PropTypes.bool,
onBulkSelect: React.PropTypes.func
},
getCellBuilder: function(column, className, id) {
var methodMap = {
product: this.buildProductCell,
number: this.buildNumberCell,
size: this.buildEstimateCell,
status: this.buildStatusCell,
title: this.buildTitleCell,
tags: this.buildTagsCell,
'created by': this.buildCreatedByCell,
'assigned to': this.buildAssigneeCell,
created: this.buildCreatedAtCell
};
return methodMap[column](className, id);
},
render: function() {
var modelId = [this.props.model.product.id, this.props.model.number];
var wrapperClass = 'wrapper ' + (this.props.expanded ? 'expanded' : 'condensed');
var rowClass = 'sortable__row ' + this.props.model.type;
if (this.props.expanded) {
rowClass += ' expanded';
}
// "matched": item is grouped with parent/subitems.
// "nonMatching": item doesn't fit collection filters, but is included to match w/subitems that do.
if (this.props.model.isMatched) {
rowClass += this.props.model.isNonMatching ? ' matched non-matching' : ' matched';
}
var cells = [];
if (this.props.isBulkEditable) {
cells.push(
<td key={'control' + ':' + modelId} className='sortable__cell'>
{this.buildControlCell(wrapperClass)}
</td>
);
}
_.each(this.props.columns, function(column) {
cells.push(
<td key={column + ':' + modelId} className='sortable__cell'>
{this.getCellBuilder(column, wrapperClass, modelId)}
</td>
);
}, this);
return (
<tr className={rowClass}>
{cells}
</tr>
);
},
buildControlCell: function(classes) {
// Left border on matched rows causes padding weirdness in checkboxes,
// so we add corrective styles on matched rows.
classes += this.props.model.isMatched ? ' narrow matched' : ' narrow';
return (
<div className={classes}>
<input type='checkbox' onClick={_.partial(this.props.onBulkSelect, this.props.model)} />
</div>
);
},
buildProductCell: function(classes) {
var linkProps = {
href: this.props.baseUrl + '/product/' + this.props.model.product.id,
className: 'js-item-link link product-cell',
};
var subitemClass = this.props.expanded ? 'subitem expanded' : 'subitem';
var subitemArrow = this.props.model.parent ? <i className={subitemClass}></i> : null;
return (
<div className={classes + ' wider'}>
{subitemArrow}
<a {...linkProps}>{this.props.model.product.name}</a>
</div>
);
},
buildNumberCell: function(classes) {
var props = {
href: this.props.baseUrl + '/product/' + this.props.model.product.id + '/item/' + this.props.model.number,
className: 'js-item-link link number-cell',
'data-item-number': this.props.model.number
};
return (
<div className={classes}>
<a {...props}>#{this.props.model.number}</a>
</div>
);
},
buildEstimateCell: function(classes, mId) {
var props = {
modelId: mId,
readOnly: !!this.props.model.isNonMatching,
itemType: this.props.model.type,
score: this.props.model.score,
estimateChanger: this.props.modelChangerUtilities.estimateChanger
};
return (
<div className={classes + ' narrow'}>
<Estimator {...props} />
</div>
);
},
buildStatusCell: function(classes, mId) {
var props = {
modelId: mId,
readOnly: !!this.props.model.isNonMatching,
status: this.props.model.status,
statusChanger: this.props.modelChangerUtilities.statusChanger
};
return (
<div className={classes + ' narrow'}>
<Status {...props} />
</div>
);
},
buildAssigneeCell: function(classes, mId) {
// TODO(fw): implement
return (<div></div>);
},
buildCreatedByCell: function(classes) {
return (
<div className={classes}>
{abbreviateUsername(this.props.model.created_by)}
</div>
);
},
buildTitleCell: function(classes) {
var props = {
href: this.props.baseUrl + '/product/' + this.props.model.product.id + '/item/' + this.props.model.number,
className: 'js-item-link link title-cell',
'data-item-number': this.props.model.number
};
return (
<div className={classes + ' widest'}>
<a {...props}>
{this.props.model.title}
</a>
</div>
);
},
buildTagsCell: function(classes, mId) {
var editorProps = {
modelId: mId,
readOnly: !!this.props.model.isNonMatching,
tags: this.props.model.tags,
tagChanger: this.props.modelChangerUtilities.tagChanger
};
var tagsProps = {
tags: this.props.model.tags,
condensed: !this.props.expanded,
navigatorUtility: this.props.navigatorUtility
};
return (
<div className={classes + ' wider'}>
<TagEditor {...editorProps} />
<Tags {...tagsProps} />
</div>
);
},
buildCreatedAtCell: function(classes) {
return (
<div className={classes + ' wide'}>
{moment(this.props.model.created_at).format('MM/DD/YY')}
</div>
);
}
});
module.exports = TableRow;