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
|
/* 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
},
/**
* 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 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(){
// 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());
}
var self = this;
},
/*
* 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();
},
/*
* 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 diff content with the last version.
*
* @method update
*/
update: 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']});
|