1
/* Copyright 2011 Canonical Ltd. This software is licensed under the
2
* GNU Affero General Public License version 3 (see the file LICENSE).
4
* Expandable branch revisions.
6
* @module lp.code.branch.revisionexpander
7
* @requires node, lp.client.plugins
10
YUI.add('lp.code.branch.revisionexpander', function(Y) {
12
var namespace = Y.namespace('lp.code.branch.revisionexpander');
15
* Take a single revno, or a pair of revnos specifying a revision range, and
16
* construct a URL to get the diff of those revnos using
17
* LP.cache.branch_diff_link.
19
function bmp_get_diff_url(start_revno, end_revno) {
21
if (Y.Lang.isUndefined(end_revno)) {
22
/* No end_revno passed, so only after a single revision diff. */
23
return LP.cache.branch_diff_link + start_revno;
25
diff_url = LP.cache.branch_diff_link + end_revno;
26
if (start_revno !== 0) {
27
diff_url += '/' + start_revno;
28
} else if (start_revno === 0 && end_revno !== 1) {
34
function difftext_to_node(difftext) {
35
var node = Y.Node.create('<table class="diff"></table>');
36
var difflines = difftext.split('\n');
38
/* Remove the empty final row caused by a trailing newline
40
if (difflines.length > 0 && difflines[difflines.length-1] === '') {
44
for (i=0; i < difflines.length; i++) {
45
var line = difflines[i];
46
var line_node = Y.Node.create('<td/>');
47
line_node.set('text', line + '\n');
48
/* Colour the unified diff */
49
var header_pat = /^(===|---|\+\+\+) /;
50
var chunk_pat = /^@@ /;
51
if (line.match(header_pat)) {
52
line_node.addClass('diff-header');
53
} else if (line.match(chunk_pat)) {
54
line_node.addClass('diff-chunk');
58
line_node.addClass('diff-added');
61
line_node.addClass('diff-removed');
65
line_node.addClass('text');
66
var row = Y.Node.create('<tr></tr>');
67
row.appendChild(line_node);
68
node.appendChild(row);
73
function revision_expander_config(expander){
76
success: function nodify_result(diff) {
77
expander.receive(difftext_to_node(diff));
79
failure: function(trid, response, args) {
80
expander.receive(Y.Node.create('<pre><i>Error</i></pre>'));
86
function bmp_diff_loader(expander, lp_client) {
87
if (lp_client === undefined) {
88
lp_client = new Y.lp.client.Launchpad();
90
var rev_no_range = expander.icon_node.get(
91
'id').replace('expandable-', '').split('-');
92
var start_revno = rev_no_range[0]-1;
93
var end_revno = rev_no_range[1];
95
lp_client.get(bmp_get_diff_url(start_revno, end_revno),
96
revision_expander_config(expander));
99
namespace.bmp_diff_loader = bmp_diff_loader;
100
namespace.difftext_to_node = difftext_to_node;
101
namespace.bmp_get_diff_url = bmp_get_diff_url;
103
}, "0.1", {"requires": ["node", "lp.app.widgets.expander"]});