~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/javascript/branchmergeproposal.updater.js

  • Committer: Julian Edwards
  • Date: 2011-07-28 20:46:18 UTC
  • mfrom: (13553 devel)
  • mto: This revision was merged to the branch mainline in revision 13555.
  • Revision ID: julian.edwards@canonical.com-20110728204618-tivj2wx2oa9s32bx
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright 2011 Canonical Ltd.  This software is licensed under the
2
 
 * GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 *
4
 
 * Code for updating the diff when a new version is available.
5
 
 *
6
 
 * @module lp.code.branchmergeproposal.updater
7
 
 * @requires node, lp.client
8
 
 */
9
 
 
10
 
YUI.add('lp.code.branchmergeproposal.updater', function(Y) {
11
 
 
12
 
var namespace = Y.namespace('lp.code.branchmergeproposal.updater');
13
 
 
14
 
function UpdaterWidget(config) {
15
 
    UpdaterWidget.superclass.constructor.apply(this, arguments);
16
 
}
17
 
 
18
 
Y.mix(UpdaterWidget, {
19
 
 
20
 
    NAME: 'updaterWidget',
21
 
 
22
 
    ATTRS: {
23
 
 
24
 
        /**
25
 
         * The LP client to use. If none is provided, one will be
26
 
         * created during initialization.
27
 
         *
28
 
         * @attribute lp_client
29
 
         */
30
 
        lp_client: {
31
 
            value: null
32
 
        },
33
 
 
34
 
        /**
35
 
         * Whether or not this MP is still 'pending'.
36
 
         *
37
 
         * @attribute pending
38
 
         * @readOnly
39
 
         */
40
 
        pending: {
41
 
            readOnly: true,
42
 
            getter: function() {
43
 
                return !Y.Lang.isValue(
44
 
                    this.get('srcNode').one('.diff-content'));
45
 
            }
46
 
        },
47
 
 
48
 
        /**
49
 
         * The HTML code for the diff.
50
 
         *
51
 
         * @attribute diff
52
 
         */
53
 
        diff: {
54
 
            getter: function() {
55
 
                if (this.get('pending')) {
56
 
                    return '';
57
 
                }
58
 
                else {
59
 
                    return this.get(
60
 
                        'srcNode').one('.diff-content').get('innerHTML');
61
 
                }
62
 
            },
63
 
            setter: function(value) {
64
 
               this._setup_diff_container();
65
 
               this.get(
66
 
                   'srcNode').one('.diff-content').set('innerHTML', value);
67
 
            }
68
 
        }
69
 
    }
70
 
 
71
 
});
72
 
 
73
 
Y.extend(UpdaterWidget, Y.Widget, {
74
 
 
75
 
    /*
76
 
     * The initializer method that is called from the base Plugin class.
77
 
     *
78
 
     * @method initializer
79
 
     * @protected
80
 
     */
81
 
    initializer: function(){
82
 
        // If we have not been provided with a Launchpad Client, then
83
 
        // create one now:
84
 
        if (null === this.get("lp_client")){
85
 
            // Create our own instance of the LP client.
86
 
            this.set("lp_client", new Y.lp.client.Launchpad());
87
 
        }
88
 
        var self = this;
89
 
    },
90
 
 
91
 
    /*
92
 
     * Set the proper icon to indicate the diff is updating.
93
 
     *
94
 
     * @method set_status_updating
95
 
     */
96
 
    set_status_updating: function() {
97
 
       this.cleanup_status();
98
 
       this._set_status('spinner', 'Update in progress.');
99
 
    },
100
 
 
101
 
    /*
102
 
     * Set the proper icon to indicate the diff will be updated when the
103
 
     * new version is available.
104
 
     *
105
 
     * @method set_status_longpolling
106
 
     */
107
 
    set_status_longpolling: function() {
108
 
       this.cleanup_status();
109
 
       this._set_status(
110
 
           'longpoll_loading',
111
 
           'The diff will be updated as soon as a new version is available.');
112
 
    },
113
 
 
114
 
    /*
115
 
     * Set the proper icon to indicate that the diff update is broken.
116
 
     *
117
 
     * @method set_status_longpollerror
118
 
     */
119
 
    set_status_longpollerror: function() {
120
 
       this.cleanup_status();
121
 
       this._set_status(
122
 
           'longpoll_error',
123
 
           'Diff update error, please reload to see the changes.');
124
 
    },
125
 
 
126
 
    /*
127
 
     * Add a status image to the diff title.
128
 
     *
129
 
     * @method _set_status
130
 
     */
131
 
    _set_status: function(image_name, title) {
132
 
       var image = Y.Node.create('<img />')
133
 
           .set('src', '/@@/' + image_name)
134
 
           .set('title', title);
135
 
       this.get('srcNode').one('h2').append(image);
136
 
    },
137
 
 
138
 
    /*
139
 
     * Remove the status image to the diff title.
140
 
     *
141
 
     * @method cleanup_status
142
 
     */
143
 
    cleanup_status: function() {
144
 
        this._setup_diff_container();
145
 
        this.get('srcNode').all('h2 img').remove();
146
 
    },
147
 
 
148
 
    /*
149
 
     * Populate the widget with the required nodes to display the diff
150
 
     * if needed.
151
 
     *
152
 
     * @method _setup_diff_container
153
 
     */
154
 
    _setup_diff_container: function() {
155
 
        if (this.get('pending')) {
156
 
            // Cleanup.get('srcNode').
157
 
            this.get('srcNode').empty();
158
 
            // Create the diff container.
159
 
            var review_diff = Y.Node.create('<div />')
160
 
                .set('id', 'review-diff')
161
 
                .append(Y.Node.create('<h2 />')
162
 
                    .set("text", "Preview Diff "))
163
 
                .append(Y.Node.create('<div />')
164
 
                    .addClass("diff-content"));
165
 
            this.get('srcNode').append(review_diff);
166
 
        }
167
 
    },
168
 
 
169
 
    /*
170
 
     * Update the diff content with the last version.
171
 
     *
172
 
     * @method update
173
 
     */
174
 
    update: function() {
175
 
        var self = this;
176
 
        var config = {
177
 
            on: {
178
 
                success: function(diff) {
179
 
                    self.set('diff', diff);
180
 
                    var node = self.get('srcNode').one('.diff-content');
181
 
                    Y.lp.anim.green_flash({node: node}).run();
182
 
                    self.fire(self.NAME + '.updated');
183
 
                },
184
 
                failure: function() {
185
 
                    var node = self.get('srcNode').one('.diff-content');
186
 
                    Y.lp.anim.red_flash({node: node}).run();
187
 
                },
188
 
                start: function() {
189
 
                    self.set_status_updating();
190
 
                },
191
 
                end: function() {
192
 
                    self.cleanup_status();
193
 
                }
194
 
            }
195
 
        };
196
 
        var mp_uri = LP.cache.context.web_link;
197
 
        this.get('lp_client').get(mp_uri + "/++diff", config);
198
 
    }
199
 
 
200
 
});
201
 
 
202
 
/*
203
 
 * Export UpdaterWidget.
204
 
 */
205
 
namespace.UpdaterWidget = UpdaterWidget;
206
 
 
207
 
/*
208
 
 * Returns true if the event fired means that the preview_diff field of the
209
 
 * MP has been updated.
210
 
 *
211
 
 */
212
 
namespace.is_mp_diff_updated = function(event_data) {
213
 
    return (event_data.what === "modified" &&
214
 
        event_data.edited_fields.indexOf("preview_diff") >= 0);
215
 
};
216
 
 
217
 
}, '0.1', {requires: ['node', 'lp.client', 'lp.anim']});