root/cgi-bin/src/main/webapp/WEB-INF/htdocs/common/js/diff.js @ 3:d4aafdd7418e

Revision 3:d4aafdd7418e, 3.9 KB (checked in by dcaoyuan, 8 months ago)

Reconstructed to a maven project

Line 
1(function($){
2 
3  function convertDiff(name, table) {
4    var inline = table.className == 'inline';
5    var ths = table.tHead.rows[0].cells;
6    var afile, bfile;
7    if ( inline ) {
8        afile = ths[0].title;
9        bfile = ths[1].title;
10    } else {
11        afile = $(ths[0]).find('a').text();
12        bfile = $(ths[1]).find('a').text();
13    }
14    if ( afile.match(/^Revision /) ) {
15        afile = 'a/' + name;
16        bfile = 'b/' + name;
17    }
18    var lines = [
19      "Index: " + name,
20      "===================================================================",
21      "--- " + afile.replace(/File /, ''),
22      "+++ " + bfile.replace(/File /, ''),
23    ];
24    var sepIndex = 0;
25    var oldOffset = 0, oldLength = 0, newOffset = 0, newLength = 0;
26 
27    for (var i = 0; i < table.tBodies.length; i++) {
28      var tBody = table.tBodies[i];
29      if (i == 0 || tBody.className == "skipped") {
30        if (i > 0) {
31          if (!oldOffset && oldLength) oldOffset = 1
32          if (!newOffset && newLength) newOffset = 1
33          lines[sepIndex] = lines[sepIndex]
34            .replace("{1}", oldOffset).replace("{2}", oldLength)
35            .replace("{3}", newOffset).replace("{4}", newLength);
36        }
37        sepIndex = lines.length;
38        lines.push("@@ -{1},{2} +{3},{4} @@");
39        oldOffset = 0, oldLength = 0, newOffset = 0, newLength = 0;
40        if (tBody.className == "skipped") continue;
41      }
42      var tmpLines = [];
43      for (var j = 0; j < tBody.rows.length; j++) {
44        var cells = tBody.rows[j].cells;
45        var oldLineNo = parseInt($(cells[0]).text());
46        var newLineNo = parseInt($(cells[inline ? 1 : 2]).text());
47        if (tBody.className == 'unmod') {
48          lines.push(" " + $(cells[inline ? 2 : 1]).text());
49          oldLength += 1;
50          newLength += 1;
51          if (!oldOffset) oldOffset = oldLineNo;
52          if (!newOffset) newOffset = newLineNo;
53        } else {
54          var oldLine;
55          var newLine;
56          if (inline) {
57            oldLine = newLine = $(cells[2]).text();
58          } else {
59            oldLine = $(cells[1]).text();
60            newLine = $(cells[3]).text();
61          }
62          if (!isNaN(oldLineNo)) {
63            lines.push("-" + oldLine);
64            oldLength += 1;
65          }
66          if (!isNaN(newLineNo)) {
67            tmpLines.push("+" + newLine);
68            newLength += 1;
69          }
70        }
71      }
72      if (tmpLines.length > 0) {
73        lines = lines.concat(tmpLines);
74      }
75    }
76 
77    if (!oldOffset && oldLength) oldOffset = 1;
78    if (!newOffset && newLength) newOffset = 1;
79    lines[sepIndex] = lines[sepIndex]
80      .replace("{1}", oldOffset).replace("{2}", oldLength)
81      .replace("{3}", newOffset).replace("{4}", newLength);
82 
83    /* remove trailing &nbsp; and join lines (with CR for IExplorer) */
84    for ( var i = 0; i < lines.length; i++ )
85        if ( lines[i] )
86            lines[i] = lines[i].replace(/\xa0$/, '');
87    return lines.join($.browser.msie ? "\r" : "\n");
88  }
89 
90  $(document).ready(function($) {
91    $("div.diff h2").each(function() {
92      var switcher = $("<span class='switch'></span>").prependTo(this);
93      var name = $.trim($(this).text());
94      var table = $(this).siblings("table").get(0);
95      if (! table) return;
96      var pre = $("<pre></pre>").hide().insertAfter(table);
97      $("<span>Tabular</span>").click(function() {
98        $(pre).hide();
99        $(table).show();
100        $(this).addClass("active").siblings("span").removeClass("active");
101        return false;
102      }).addClass("active").appendTo(switcher);
103      $("<span>Unified</span>").click(function() {
104        $(table).hide();
105        if (!pre.get(0).firstChild) pre.text(convertDiff(name, table));
106        $(pre).fadeIn("fast")
107        $(this).addClass("active").siblings("span").removeClass("active");
108        return false;
109      }).appendTo(switcher);
110    });
111  });
112
113})(jQuery);
Note: See TracBrowser for help on using the browser.