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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
/* Copyright 2011 Canonical Ltd. This software is licensed under the
* GNU Affero General Public License version 3 (see the file LICENSE).
*
* A form overlay that can request builds for a recipe..
*
* @namespace Y.lp.code.recipebuild_overlay
* @requires dom, node, io-base, lp.anim, lazr.formoverlay
*/
YUI.add('lp.code.requestbuild_overlay', function(Y) {
var namespace = Y.namespace('lp.code.requestbuild_overlay');
var lp_client;
var request_build_overlay = null;
var request_build_submit_button;
var request_build_response_handler;
var request_daily_build_response_handler;
function set_up_lp_client() {
if (lp_client === undefined) {
lp_client = new Y.lp.client.Launchpad();
}
}
// This handler is used to process the results of form submission or other
// such operation (eg get, post). It provides some boiler plate and allows the
// developer to specify onSuccess and onError hooks. It is quite generic and
// perhaps could be moved to an infrastructure class.
RequestResponseHandler = function () {};
RequestResponseHandler.prototype = {
clearProgressUI: function () {},
showError: function (header, error_msg) {},
getErrorHandler: function (errorCallback) {
var self = this;
return function (id, response) {
self.clearProgressUI();
// If it was a timeout...
if (response.status == 503) {
self.showError(
'Timeout error, please try again in a few minutes.',
null);
} else {
if (errorCallback != null) {
errorCallback(self, id, response);
} else {
self.showError(response.responseText, null);
}
}
};
},
getSuccessHandler: function (successCallback) {
var self = this;
return function (id, response) {
self.clearProgressUI();
successCallback(self, id, response);
};
}
};
namespace.RequestResponseHandler = RequestResponseHandler;
namespace.connect_requestbuilds = function() {
var request_build_handle = Y.one('#request-builds');
request_build_handle.addClass('js-action');
request_build_handle.on('click', function(e) {
e.preventDefault();
if (request_build_overlay == null) {
// Render the form and load the widgets to display
var recipe_name = LP.cache.context['name'];
request_build_overlay = new Y.lazr.FormOverlay({
headerContent: '<h2>Request builds for '
+ recipe_name + ' </h2>',
form_submit_button: Y.Node.create(
'<button type="submit" name="field.actions.request" ' +
'value="Request builds">Request Builds</button>'),
form_cancel_button: Y.Node.create(
'<button type="button" name="field.actions.cancel" ' +
'>Cancel</button>'),
centered: true,
form_submit_callback: do_request_builds,
visible: false
});
Y.after(function() {
disable_existing_builds(request_build_submit_button);
}, request_build_overlay, "bindUI");
request_build_overlay.render();
request_build_submit_button =
Y.Node.one("[name='field.actions.request']");
}
request_build_submit_button.removeAttribute("disabled");
request_build_overlay.clearError();
var loading_spinner = [
'<div id="temp-spinner">',
'<img src="/@@/spinner"/>Loading...',
'</div>'].join('');
request_build_overlay.form_node.set("innerHTML", loading_spinner);
request_build_overlay.loadFormContentAndRender('+builds/++form++');
request_build_submit_button.show();
request_build_overlay.show();
});
// Wire up the processing hooks
request_build_response_handler = new RequestResponseHandler();
request_build_response_handler.clearProgressUI = function() {
destroy_temporary_spinner();
};
request_build_response_handler.showError = function(header, error_msgs) {
if (header == null) {
if (error_msgs == null)
header = "An error occurred, please contact an " +
"administrator.";
else
header = "The following errors occurred:";
}
var error_html = header;
if (error_msgs != null) {
error_html += "<ul>";
if (typeof(error_msgs) == "string"){
error_msgs = [error_msgs];
}
Y.each(error_msgs, function(error_msg){
error_html += "<li>" + error_msg.replace(/<([^>]+)>/g,'') +
"</li>";
});
error_html += "</ul><p/>";
}
request_build_overlay.error_node.set('innerHTML', error_html);
};
};
var NO_BUILDS_MESSAGE = "All requested recipe builds are already queued.";
var ONE_BUILD_MESSAGE = "1 new recipe build has been queued.";
var MANY_BUILDS_MESSAGE = "{nr_new} new recipe builds have been queued.";
namespace.connect_requestdailybuild = function(config) {
var request_daily_build_handle = Y.one('#request-daily-build');
var display_message = function (message, css_class){
var build_message_node = Y.Node.create('<div></div>')
.set('id', 'new-builds-info')
.addClass(css_class)
.set('text', message)
.append(Y.Node.create('<br />'))
.append(Y.Node.create('<a></a>')
.set('href', '#')
.addClass('discreet')
.addClass('js-action')
.set('text', 'Dismiss'));
build_message_node.one('a').on('click', function(e) {
e.preventDefault();
build_message_node.hide();
if (css_class === 'build-error') {
request_daily_build_handle.removeClass("unseen");
}
});
request_daily_build_handle.insert(
build_message_node,
request_daily_build_handle);
};
request_daily_build_handle.on('click', function(e) {
e.preventDefault();
create_temporary_spinner(
"Requesting build...", request_daily_build_handle);
request_daily_build_handle.addClass("unseen");
var base_url = LP.cache.context.web_link;
var submit_url = base_url+"/+request-daily-build";
var current_builds = harvest_current_build_records();
var qs = Y.lp.client.append_qs(
'', 'field.actions.build', 'Build now');
var y_config = {
method: "POST",
headers: {'Accept': 'application/xhtml'},
on: {
failure: request_daily_build_response_handler.getErrorHandler(
function(handler, id, response) {
var server_error = 'Server error, ' +
'please contact an administrator.';
handler.showError(server_error, null);
}),
success:
request_daily_build_response_handler.getSuccessHandler(
function(handler, id, response) {
var nr_new = display_build_records(
response.responseText, current_builds);
var new_builds_message;
switch (nr_new) {
case 0:
new_builds_message = NO_BUILDS_MESSAGE;
break;
case 1:
new_builds_message = ONE_BUILD_MESSAGE;
break;
default:
new_builds_message =
Y.Lang.substitute(
MANY_BUILDS_MESSAGE,
{nr_new: nr_new});
}
display_message(
new_builds_message, 'build-informational');
}
)
},
data: qs
};
var io = Y.io;
if ( config !== undefined && Y.Lang.isValue(config.io)) {
io = config.io;
}
io(submit_url, y_config);
});
// Wire up the processing hooks
request_daily_build_response_handler = new RequestResponseHandler();
request_daily_build_response_handler.clearProgressUI = function() {
destroy_temporary_spinner();
};
request_daily_build_response_handler.showError = function(header, error) {
var error_msg = header;
if (error != null)
error_msg += " " + error;
display_message(error_msg, 'build-error');
Y.log(error_msg);
};
};
/*
* A function to return the current build records as displayed on the page
*/
function harvest_current_build_records() {
var row_classes = ['package-build', 'binary-build'];
var builds = new Array();
Y.Array.each(row_classes, function(row_class) {
Y.all('.'+row_class).each(function(row) {
var row_id = row.getAttribute('id');
if (builds.indexOf(row_id)<0) {
builds.push(row_id);
}
});
});
return builds;
}
/*
* Render build records and flash the new ones
*/
function display_build_records(build_records_markup, current_builds) {
var target = Y.one('#builds-target');
target.set('innerHTML', build_records_markup);
var new_builds = harvest_current_build_records();
var nr_new_builds = 0;
Y.Array.each(new_builds, function(row_id) {
if( current_builds.indexOf(row_id)>=0 )
return;
nr_new_builds += 1;
var row = Y.one('#'+row_id);
var anim = Y.lp.anim.green_flash({node: row});
anim.run();
});
return nr_new_builds;
}
/*
* Perform any client side validation
* Return: true if data is valid
*/
function validate(data) {
var distros = data['field.distroseries']
if (Y.Object.size(distros) == 0) {
request_build_response_handler.showError(
"You need to specify at least one distro series for " +
"which to build.", null);
return false;
}
return true;
}
/*
* The form submit function
*/
function do_request_builds(data) {
if (!validate(data))
return;
request_build_submit_button.setAttribute("disabled", "disabled");
var spinner_location = Y.one('.yui3-lazr-formoverlay-actions');
create_temporary_spinner("Requesting builds...", spinner_location);
var base_url = LP.cache.context.web_link;
var submit_url = base_url+"/+builds";
var current_builds = harvest_current_build_records();
var y_config = {
method: "POST",
headers: {'Accept': 'application/json; application/xhtml'},
on: {
failure: request_build_response_handler.getErrorHandler(
function(handler, id, response) {
if( response.status >= 500 ) {
// There's some error content we need to display.
request_build_overlay.set(
'form_content', response.responseText);
request_build_overlay.get("form_submit_button")
.addClass('unseen');
request_build_overlay.renderUI();
//We want to force the form to be re-created
request_build_overlay = null;
return;
}
// The response will be a json data object containing info
// about what builds there are, informational text about
// any builds already pending, plus any errors. The
// FormOverlay infrastructure only supports displaying
// errors so we will construct our own HTML where the
// informational text will be appropriately displayed.
var build_info = Y.JSON.parse(response.responseText);
// The result of rendering the +builds view
var build_html = build_info['builds'];
// Any builds already pending (informational only)
var pending_build_info = build_info['already_pending'];
// Other more critical errors
var error_info = build_info['errors'];
var error_header_text = "";
if (build_html != null) {
var nr_new = display_build_records(
build_html, current_builds);
var new_builds_message = ONE_BUILD_MESSAGE;
if (nr_new > 1) {
new_builds_message =
Y.Lang.substitute(
MANY_BUILDS_MESSAGE,
{nr_new: nr_new});
}
error_header_text = new_builds_message;
}
if (pending_build_info != null) {
if (error_header_text != "" )
error_header_text += "<p/>" + pending_build_info;
else
error_header_text = pending_build_info;
}
var errors = [];
for (var field_name in error_info)
errors.push(error_info[field_name]);
var error_header;
if (error_header_text != "") {
error_header =
"<p><div class='popup-build-informational'>"
+error_header_text+"</p></div>";
if (Y.Object.size(errors)>0) {
error_header +=
"<p/>" + "There were also some errors:";
}
} else {
error_header = "There were some errors:"
}
handler.showError(error_header, errors);
}),
success: request_build_response_handler.getSuccessHandler(
function(handler, id, response) {
request_build_overlay.hide();
display_build_records(
response.responseText, current_builds)
})
},
form: {
id: request_build_overlay.form_node,
useDisabled: true
}
};
Y.io(submit_url, y_config);
}
/*
* Show the temporary "Requesting..." text
*/
function create_temporary_spinner(text, node) {
// Add the temp "Requesting build..." text
var temp_spinner = Y.Node.create([
'<div id="temp-spinner">',
'<img src="/@@/spinner"/>',
text,
'</div>'].join(''));
node.insert(temp_spinner, node);
}
/*
* Destroy the temporary "Requesting..." text
*/
function destroy_temporary_spinner() {
var temp_spinner = Y.one('#temp-spinner');
var spinner_parent = temp_spinner.get('parentNode');
spinner_parent.removeChild(temp_spinner);
}
//****************************************************************************
// This section contains code to manage the disabling of distro series
// selection on the request build form where there are already pending builds.
//****************************************************************************
var last_known_pending_builds;
// We store the original html for the distro series checkboxes so we can
// restore it when needed.
var distroseries_node_html;
function get_distroseries_nodes() {
return request_build_overlay.form_node.all(
"label[for^='field.distroseries.']");
}
var DISABLED_DISTROSERIES_CHECKBOX_HTML =
"<input type='checkbox' class='checkboxType' disabled='disabled'>" +
" {distro} (build pending)";
/*
* Callback used to enable/disable distro series selection when a different
* ppa is selected.
*/
function ppa_changed(ppa_value, submit_button) {
// Reset the disro series checkboxs to their default html.
var distroseries_nodes = get_distroseries_nodes();
for (var i=0; i<distroseries_nodes.size(); i++) {
var distroseries_node = distroseries_nodes.item(i);
distroseries_node.set("innerHTML", distroseries_node_html[i]);
distroseries_node.removeClass("lowlight");
}
var nr_matches = 0;
Y.Array.each(last_known_pending_builds, function(distroarchive) {
if (ppa_value != distroarchive[1])
return;
for (var i=0; i<distroseries_nodes.size(); i++) {
var distroseries_node = distroseries_nodes.item(i);
var distro = distroseries_node.get("text").trim();
if (distro == distroarchive[0]) {
nr_matches += 1;
var escaped_distro = Y.Escape.html(distro);
var disabled_checkbox_html = Y.Lang.substitute(
DISABLED_DISTROSERIES_CHECKBOX_HTML,
{distro: escaped_distro});
distroseries_node.set("innerHTML", disabled_checkbox_html);
distroseries_node.addClass("lowlight");
break;
}
}
});
// If no distro series can be selected, no need to show the submit btn.
if (nr_matches>0 && nr_matches == distroseries_nodes.size())
submit_button.hide();
else
submit_button.show();
}
function disable_existing_builds(submit_button) {
// Lookup a the exiting builds and parse the info into a data structure so
// so that we can attempt to prevent the user from requesting builds
// which are already pending. It's not foolproof since a build may finish
// or be initiated after this initial lookup, but we do handle that
// situation in the error handling.
// The ui may not be ready yet.
var ppa_selector = request_build_overlay.form_node.one(
"[name='field.archive']");
if (ppa_selector == null) {
return;
}
distroseries_node_html = new Array();
last_known_pending_builds = new Array();
var y_config = {
headers: {'Accept': 'application/json;'},
on: {
success:
function(build_info) {
// We save the inner html of each distro series checkbox
// so we can restore it when required.
var distro_nodes = get_distroseries_nodes();
distro_nodes.each(function(distro_node) {
distroseries_node_html.push(
distro_node.get("innerHTML"));
});
// We have a collection of the pending build info.
// The info is the distroseries displayname and archive
// token.
var size = build_info.length;
for( var i=0; i<size; i++ ) {
var build_record = build_info[i];
var distro_name = build_record["distroseries"];
var archive_token = build_record["archive"];
last_known_pending_builds.push(
[distro_name, archive_token]);
}
ppa_selector.on("change", function(e) {
ppa_changed(ppa_selector.get("value"), submit_button);
});
ppa_changed(ppa_selector.get("value"), submit_button);
}
}
};
set_up_lp_client();
lp_client.named_get(
LP.cache.context.self_link, 'getPendingBuildInfo', y_config);
}
}, "0.1", {"requires": [
"dom", "node", "escape", "io-base", "lp.anim", "lazr.formoverlay",
"lp.client"
]});
|