forked from Automattic/kue
/
main.js
226 lines (186 loc) · 4.06 KB
/
main.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
223
224
225
226
/*!
* kue - http - main
* Copyright (c) 2011 LearnBoost <tj@learnboost.com>
* MIT Licensed
*/
// TODO: clean up
// TODO: server-side config for this stuff
/**
* Active state.
*/
var active;
/**
* Active type filter.
*/
var filter;
/**
* Number of jobs fetched when "more" is clicked.
*/
var more = 10;
/**
* Number of jobs shown.
*/
var to = more;
/**
* Sort order.
*/
var sort = 'asc';
/**
* Initialize UI.
*/
function init(state) {
pollStats(1000);
show(state)();
o('li.inactive a').click(show('inactive'));
o('li.complete a').click(show('complete'));
o('li.active a').click(show('active'));
o('li.failed a').click(show('failed'));
o('#more').click(function(){
to += more;
return false;
});
o('#filter').change(function(){
filter = $(this).val();
});
o('#sort').change(function(){
sort = $(this).val();
o('#jobs .job').remove();
});
onpopstate = function(e){
if (e.state) show(e.state.state)();
};
}
/**
* Show jobs with `state`.
*
* @param {String} state
* @param {Boolean} init
* @return {Function}
*/
function show(state) {
return function(){
active = state;
if (pollForJobs.timer) clearTimeout(pollForJobs.timer);
history.pushState({ state: state }, state, state);
o('#jobs .job').remove();
o('#menu li a').removeClass('active');
o('#menu li.' + state + ' a').addClass('active');
o('#more').hide();
pollForJobs(state, 2000);
return false;
}
}
/**
* Poll for jobs with `state` every `ms`.
*
* @param {String} state
* @param {Number} ms
*/
function pollForJobs(state, ms) {
o('h1').text(state);
refreshJobs(state, function(){
pollForJobs.timer = setTimeout(function(){
pollForJobs(state, ms);
}, 1000);
});
};
/**
* Re-request and refresh job elements.
*
* @param {String} state
* @param {Function} fn
*/
function refreshJobs(state, fn) {
// TODO: clean this crap up
var url = '/jobs/'
+ (filter ? filter + '/' : '')
+ state + '/0..' + to
+ '/' + sort;
request(url, function(jobs){
var len = jobs.length
, job
, el;
// enough jobs to show "more" button
len >= more
? o('#more').show()
: o('#more').hide();
// remove jobs which have changed their state
o('#jobs .job').each(function(i, el){
var el = $(el)
, id = (el.attr('id') || '').replace('job-', '')
, found = jobs.some(function(job){
return job && id == job.id;
});
if (!found) el.remove();
});
for (var i = 0; i < len; ++i) {
if (!jobs[i]) continue;
// exists
if (o('#job-' + jobs[i].id).length) {
el = o('#job-' + jobs[i].id);
job = el.get(0).job;
job.update(jobs[i])
.showProgress('active' == active)
.showErrorMessage('failed' == active)
.render();
// new
} else {
job = new Job(jobs[i]);
el = job.showProgress('active' == active)
.showErrorMessage('failed' == active)
.render(true);
el.get(0).job = job;
el.appendTo('#jobs');
}
}
fn();
});
}
/**
* Poll for stats every `ms`.
*
* @param {Number} ms
*/
function pollStats(ms) {
request('/stats', function(data){
o('li.inactive .count').text(data.inactiveCount);
o('li.active .count').text(data.activeCount);
o('li.complete .count').text(data.completeCount);
o('li.failed .count').text(data.failedCount);
setTimeout(function(){
pollStats(ms);
}, ms);
});
}
/**
* Request `url` and invoke `fn(res)`.
*
* @param {String} url
* @param {Function} fn
*/
function request(url, fn) {
var method = 'GET';
if ('string' == typeof fn) {
method = url;
url = fn;
fn = arguments[2];
}
fn = fn || function(){};
o.ajax({ type: method, url: url })
.success(function(res){
res.error
? error(res.error)
: fn(res);
});
}
/**
* Display error `msg`.
*
* @param {String} msg
*/
function error(msg) {
o('#error').text(msg).addClass('show');
setTimeout(function(){
o('#error').removeClass('show');
}, 4000);
}