Merge lp:~cprov/launchpad/ic-ui-stab-one into lp:launchpad

Proposed by Celso Providelo
Status: Merged
Merged at revision: 16977
Proposed branch: lp:~cprov/launchpad/ic-ui-stab-one
Merge into: lp:launchpad
Diff against target: 511 lines (+161/-166)
4 files modified
lib/lp/code/javascript/branchmergeproposal.inlinecomments.js (+99/-27)
lib/lp/code/javascript/branchmergeproposal.updater.js (+4/-53)
lib/lp/code/javascript/tests/test_branchmergeproposal.inlinecomments.js (+54/-9)
lib/lp/code/javascript/tests/test_branchmergeproposal.updater.js (+4/-77)
To merge this branch: bzr merge lp:~cprov/launchpad/ic-ui-stab-one
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+213542@code.launchpad.net

Description of the change

Moving 'diff-content update' code from Updater() to the new DiffNav() widget (which works with and without inline-comments). The Updater() widget continues to handle diff-stats updates because it's not relevant for the inline-comments context.
Also rendering the spinner during DiffNav() actions, so users are not left uninformed.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) wrote :

Some of the new code is indented with tabs rather than spaces, but otherwise this looks good.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/javascript/branchmergeproposal.inlinecomments.js'
2--- lib/lp/code/javascript/branchmergeproposal.inlinecomments.js 2014-02-28 01:19:39 +0000
3+++ lib/lp/code/javascript/branchmergeproposal.inlinecomments.js 2014-03-31 23:30:31 +0000
4@@ -249,46 +249,107 @@
5 }
6 },
7
8- _connectNavigator: function(navigator) {
9- var self = this;
10+ /*
11+ * Add the spinner image to the diff section title.
12+ *
13+ * @method set_status_updating
14+ */
15+ set_status_updating: function() {
16+ var image = Y.Node.create('<img />')
17+ .set('src', '/@@/spinner')
18+ .set('title', 'Updating diff ...');
19+ this.get('srcNode').one('h2').append(image);
20+ },
21+
22+ /**
23+ * Run finishing tasks after the diff content is updated.
24+ *
25+ * @method updated
26+ */
27+ set_status_updated: function() {
28 var rc = Y.lp.code.branchmergeproposal.reviewcomment;
29 (new rc.NumberToggle()).render();
30- namespace.setup_inline_comments(navigator.get('value'));
31-
32+ var navigator = this.get('srcNode').one('select');
33+ if (navigator) {
34+ namespace.setup_inline_comments(navigator.get('value'));
35+ }
36+ },
37+
38+ /**
39+ * Remove the spinner image from the diff section title.
40+ *
41+ * @method cleanup_status
42+ */
43+ cleanup_status: function() {
44+ this.get('srcNode').all('h2 img').remove();
45+ },
46+
47+ /*
48+ * Helper method to update diff-content area according to given
49+ * diff content uri
50+ *
51+ * @method _updateDiff
52+ */
53+ _updateDiff: function(preview_diff_uri) {
54+ var self = this;
55+ var container = self.get('srcNode').one('.diff-content');
56+ var config = {
57+ on: {
58+ success: function (diff_html) {
59+ container.set('innerHTML', diff_html);
60+ self.set_status_updated();
61+ },
62+ failure: function(ignore, response, args) {
63+ Y.log('DiffNav: ' + preview_diff_uri +
64+ ' - ' + response.statusText);
65+ var error_note = Y.Node.create('<p />')
66+ .addClass('exception')
67+ .addClass('lowlight')
68+ .set('text', 'Failed to fetch diff content.');
69+ container.empty();
70+ container.append(error_note);
71+ },
72+ start: function() {
73+ self.set_status_updating();
74+ },
75+ end: function() {
76+ self.cleanup_status();
77+ }
78+ }
79+ };
80+ this.get('lp_client').get(preview_diff_uri, config);
81+ },
82+
83+ /*
84+ * Helper method to connect changes on the diff navigator (<select>)
85+ * to the diff-content updater.
86+ *
87+ * @method _connectNavigator
88+ */
89+ _connectNavigator: function(navigator) {
90+ var self = this;
91 navigator.on('change', function() {
92 var previewdiff_id = this.get('value');
93 var preview_diff_uri = (
94 LP.cache.context.web_link +
95 '/+preview-diff/' + previewdiff_id + '/+diff');
96- var container = self.get('srcNode').one('.diff-content');
97- var config = {
98- on: {
99- success: function (diff_html) {
100- container.set('innerHTML', diff_html);
101- (new rc.NumberToggle()).render();
102- namespace.setup_inline_comments(previewdiff_id);
103- },
104- failure: function(ignore, response, args) {
105- Y.log('DiffNav: ' + preview_diff_uri +
106- ' - ' + response.statusText);
107- var error_note = Y.Node.create('<p />')
108- .addClass('exception')
109- .addClass('lowlight')
110- .set('text', 'Failed to fetch diff content.');
111- container.empty();
112- container.append(error_note);
113- },
114- },
115- };
116- self.get('lp_client').get(preview_diff_uri, config);
117+ self._updateDiff(preview_diff_uri);
118 });
119 },
120
121+ /*
122+ * Render diff navigator contents (navigator and diff area).
123+ *
124+ * @method renderUI
125+ */
126 renderUI: function() {
127+ var self = this;
128 if (LP.cache.inline_diff_comments !== true) {
129- return
130+ var preview_diff_uri = (
131+ LP.cache.context.web_link + '/++diff');
132+ self._updateDiff(preview_diff_uri);
133+ return;
134 }
135- var self = this;
136 var container = self.get('srcNode').one('.diff-navigator');
137 container.empty();
138 var preview_diffs_uri = LP.cache.context.preview_diffs_collection_link;
139@@ -317,6 +378,11 @@
140 });
141 container.append(navigator)
142 self._connectNavigator(navigator);
143+ var previewdiff_id = navigator.get('value');
144+ var preview_diff_uri = (
145+ LP.cache.context.web_link +
146+ '/+preview-diff/' + previewdiff_id + '/+diff');
147+ self._updateDiff(preview_diff_uri);
148 },
149 failure: function(ignore, response, args) {
150 Y.log('DiffNav: ' + preview_diffs_uri +
151@@ -328,6 +394,12 @@
152 container.empty();
153 container.append(error_note);
154 },
155+ start: function() {
156+ self.set_status_updating();
157+ },
158+ end: function() {
159+ self.cleanup_status();
160+ }
161 }
162 };
163 this.get('lp_client').get(preview_diffs_uri, config);
164
165=== modified file 'lib/lp/code/javascript/branchmergeproposal.updater.js'
166--- lib/lp/code/javascript/branchmergeproposal.updater.js 2014-02-27 19:26:31 +0000
167+++ lib/lp/code/javascript/branchmergeproposal.updater.js 2014-03-31 23:30:31 +0000
168@@ -77,28 +77,6 @@
169 'summary_node').one('#summary-row-b-diff').one('td');
170 container.set('innerHTML', value);
171 }
172- },
173-
174- /**
175- * The HTML code for the diff.
176- *
177- * @attribute diff
178- */
179- diff: {
180- getter: function() {
181- if (this.get('pending')) {
182- return '';
183- }
184- else {
185- return this.get(
186- 'srcNode').one('.diff-content').get('innerHTML');
187- }
188- },
189- setter: function(value) {
190- this._setup_diff_container();
191- this.get(
192- 'srcNode').one('.diff-content').set('innerHTML', value);
193- }
194 }
195 }
196
197@@ -214,7 +192,7 @@
198 .append(Y.Node.create('<h2 />')
199 .set("text", "Preview Diff "))
200 .append(Y.Node.create('<div />')
201- .addClass("diff-content"))
202+ .addClass("diff-navigator"))
203 .append(Y.Node.create('<div />')
204 .addClass("diff-content"));
205 this.get('srcNode').append(review_diff);
206@@ -222,14 +200,12 @@
207 },
208
209 /*
210- * Update the page with the last version of the diff and update the
211- * stats.
212+ * Update the page diff stats.
213 *
214 * @method update
215 */
216 update: function() {
217 this.update_stats();
218- this.update_diff();
219 },
220
221 /*
222@@ -248,35 +224,10 @@
223 '#proposal-summary a.diff-link', '#review-diff');
224 var node = self.get('summary_node');
225 Y.lp.anim.green_flash({node: node}).run();
226- },
227- failure: function() {
228- var node = self.get('summary_node');
229- Y.lp.anim.red_flash({node: node}).run();
230- }
231- }
232- };
233- var mp_uri = LP.cache.context.web_link;
234- this.get('lp_client').get(mp_uri + "/++diff-stats", config);
235- },
236-
237-
238- /*
239- * Update the diff content with the last version.
240- *
241- * @method update_diff
242- */
243- update_diff: function() {
244- var self = this;
245- var config = {
246- on: {
247- success: function(diff) {
248- self.set('diff', diff);
249- var node = self.get('srcNode').one('.diff-content');
250- Y.lp.anim.green_flash({node: node}).run();
251 self.fire(self.NAME + '.updated');
252 },
253 failure: function() {
254- var node = self.get('srcNode').one('.diff-content');
255+ var node = self.get('summary_node');
256 Y.lp.anim.red_flash({node: node}).run();
257 },
258 start: function() {
259@@ -288,7 +239,7 @@
260 }
261 };
262 var mp_uri = LP.cache.context.web_link;
263- this.get('lp_client').get(mp_uri + "/++diff", config);
264+ this.get('lp_client').get(mp_uri + "/++diff-stats", config);
265 }
266
267 });
268
269=== modified file 'lib/lp/code/javascript/tests/test_branchmergeproposal.inlinecomments.js'
270--- lib/lp/code/javascript/tests/test_branchmergeproposal.inlinecomments.js 2014-02-28 03:32:53 +0000
271+++ lib/lp/code/javascript/tests/test_branchmergeproposal.inlinecomments.js 2014-03-31 23:30:31 +0000
272@@ -8,20 +8,15 @@
273 tests.suite = new Y.Test.Suite('branchmergeproposal.inlinecomments Tests');
274
275 tests.suite.add(new Y.Test.Case({
276- name: 'code.branchmergeproposal.inlinecomments_tests',
277+ name: 'code.branchmergeproposal.inlinecomments_comments_tests',
278
279 setUp: function () {
280 // Loads testing values into LP.cache.
281 LP.cache.context = {
282- web_link: "https://launchpad.dev/~foo/bar/foobr/+merge/1",
283 self_link: "https://code.launchpad.dev/api/devel/~foo/bar/foobr/+merge/1",
284- preview_diff_link: "https://code.launchpad.dev/api/devel/~foo/bar/foobr/+merge/1/preview_diff",
285- preview_diffs_collection_link: "https://code.launchpad.dev/api/devel/~foo/bar/foobr/+merge/1/preview_diffs"
286 };
287
288 LP.cache.inline_diff_comments = true;
289- module.current_previewdiff_id = 1;
290- module.inlinecomments = {};
291 },
292
293 tearDown: function () {},
294@@ -47,6 +42,7 @@
295 {io_provider: mockio});
296
297 // Populate the page.
298+ module.current_previewdiff_id = 1;
299 module.populate_existing_comments();
300
301 // LP was hit twice for fetching published and draft inline
302@@ -176,8 +172,27 @@
303
304 Y.Assert.isTrue(called);
305 Y.Assert.areEqual(1, module.current_previewdiff_id);
306+ }
307+
308+ }));
309+
310+ tests.suite.add(new Y.Test.Case({
311+ name: 'code.branchmergeproposal.inlinecomments_diffnav_tests',
312+
313+ setUp: function () {
314+ // Loads testing values into LP.cache.
315+ LP.cache.context = {
316+ web_link: "https://launchpad.dev/~foo/bar/foobr/+merge/1",
317+ self_link: "https://code.launchpad.dev/api/devel/~foo/bar/foobr/+merge/1",
318+ preview_diff_link: "https://code.launchpad.dev/api/devel/~foo/bar/foobr/+merge/1/preview_diff",
319+ preview_diffs_collection_link: "https://code.launchpad.dev/api/devel/~foo/bar/foobr/+merge/1/preview_diffs"
320+ };
321+
322+ LP.cache.inline_diff_comments = true;
323 },
324
325+ tearDown: function () {},
326+
327 test_diff_nav_feature_flag_disabled: function() {
328 // Disable feature flag.
329 LP.cache.inline_diff_comments = false;
330@@ -194,8 +209,15 @@
331 (new module.DiffNav(
332 {srcNode: Y.one('#review-diff'), lp_client: lp_client}
333 )).render();
334- // Nothing actually happens.
335- Y.Assert.areEqual(0, mockio.requests.length);
336+ // The lastest diff is retrieved.
337+ Y.Assert.areEqual(1, mockio.requests.length);
338+ Y.Assert.areSame(
339+ "/~foo/bar/foobr/+merge/1/++diff",
340+ mockio.last_request.url);
341+ mockio.success({
342+ responseText: Y.one('.diff-content').get('innerHTML'),
343+ responseHeaders: {'Content-Type': 'text/html'}});
344+ // But no inline_comments are set up.
345 Y.Assert.isNull(called_diff_id);
346 },
347
348@@ -235,6 +257,14 @@
349 mockio.success({
350 responseText: Y.JSON.stringify(preview_diffs),
351 responseHeaders: {'Content-Type': 'application/json'}});
352+ // The selected preview_diff content is retrieved.
353+ Y.Assert.areEqual(2, mockio.requests.length);
354+ Y.Assert.areSame(
355+ "/~foo/bar/foobr/+merge/1/+preview-diff/101/+diff",
356+ mockio.last_request.url);
357+ mockio.success({
358+ responseText: Y.one('.diff-content').get('innerHTML'),
359+ responseHeaders: {'Content-Type': 'text/html'}});
360 // The local (fake) setup_inline_comments function
361 // was called with the selected diff_id value.
362 Y.Assert.areEqual(101, called_diff_id);
363@@ -252,7 +282,7 @@
364 Y.one('select').set('value', 202);
365 Y.one('select').simulate('change');
366 // diff content was updated with the selected diff_id content.
367- Y.Assert.areEqual(2, mockio.requests.length);
368+ Y.Assert.areEqual(3, mockio.requests.length);
369 Y.Assert.areSame(
370 "/~foo/bar/foobr/+merge/1/+preview-diff/202/+diff",
371 mockio.last_request.url);
372@@ -306,6 +336,21 @@
373 Y.Assert.areEqual(
374 'Failed to fetch available diffs.',
375 Y.one('.diff-navigator').get('text'));
376+ },
377+
378+ test_status_indicator: function() {
379+ var diff_nav = new module.DiffNav(
380+ {srcNode: Y.one('#review-diff')});
381+ // Indicating progress with the spinner image.
382+ diff_nav.set_status_updating();
383+ Y.Assert.areEqual(
384+ '/@@/spinner',
385+ Y.one('h2').one('img').getAttribute('src'));
386+ // Remove the spinner when the work is done.
387+ diff_nav.cleanup_status();
388+ Y.Assert.areEqual(
389+ 'Preview Diff',
390+ Y.one('h2').get('innerHTML'));
391 }
392
393 }));
394
395=== modified file 'lib/lp/code/javascript/tests/test_branchmergeproposal.updater.js'
396--- lib/lp/code/javascript/tests/test_branchmergeproposal.updater.js 2012-10-26 10:00:20 +0000
397+++ lib/lp/code/javascript/tests/test_branchmergeproposal.updater.js 2014-03-31 23:30:31 +0000
398@@ -43,9 +43,7 @@
399
400 test_default_values: function() {
401 Y.Assert.isTrue(this.updater.get('pending'));
402- Y.Assert.areEqual(
403- '',
404- this.updater.get('diff'));
405+ Y.Assert.isNull(this.updater.get('diff_stats'));
406 },
407
408 test__setup_diff_container: function() {
409@@ -104,38 +102,6 @@
410 Y.one('h2').get('innerHTML'));
411 },
412
413- test_get_diff: function() {
414- this.updater._setup_diff_container();
415- Y.one('.diff-content').set(
416- 'innerHTML', 'this is a <span>diff</span>');
417- Y.Assert.areEqual(
418- 'this is a <span>diff</span>',
419- this.updater.get('diff'));
420- },
421-
422- test_set_diff: function() {
423- this.updater.set('diff', 'this is a <span>diff</span>');
424- Y.Assert.areEqual(
425- 'this is a <span>diff</span>',
426- Y.one('.diff-content').get('innerHTML'));
427- },
428-
429- test_update_diff_success: function() {
430- var mockio = new Y.lp.testing.mockio.MockIo();
431- this.updater.get('lp_client').io_provider = mockio;
432- Y.Assert.areEqual(
433- '',
434- this.updater.get('diff'));
435- this.updater.update_diff();
436- mockio.success({
437- responseText: 'New <span>diff</span>',
438- responseHeaders: {'Content-Type': 'text/html'}});
439-
440- Y.Assert.areEqual(
441- 'New <span>diff</span>',
442- this.updater.get('diff'));
443- },
444-
445 test_update_stats_success: function() {
446 var mockio = new Y.lp.testing.mockio.MockIo();
447 this.updater.get('lp_client').io_provider = mockio;
448@@ -150,61 +116,22 @@
449 this.updater.get('diff_stats'));
450 },
451
452- test_update_diff_fires_event: function() {
453+ test_update_fires_event: function() {
454 var fired = false;
455 var mockio = new Y.lp.testing.mockio.MockIo();
456 this.updater.get('lp_client').io_provider = mockio;
457 this.updater.on(this.updater.NAME + '.updated', function() {
458 fired = true;
459 });
460- this.updater.update_diff();
461+ this.updater.update();
462 mockio.success({
463- responseText: 'New <span>diff</span>',
464+ responseText: '13 lines (+4/-0) 1 file modified',
465 responseHeaders: {'Content-Type': 'text/html'}});
466-
467 Y.Assert.isTrue(fired);
468 }
469
470 }));
471
472-/*
473- * Tests for when the updater is built on top of an existing diff.
474- *
475- */
476-var current_mp = Y.one('#current-mp').getContent();
477-
478-tests.suite.add(new Y.Test.Case({
479-
480- name: 'branchmergeproposal-updater-refresh-tests',
481-
482- setUp: function() {
483- Y.one("#placeholder")
484- .empty()
485- .append(Y.Node.create(current_mp));
486- var diff_area = Y.one('#diff-area');
487- this.updater = new UpdaterWidget({srcNode: diff_area});
488- },
489-
490- tearDown: function() {
491- this.updater.destroy();
492- },
493-
494- test_default_values: function() {
495- Y.Assert.isFalse(this.updater.get('pending'));
496- Y.Assert.areEqual(
497- 'Example diff',
498- this.updater.get('diff'));
499- },
500-
501- test_get_diff: function() {
502- Y.one('.diff-content').set(
503- 'innerHTML', 'this is a <span>diff</span>');
504- Y.Assert.areEqual(
505- 'this is a <span>diff</span>',
506- this.updater.get('diff'));
507- }
508-
509-}));
510
511 tests.suite.add(new Y.Test.Case({
512