/
app.js
357 lines (300 loc) · 9.91 KB
/
app.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
var PHPCensor = {
intervals: {},
widgets: {},
init: function () {
$(document).ready(function () {
// Update latest builds every 5 seconds:
PHPCensor.getBuilds();
PHPCensor.intervals.getBuilds = setInterval(PHPCensor.getBuilds, 5000);
// Update latest project builds every 10 seconds:
if (typeof PROJECT_ID != 'undefined') {
PHPCensor.intervals.getProjectBuilds = setInterval(PHPCensor.getProjectBuilds, 10000);
}
});
$(window).on('builds-updated', function (e, data) {
PHPCensor.updateHeaderBuilds(data);
});
},
getBuilds: function () {
$.ajax({
url: APP_URL + 'build/ajax-queue',
success: function (data) {
$(window).trigger('builds-updated', [data]);
},
error: PHPCensor.handleFailedAjax
});
},
getProjectBuilds: function () {
$.ajax({
url: APP_URL + 'project/ajax-builds/' + PROJECT_ID + '?branch=' + PROJECT_BRANCH + '&environment=' + PROJECT_ENVIRONMENT + '&per_page=' + PER_PAGE + '&page=' + PAGE,
success: function (data) {
$('#latest-builds').html(data);
},
error: PHPCensor.handleFailedAjax
});
},
updateHeaderBuilds: function (data) {
$('.app-pending-list').empty();
$('.app-running-list').empty();
if (!data.pending.count) {
$('.app-pending').hide();
} else {
$('.app-pending').show();
$('.app-pending .header').text(Lang.get('n_builds_pending', data.pending.count));
$.each(data.pending.items, function (idx, build) {
$('.app-pending-list').append(build.header_row);
});
}
if (!data.running.count) {
$('.app-running').hide();
} else {
$('.app-running').show();
$('.app-running .header').text(Lang.get('n_builds_running', data.running.count));
$.each(data.running.items, function (idx, build) {
$('.app-running-list').append(build.header_row);
});
}
},
get: function (uri, success) {
$.ajax({
url: window.APP_URL + uri,
success: function (data) {
success();
},
error: PHPCensor.handleFailedAjax
});
},
handleFailedAjax: function (xhr) {
if (xhr.status == 401) {
window.location.href = window.APP_URL + 'session/login';
}
}
};
PHPCensor.init();
function handleFailedAjax(xhr)
{
PHPCensor.handleFailedAjax(xhr);
}
/**
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
* for the details of code below
*/
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {
},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
/**
* Used for delete buttons in the system, just to prevent accidental clicks.
*/
function confirmDelete(url, reloadAfter)
{
var dialog = new PHPCensorConfirmDialog({
title: Lang.get('confirm_title'),
message: Lang.get('confirm_message'),
confirmBtnCaption: Lang.get('confirm_ok'),
cancelBtnCaption: Lang.get('confirm_cancel'),
/*
confirm-btn click handler
*/
confirmed: function (e) {
var dialog = this;
e.preventDefault();
/*
Call delete URL
*/
$.ajax({
url: url,
success: function (data) {
if (reloadAfter) {
dialog.onClose = function () {
window.location.reload();
};
}
dialog.showStatusMessage(Lang.get('confirm_success'), 500);
},
error: function (data) {
dialog.showStatusMessage(Lang.get('confirm_failed') + data.statusText);
if (data.status == 401) {
handleFailedAjax(data);
}
}
});
}
});
dialog.show();
return dialog;
}
/**
* PHPCensorConfirmDialog constructor options object
* @type {{message: string, title: string, confirmBtnCaption: string, cancelBtnCaption: string, confirmed: Function}}
*/
var PHPCensorConfirmDialogOptions = {
message: 'Are you sure?',
title: 'Confirmation',
confirmBtnCaption: 'Ok',
cancelBtnCaption: 'Cancel',
confirmed: function (e) {
this.close();
}
};
var PHPCensorConfirmDialog = Class.extend({
/**
* @private
* @var {bool} Determines whether the dialog has been confirmed
*/
confirmed: false,
/**
* @param {PHPCensorConfirmDialogOptions} options
*/
init: function (options) {
options = options ? $.extend(PHPCensorConfirmDialogOptions, options) : PHPCensorConfirmDialogOptions;
if (!$('#confirm-dialog').length) {
/*
Add the dialog html to a page on first use. No need to have it there before first use.
*/
$('body').append(
'<div class="modal fade" id="confirm-dialog">'
+ '<div class="modal-dialog">'
+ '<div class="modal-content">'
+ '<div class="modal-header">'
+ '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>'
+ '<h4 class="modal-title"></h4>'
+ '</div>'
+ '<div class="modal-body">'
+ '<p></p>'
+ '</div>'
+ '<div class="modal-footer">'
+ '<button id="confirm-cancel" type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>'
+ '<button id="confirm-ok" type="button" class="btn btn-danger"></button>'
+ '</div>'
+ '</div>'
+ '</div>'
+ '</div>'
);
}
/*
Define dialog controls
*/
this.$dialog = $('#confirm-dialog');
this.$cancelBtn = this.$dialog.find('#confirm-cancel');
this.$confirmBtn = this.$dialog.find('#confirm-ok');
this.$title = this.$dialog.find('h4.modal-title');
this.$body = this.$dialog.find('div.modal-body');
/*
Initialize its values
*/
this.$title.html(options.title ? options.title : PHPCensorConfirmDialogOptions.title);
this.$body.html(options.message ? options.message : PHPCensorConfirmDialogOptions.message);
this.$confirmBtn.html(
options.confirmBtnCaption ? options.confirmBtnCaption : PHPCensorConfirmDialogOptions.confirmBtnCaption
);
this.$cancelBtn.html(
options.cancelBtnCaption ? options.cancelBtnCaption : PHPCensorConfirmDialogOptions.cancelBtnCaption
);
/*
Events
*/
this.confirmBtnClick = options.confirmed;
/*
Re-bind handlers
*/
this.$confirmBtn.unbind('click');
this.$confirmBtn.click(this.onConfirm.bind(this));
this.$confirmBtn.unbind('hidden.bs.modal');
/*
Bind the close event of the dialog to the set of onClose* methods
*/
this.$dialog.on('hidden.bs.modal', function () {
this.onClose()
}.bind(this));
this.$dialog.on('hidden.bs.modal', function () {
if (this.confirmed) {
this.onCloseConfirmed();
} else {
this.onCloseCanceled();
}
}.bind(this));
/*
Restore state if was changed previously
*/
this.$cancelBtn.show();
this.$confirmBtn.show();
this.confirmed = false;
},
/**
* Show dialog
*/
show: function () {
this.$dialog.modal('show');
},
/**
* Hide dialog
*/
close: function () {
this.$dialog.modal('hide');
},
onConfirm: function (e) {
this.confirmed = true;
$(this).attr('disabled', 'disabled');
this.confirmBtnClick(e);
},
/**
* Called only when confirmed dialog was closed
*/
onCloseConfirmed: function () {
},
/**
* Called only when canceled dialog was closed
*/
onCloseCanceled: function () {
},
/**
* Called always when the dialog was closed
*/
onClose: function () {
},
showStatusMessage: function (message, closeTimeout) {
this.$confirmBtn.hide();
this.$cancelBtn.hide();
/*
Status message
*/
this.$body.html(message);
if (closeTimeout) {
window.setTimeout(function () {
/*
Hide the dialog
*/
this.close();
}.bind(this), closeTimeout);
}
}
});
var Lang = {
get: function () {
var args = Array.prototype.slice.call(arguments);
var string = args.shift();
if (STRINGS[string]) {
args.unshift(STRINGS[string]);
return sprintf.apply(sprintf[0], args);
}
return string;
}
};