~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/javascript/branch.revisionexpander.js

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-06-25 08:55:37 UTC
  • mfrom: (13287.1.8 bug-800652)
  • Revision ID: launchpad@pqm.canonical.com-20110625085537-moikyoo2pe98zs7r
[r=jcsackett, julian-edwards][bug=800634,
        800652] Enable and display overrides on sync package uploads.

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
 
 * Expandable branch revisions.
5
 
 *
6
 
 * @module lp.code.branch.revisionexpander
7
 
 * @requires node, lp.client.plugins
8
 
 */
9
 
 
10
 
YUI.add('lp.code.branch.revisionexpander', function(Y) {
11
 
 
12
 
var namespace = Y.namespace('lp.code.branch.revisionexpander');
13
 
 
14
 
/*
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.
18
 
 */
19
 
function bmp_get_diff_url(start_revno, end_revno) {
20
 
    var diff_url;
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;
24
 
    }
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) {
29
 
       diff_url += '/null:';
30
 
    }
31
 
    return diff_url;
32
 
}
33
 
 
34
 
function difftext_to_node(difftext) {
35
 
    var node = Y.Node.create('<table class="diff"></table>');
36
 
    var difflines = difftext.split('\n');
37
 
    var i;
38
 
    /* Remove the empty final row caused by a trailing newline
39
 
     * (if it is empty) */
40
 
    if (difflines.length > 0 && difflines[difflines.length-1] === '') {
41
 
        difflines.pop();
42
 
    }
43
 
 
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');
55
 
        } else {
56
 
            switch (line[0]) {
57
 
                case '+':
58
 
                    line_node.addClass('diff-added');
59
 
                    break;
60
 
                case '-':
61
 
                    line_node.addClass('diff-removed');
62
 
                    break;
63
 
            }
64
 
        }
65
 
        line_node.addClass('text');
66
 
        var row = Y.Node.create('<tr></tr>');
67
 
        row.appendChild(line_node);
68
 
        node.appendChild(row);
69
 
    }
70
 
    return node;
71
 
}
72
 
 
73
 
function revision_expander_config(expander){
74
 
   return {
75
 
        on: {
76
 
            success: function nodify_result(diff) {
77
 
                expander.receive(difftext_to_node(diff));
78
 
            },
79
 
            failure: function(trid, response, args) {
80
 
                expander.receive(Y.Node.create('<pre><i>Error</i></pre>'));
81
 
            }
82
 
        }
83
 
   };
84
 
}
85
 
 
86
 
function bmp_diff_loader(expander, lp_client) {
87
 
    if (lp_client === undefined) {
88
 
        lp_client = new Y.lp.client.Launchpad();
89
 
    }
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];
94
 
 
95
 
    lp_client.get(bmp_get_diff_url(start_revno, end_revno),
96
 
        revision_expander_config(expander));
97
 
}
98
 
 
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;
102
 
 
103
 
}, "0.1", {"requires": ["node", "lp.app.widgets.expander"]});