-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathscheduling_checks.jsx
208 lines (192 loc) · 5.26 KB
/
scheduling_checks.jsx
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
/**
* List of scheduling checks.
*
* Should have individual SchedulingCheck elements as children.
*/
var SchedulingCheckList = React.createClass({
render: function () {
return <div className="scheduling-check-list">
{this.props.children}
</div>;
},
});
/**
* A single scheduling check.
*
* This might or might not be loaded yet; clicking the heading will load the
* data from the server and expand it.
*/
var SchedulingCheck = React.createClass({
propTypes: {
slug: React.PropTypes.string.isRequired,
title: React.PropTypes.string,
},
getInitialState: function () {
return {
open: false,
failed: false,
timestamp: "never",
};
},
handleClick: function () {
if (this.state.open) {
this.setState({open: false})
} else {
this.setState({open: true});
if (!this.state.data) {
this.loadData();
}
}
},
timestamp: function () {
var now = new Date();
return now.toLocaleTimeString();
},
loadData: function () {
// remove any existing data, so we see a loading thing again
this.setState({data: undefined});
$j.get("scheduling_checks/" + this.props.slug)
.done(function (data) {
this.setState({
data: data,
failed: false,
timestamp: this.timestamp(),
});
}.bind(this))
.fail(function (data) {
this.setState({
failed: true,
timestamp: this.timestamp(),
});
}.bind(this));
},
render: function () {
var body;
if (this.state.failed) {
body = <div className="placeholder">
(loaded {this.state.timestamp}, loading failed ☹)
</div>;
} else if (!this.state.open) {
body = <div className="placeholder">
(loaded {this.state.timestamp}, click title to open)
</div>;
} else if (!this.state.data) {
body = <div className="placeholder">loading...</div>;
} else {
var data = JSON.parse(this.state.data); // Might not work on old browsers
var table;
if (data.headings.length == 0) {
var settings = {
header: false
};
table = <SelectTable rows = {data.body} settings = {settings} />;
} else {
var columns = [];
for (i = 0; i < data.headings.length; i++) {
if (!!data.headings[i]) {
columns[i] = {key: String(i), label: data.headings[i]};
} else {
columns[i] = {key: String(i), label: " "};
}
}
table = <SelectTable rows = {data.body} columns = {columns} />;
}
body = <div>
<div className="placeholder">
(loaded {this.state.timestamp}, click title to close)
</div>
{table}
</div>;
}
return <div className="scheduling-check">
<div className="scheduling-check-title">
<span onClick={this.handleClick}>{this.props.title}</span>
<RefreshButton onClick={this.loadData} />
</div>
<div className="scheduling-check-body">
{body}
</div>
</div>;
},
});
/**
* A refresh button, which calls its onClick prop
*/
var RefreshButton = React.createClass({
propTypes: {
onClick: React.PropTypes.func.isRequired,
},
render: function () {
return <button onClick={this.props.onClick} className="refresh-button">
↻
</button>;
},
});
// Modified from react-json-table example code.
var SelectTable = React.createClass({
getInitialState: function(){
// We will store the sorted column and whether each row is greyed out
var temp = new Array();
for (i = 0; i < this.props.rows.length; i++) {
temp[this.props.rows[i]] = false;
}
if (this.props.settings == undefined) {
this.props.settings = {
header: true
};
}
return {sort: -1, greyed : temp};
},
render: function(){
var me = this,
// clone the rows
items = this.props.rows.slice()
;
// Sort the table
if( this.state.sort ){
items.sort( function( a, b ){
return a[ me.state.sort ] > b[ me.state.sort ] ? 1 : -1;
});
}
if (this.props.columns == undefined) {
return <JsonTable
rows={items}
settings={ this.getSettings() }
onClickHeader={ this.onClickHeader }
onClickRow={ this.onClickRow }
/>;
} else {
return <JsonTable
rows={items}
columns={this.props.columns}
settings={ this.getSettings() }
onClickHeader={ this.onClickHeader }
onClickRow={ this.onClickRow }
/>;
}
},
getSettings: function(){
var me = this;
// We will add some classes to the selected rows and cells
return {
headerClass: function( current, key ){
if( me.state.sort == key )
return current + ' headerSelected';
return current;
},
rowClass: function( current, item ){
if( me.state.greyed[item] )
return current + ' rowGreyed';
return current;
},
header: this.props.settings.header
};
},
onClickHeader: function( e, column ){
this.setState( {sort: column} );
},
onClickRow: function( e, item ){
this.state.greyed[item] = !this.state.greyed[item];
this.setState(); // so that it actually updates
}
});