1
/* Copyright 2011 Canonical Ltd. This software is licensed under the
2
* GNU Affero General Public License version 3 (see the file LICENSE).
4
* Code for updating the diff when a new version is available.
6
* @module lp.code.branchmergeproposal.updater
7
* @requires node, lp.client
10
YUI.add('lp.code.branchmergeproposal.updater', function(Y) {
12
var namespace = Y.namespace('lp.code.branchmergeproposal.updater');
14
function UpdaterWidget(config) {
15
UpdaterWidget.superclass.constructor.apply(this, arguments);
18
Y.mix(UpdaterWidget, {
20
NAME: 'updaterWidget',
25
* The LP client to use. If none is provided, one will be
26
* created during initialization.
28
* @attribute lp_client
35
* Whether or not this MP is still 'pending'.
43
return !Y.Lang.isValue(
44
this.get('srcNode').one('.diff-content'));
49
* The HTML code for the diff.
55
if (this.get('pending')) {
60
'srcNode').one('.diff-content').get('innerHTML');
63
setter: function(value) {
64
this._setup_diff_container();
66
'srcNode').one('.diff-content').set('innerHTML', value);
73
Y.extend(UpdaterWidget, Y.Widget, {
76
* The initializer method that is called from the base Plugin class.
81
initializer: function(){
82
// If we have not been provided with a Launchpad Client, then
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());
92
* Set the proper icon to indicate the diff is updating.
94
* @method set_status_updating
96
set_status_updating: function() {
97
this.cleanup_status();
98
this._set_status('spinner', 'Update in progress.');
102
* Set the proper icon to indicate the diff will be updated when the
103
* new version is available.
105
* @method set_status_longpolling
107
set_status_longpolling: function() {
108
this.cleanup_status();
111
'The diff will be updated as soon as a new version is available.');
115
* Set the proper icon to indicate that the diff update is broken.
117
* @method set_status_longpollerror
119
set_status_longpollerror: function() {
120
this.cleanup_status();
123
'Diff update error, please reload to see the changes.');
127
* Add a status image to the diff title.
129
* @method _set_status
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);
139
* Remove the status image to the diff title.
141
* @method cleanup_status
143
cleanup_status: function() {
144
this._setup_diff_container();
145
this.get('srcNode').all('h2 img').remove();
149
* Populate the widget with the required nodes to display the diff
152
* @method _setup_diff_container
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);
170
* Update the diff content with the last version.
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');
184
failure: function() {
185
var node = self.get('srcNode').one('.diff-content');
186
Y.lp.anim.red_flash({node: node}).run();
189
self.set_status_updating();
192
self.cleanup_status();
196
var mp_uri = LP.cache.context.web_link;
197
this.get('lp_client').get(mp_uri + "/++diff", config);
203
* Export UpdaterWidget.
205
namespace.UpdaterWidget = UpdaterWidget;
208
* Returns true if the event fired means that the preview_diff field of the
209
* MP has been updated.
212
namespace.is_mp_diff_updated = function(event_data) {
213
return (event_data.what === "modified" &&
214
event_data.edited_fields.indexOf("preview_diff") >= 0);
217
}, '0.1', {requires: ['node', 'lp.client', 'lp.anim']});