~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
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
/* Copyright 2011 Canonical Ltd.  This software is licensed under the
 * GNU Affero General Public License version 3 (see the file LICENSE).
 *
 * Code for updating the diff when a new version is available.
 *
 * @module lp.code.branchmergeproposal.updater
 * @requires node, lp.client
 */

YUI.add('lp.code.branchmergeproposal.updater', function(Y) {

var namespace = Y.namespace('lp.code.branchmergeproposal.updater');

function UpdaterWidget(config) {
    UpdaterWidget.superclass.constructor.apply(this, arguments);
}

Y.mix(UpdaterWidget, {

    NAME: 'updaterWidget',

    ATTRS: {

        /**
         * The LP client to use. If none is provided, one will be
         * created during initialization.
         *
         * @attribute lp_client
         */
        lp_client: {
            value: null
        },

        /**
         * The summary node.
         *
         * @attribute summary_node
         */
        summary_node: {
            value: null,
            writeOnce: "initOnly"
        },

        /**
         * Whether or not this MP is still 'pending'.
         *
         * @attribute pending
         * @readOnly
         */
        pending: {
            readOnly: true,
            getter: function() {
                return !Y.Lang.isValue(
                    this.get('srcNode').one('.diff-content'));
            }
        },

        /**
         * The HTML code for the stats diff.
         *
         * @attribute diff_stats
         */
        diff_stats: {
            getter: function() {
                var summary_node = this.get('summary_node');
                if (!Y.Lang.isValue(summary_node) ||
                    !Y.Lang.isValue(summary_node.one(
                         '#summary-row-b-diff'))) {
                    return null;
                }
                return summary_node.one(
                    '#summary-row-b-diff').one('td').get('innerHTML');
            },
            setter: function(value) {
               this._setup_diff_stats_container();
               var container = this.get(
                   'summary_node').one('#summary-row-b-diff').one('td');
               container.set('innerHTML', value);
            }
        },

        /**
         * The HTML code for the diff.
         *
         * @attribute diff
         */
        diff: {
            getter: function() {
                if (this.get('pending')) {
                    return '';
                }
                else {
                    return this.get(
                        'srcNode').one('.diff-content').get('innerHTML');
                }
            },
            setter: function(value) {
               this._setup_diff_container();
               this.get(
                   'srcNode').one('.diff-content').set('innerHTML', value);
            }
        }
    }

});

Y.extend(UpdaterWidget, Y.Widget, {

    /*
     * The initializer method that is called from the base Plugin class.
     *
     * @method initializer
     * @protected
     */
    initializer: function(cfg){
        // If we have not been provided with a Launchpad Client, then
        // create one now:
        if (null === this.get("lp_client")){
            // Create our own instance of the LP client.
            this.set("lp_client", new Y.lp.client.Launchpad());
        }
        this.set('summary_node', cfg.summary_node);
    },

    /*
     * Set the proper icon to indicate the diff is updating.
     *
     * @method set_status_updating
     */
    set_status_updating: function() {
       this.cleanup_status();
       this._set_status('spinner', 'Update in progress.');
    },

    /*
     * Set the proper icon to indicate the diff will be updated when the
     * new version is available.
     *
     * @method set_status_longpolling
     */
    set_status_longpolling: function() {
       this.cleanup_status();
       this._set_status(
           'longpoll_loading',
           'The diff will be updated as soon as a new version is available.');
    },

    /*
     * Set the proper icon to indicate that the diff update is broken.
     *
     * @method set_status_longpollerror
     */
    set_status_longpollerror: function() {
       this.cleanup_status();
       this._set_status(
           'longpoll_error',
           'Diff update error, please reload to see the changes.');
    },

    /*
     * Add a status image to the diff title.
     *
     * @method _set_status
     */
    _set_status: function(image_name, title) {
       var image = Y.Node.create('<img />')
           .set('src', '/@@/' + image_name)
           .set('title', title);
       this.get('srcNode').one('h2').append(image);
    },

    /*
     * Remove the status image to the diff title.
     *
     * @method cleanup_status
     */
    cleanup_status: function() {
        this._setup_diff_container();
        this.get('srcNode').all('h2 img').remove();
    },

    /*
     * Add a row in the page summary table to display the diff stats
     * if needed.
     *
     * @method _setup_diff_stats_container
     */
     _setup_diff_stats_container: function() {
        if (!Y.Lang.isValue(this.get('diff_stats'))) {
            var summary_node = this.get('summary_node');
            var diff_stats = Y.Node.create('<tr />')
                .set('id', 'summary-row-b-diff')
                .append(Y.Node.create('<th />')
                    .set("text", "Diff against target:"))
                .append(Y.Node.create('<td />'));
            summary_node.one(
                '#summary-row-9-target-branch').insert(diff_stats, 'after');
        }
     },

    /*
     * Populate the widget with the required nodes to display the diff
     * if needed.
     *
     * @method _setup_diff_container
     */
    _setup_diff_container: function() {
        if (this.get('pending')) {
            // Cleanup.get('srcNode').
            this.get('srcNode').empty();
            // Create the diff container.
            var review_diff = Y.Node.create('<div />')
                .set('id', 'review-diff')
                .append(Y.Node.create('<h2 />')
                    .set("text", "Preview Diff "))
                .append(Y.Node.create('<div />')
                    .addClass("diff-content"));
            this.get('srcNode').append(review_diff);
        }
    },

    /*
     * Update the page with the last version of the diff and update the
     * stats.
     *
     * @method update
     */
    update: function() {
        this.update_stats();
        this.update_diff();
    },

    /*
     * Update the diff stats with the last version.
     *
     * @method update_stats
     */
    update_stats: function() {
        var self = this;
        var config = {
            on: {
                success: function(diff_stats) {
                    self.set('diff_stats', diff_stats);
                    // (re)connect the js scroller link.
                    Y.lp.code.branchmergeproposal.reviewcomment.link_scroller(
                        '#proposal-summary a.diff-link', '#review-diff');
                    var node = self.get('summary_node');
                    Y.lp.anim.green_flash({node: node}).run();
                },
                failure: function() {
                    var node = self.get('summary_node');
                    Y.lp.anim.red_flash({node: node}).run();
                }
            }
        };
        var mp_uri = LP.cache.context.web_link;
        this.get('lp_client').get(mp_uri + "/++diff-stats", config);
     },


    /*
     * Update the diff content with the last version.
     *
     * @method update_diff
     */
    update_diff: function() {
        var self = this;
        var config = {
            on: {
                success: function(diff) {
                    self.set('diff', diff);
                    var node = self.get('srcNode').one('.diff-content');
                    Y.lp.anim.green_flash({node: node}).run();
                    self.fire(self.NAME + '.updated');
                },
                failure: function() {
                    var node = self.get('srcNode').one('.diff-content');
                    Y.lp.anim.red_flash({node: node}).run();
                },
                start: function() {
                    self.set_status_updating();
                },
                end: function() {
                    self.cleanup_status();
                }
            }
        };
        var mp_uri = LP.cache.context.web_link;
        this.get('lp_client').get(mp_uri + "/++diff", config);
    }

});

/*
 * Export UpdaterWidget.
 */
namespace.UpdaterWidget = UpdaterWidget;

/*
 * Returns true if the event fired means that the preview_diff field of the
 * MP has been updated.
 *
 */
namespace.is_mp_diff_updated = function(event_data) {
    return (event_data.what === "modified" &&
        event_data.edited_fields.indexOf("preview_diff") >= 0);
};

}, '0.1', {requires: ['node', 'lp.client', 'lp.anim',
                      'lp.code.branchmergeproposal.reviewcomment']});