~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/javascript/requestbuild_overlay.js

  • Committer: Henning Eggers
  • Date: 2011-08-26 14:51:02 UTC
  • mto: This revision was merged to the branch mainline in revision 13837.
  • Revision ID: henning@canonical.com-20110826145102-6rhnh6cbq8cnpkcn
Refactored request build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
    request_build_response_handler.clearProgressUI = function() {
97
97
        destroy_temporary_spinner();
98
98
    };
99
 
    request_build_response_handler.showError = function(header, error_msgs) {
100
 
        if (header === null) {
101
 
            if (error_msgs === null) {
102
 
                header = "An error occurred, please contact an " +
103
 
                         "administrator.";
104
 
            } else {
105
 
                header = "The following errors occurred:";
106
 
            }
107
 
        }
108
 
        var error_html = header;
109
 
        if (error_msgs !== null) {
110
 
            error_html += "<ul>";
111
 
            if (typeof(error_msgs) === "string"){
112
 
                error_msgs = [error_msgs];
113
 
            }
114
 
            Y.each(error_msgs, function(error_msg){
115
 
                error_html += "<li>" + error_msg.replace(/<([^>]+)>/g,'') +
116
 
                              "</li>";
117
 
            });
118
 
            error_html += "</ul><p/>";
119
 
        }
120
 
        request_build_overlay.error_node.set('innerHTML', error_html);
 
99
    request_build_response_handler.showError = function(errormessage) {
 
100
        display_errors(null, errormessage);
121
101
    };
122
102
};
123
103
 
267
247
    return true;
268
248
};
269
249
 
 
250
var get_new_builds_message = function(build_html, current_builds) {
 
251
    var nr_new;
 
252
    if (build_html === null) {
 
253
        return null;
 
254
    }
 
255
    nr_new = display_build_records(build_html, current_builds);
 
256
    if (nr_new > 1) {
 
257
        return Y.Lang.substitute(MANY_BUILDS_MESSAGE, {nr_new: nr_new});
 
258
    }
 
259
    return ONE_BUILD_MESSAGE;
 
260
};
 
261
 
 
262
var get_info_header = function(new_builds_message, pending_build_info) {
 
263
    var info_header = Y.Node.create('<div></div>')
 
264
        .addClass('popup-build-informational');
 
265
    if (new_builds_message !== null) {
 
266
        info_header.append(Y.Node.create('<p></p>')
 
267
                .set('text', new_builds_message));
 
268
    }
 
269
    if (pending_build_info !== null) {
 
270
        info_header.append(Y.Node.create('<p></p>')
 
271
                .set('text', pending_build_info));
 
272
    }
 
273
    if (info_header.getContent.hasChildNodes()) {
 
274
        return info_header;
 
275
    }
 
276
    return null;
 
277
};
 
278
 
270
279
/*
271
280
 * The form submit function
272
281
 */
285
294
        method: "POST",
286
295
        headers: {'Accept': 'application/json; application/xhtml'},
