~launchpad-pqm/launchpad/devel

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
/* Copyright 2010 Canonical Ltd.  This software is licensed under the
 * GNU Affero General Public License version 3 (see the file LICENSE).
 *
 * Dynamically add milestones to an HTML table.
 *
 * @module Y.lp.registry.milestonetable
 * @requires node, io-base, substitute, lazr.anim
 */
YUI.add('lp.registry.milestonetable', function(Y) {
    Y.log('loading lp.registry.milestonetable');
    var module = Y.namespace('lp.registry.milestonetable');

    // get_milestone_row() needs these when it is called. Other methods
    // should get this information passed as an argument.
    module._milestone_row_uri_template = null;
    module._tbody = null;

    module._prepend_node = function(parent_node, child_node) {
        // Add the child_node to the parent_node as the first item.
        var children = parent_node.get('children');
        if (children === null) {
            parent_node.appendChild(child_node);
        } else {
            parent_node.insertBefore(child_node, children.item(0));
            }
        };

    module._ensure_table_is_seen = function(tbody) {
        // Remove the 'unseen' class from the table to ensure it is visible.
        table = tbody.ancestor();
        table.removeClass('unseen');
        };

    module._clear_add_handlers = function(data) {
        // Detach the callback and errorback functions from the Y.io events.
        // The data has been used. If they are not detached, there will be
        // multiple adds for each use of the Create milestone link.
        data.success_handle.detach();
        data.failure_handle.detach();
        };

    module._on_add_success = function(id, response, data) {
        // Add the milestone to the milestone table on Y.io success.
        var row = Y.Node.create(Y.Lang.trim(response.responseText));
        module._ensure_table_is_seen(data.tbody);
        module._prepend_node(data.tbody, row);
        Y.lazr.anim.green_flash({node: row}).run();
        module._clear_add_handlers(data);
        };

    module._on_add_failure = function(id, response, data) {
        // Add the failure message to the milestone table on Y.io failure.
        var row = Y.Node.create(Y.substitute(
            '<tr><td colspan="0">' +
            'Could not retrieve milestone {name}</td></tr>', data));
        module._ensure_table_is_seen(data.tbody);
        module._prepend_node(data.tbody, row);
        Y.lazr.anim.red_flash({node: row}).run();
        module._clear_add_handlers(data);
        };

    module._setup_milestone_event_data = function(parameters, tbody) {
        // Attach the callback to the Y.io event and return their data
        var data = {
            name: parameters.name,
            tbody: tbody
            };
        data.success_handle = Y.on(
            'io:success', module._on_add_success, this, data);
        data.failure_handle = Y.on(
            'io:failure', module._on_add_failure, this, data);
        return data;
        };

    /**
      * The milestoneoverlay next_step to add the milestone to the page.
      *
      * This is the callback passed as 'next_step' in milestoneoverlay config.
      *
      * @method get_milestone_row
      * @param {Object} parameters Object literal of config name/value pairs.
      *     The form parameters that were submitted to create the milestone.
      */
    module.get_milestone_row = function(parameters) {
        module._setup_milestone_event_data(parameters, module._tbody);
        var milestone_row_uri = Y.substitute(
            module._milestone_row_uri_template, parameters);
        Y.io(milestone_row_uri);
        };

    /**
      * Setup the URL to get the milestone and the table it will be added too.
      *
      * @method setup
      * @param {Object} parameters Object literal of config name/value pairs.
      *     config.milestone_row_uri_template is the Y.substitute template
      *         that is used to create the URL to get the milestone row.
      *     config.milestone_rows_id is the id the the tbody that the
      *         milestone row will be added too.
      */
    module.setup =  function(config) {
        if (config === undefined) {
            throw new Error(
                "Missing setup config for milestonetable.");
            }
        if (config.milestone_row_uri_template === undefined ||
            config.milestone_rows_id === undefined ) {
            throw new Error(
                "Undefined properties in setup config for milestonetable.");
            }
        module._milestone_row_uri_template = config.milestone_row_uri_template;
        module._tbody = Y.one(config.milestone_rows_id);
        if (module._tbody === null) {
            throw new Error(
                Y.substitute("'{milestone_rows_id}' not in page.", config));
            }
        };

}, "0.1", {"requires": [
    "node", "io-base", "substitute", "lazr.anim"
    ]});