287
296
        on: {
288
 
            failure: request_build_response_handler.getErrorHandler(
289
 
                function(handler, id, response) {
290
 
                    var errors = [],
291
 
                        error_header = null,
292
 
                        error_header_text = "",
293
 
                        build_info, build_html, error_info,
294
 
                        nr_new, new_builds_message, field_name;
295
 
                    if( response.status >= 500 ) {
296
 
                        // There's some error content we need to display.
297
 
                        errors = [
298
 
                            response.status + ' ' + response.statusText];
299
 
                    } else {
300
 
                        // The response will be a json data object containing
301
 
                        // info about what builds there are, informational
302
 
                        // text about any builds already pending, plus any
303
 
                        // errors. The FormOverlay infrastructure only
304
 
                        // supports displaying errors so we will construct
305
 
                        // our own HTML where the informational text will be
306
 
                        // appropriately displayed.
307
 
                        build_info = Y.JSON.parse(response.responseText);
308
 
 
309
 
                        // The result of rendering the +builds view
310
 
                        build_html = build_info.builds;
311
 
                        // Any builds already pending (informational only)
312
 
                        pending_build_info = build_info.already_pending;
313
 
                        // Other more critical errors
314
 
                        error_info = build_info.errors;
315
 
 
316
 
                        if (build_html !== null) {
317
 
                            nr_new = display_build_records(
318
 
                                    build_html, current_builds);
319
 
                            new_builds_message = ONE_BUILD_MESSAGE;
320
 
                            if (nr_new > 1) {
321
 
                                new_builds_message =
322
 
                                        Y.Lang.substitute(
323
 
                                                MANY_BUILDS_MESSAGE,
324
 
                                                {nr_new: nr_new});
325
 
                            }
326
 
                            error_header_text = new_builds_message;
327
 
                        }
328
 
                        if (pending_build_info !== null) {
329
 
                            if (error_header_text !== "" ) {
330
 
                                error_header_text += "<p/>" +
331
 
                                pending_build_info;
332
 
                            } else {
333
 
                                error_header_text = pending_build_info;
334
 
                            }
335
 
                        }
336
 
                        for (field_name in error_info) {
337
 
                            if (error_info.hasOwnProperty(field_name)) {
338
 
                                errors.push(error_info[field_name]);
339
 
                            }
340
 
                        }
341
 
                        if (error_header_text !== "") {
342
 
                            error_header =
343
 
                                "<p><div class='popup-build-informational'>"
344
 
                                + error_header_text + "</p></div>";
345
 
                            if (Y.Object.size(errors)>0) {
346
 
                                error_header +=
347
 
                                    "<p/>" + "There were also some errors:";
348
 
                            }
349
 
                        } else {
350
 
                            error_header = "There were some errors:";
351
 
                        }
352
 
                    }
353
 
                    handler.showError(error_header, errors);
354
 
                }),
 
297
            failure: request_build_response_handler.getFailureHandler(),
355
298
            success: request_build_response_handler.getSuccessHandler(
356
299
                function(handler, id, response) {
 
300
                var errors = [],
 
301
                    error_header = null,
 
302
                    error_header_text = "",
 
303
                    build_info, build_html, error_info,
 
304
                    nr_new, info_header, field_name;
 
305
                content_type = response.getResponseHeader('Content-type');
 
306
                if( content_type !== 'application/json' ) {
 
307
                    // We got the HTML for the builds back, we're done.
357
308
                    request_build_overlay.hide();
358
309
                    display_build_records(
359
310
                            response.responseText, current_builds);
360
 
                })
 
311
                } else {
 
312
                    // The response will be a json data object containing
 
313
                    // info about what builds there are, informational
 
314
                    // text about any builds already pending, plus any
 
315
                    // errors. The FormOverlay infrastructure only
 
316
                    // supports displaying errors so we will construct
 
317
                    // our own HTML where the informational text will be
 
318
                    // appropriately displayed.
 
319
                    build_info = Y.JSON.parse(response.responseText);
 
320
 
 
321
                    // The result of rendering the +builds view
 
322
                    build_html = build_info.builds;
 
323
                    // Any builds already pending (informational only)
 
324
                    pending_build_info = build_info.already_pending;
 
325
                    // Other more critical errors
 
326
                    error_info = build_info.errors;
 
327
 
 
328
                    info_header = get_info_header(
 
329
                        get_new_builds_message(
 
330
                            build_html, current_builds),
 
331
                            pending_build_info);
 
332
 
 
333
                    for (field_name in error_info) {
 
334
                        if (error_info.hasOwnProperty(field_name)) {
 
335
                            errors.push(error_info[field_name]);
 
336
                        }
 
337
                    }
 
338
                    error_container = Y.Node.create('<div></div>')
 
339
                    if (info_header !== null) {
 
340
                        error_container.append(info_header);
 
341
                        if (errors.length > 0) {
 
342
                            error_container.append(
 
343
                                Y.Node.create('<p></p>')
 
344
                                    .set(
 
345
                                        'text',
 
346
                                        "There were also some errors:"));
 
347
                        }
 
348
                    } else {
 
349
                        error_container.set(
 
350
                            'text', "There were some errors:");
 
351
                    }
 
352
                    display_errors(error_container, errors);
 
353
                }
 
354
            })
361
355
        },
362
356
        form: {
363
357
            id: request_build_overlay.form_node,
405
399
            "label[for^='field.distroseries.']");
406
400
};
407
401
 
 
402
var display_errors = function(container, error_msgs) {
 
403
    var errors_list, header;
 
404
    if (container === null) {
 
405
        if (error_msgs === null) {
 
406
            header = "An error occurred, please contact an administrator.";
 
407
        } else {
 
408
            header = "The following errors occurred:";
 
409
        }
 
410
        container = Y.Node.create('<div></div>')
 
411
            .set('text', header);
 
412
    }
 
413
    if (error_msgs !== null) {
 
414
        errors_list = Y.Node.create('<ul></ul>');
 
415
        if (Y.Lang.isString(error_msgs)){
 
416
            error_msgs = [error_msgs];
 
417
        }
 
418
        Y.each(error_msgs, function(error_msg){
 
419
            errors_list.append(Y.Node.create('<li></li>')
 
420
                .set('text', error_msg));
 
421
        });
 
422
        container.append(errors_list);
 
423
    }
 
424
    request_build_overlay.error_node.setContent(container);
 
425
};
 
426
 
408
427
/*
409
428
 * Callback used to enable/disable distro series selection when a different
410
429
 * ppa is selected.