-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcleandiff.js
123 lines (111 loc) · 2.84 KB
/
cleandiff.js
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
var defaultClickExpansionSize = 10;
function getChildren(node, skipMe){
var r = [];
for ( ; node; node = node.nextSibling ) {
if ( node.nodeType == 1 && node != skipMe){
r.push( node );
}
}
return r;
};
function getChildrenUpTo(node, stopAt){
var r = [];
for ( ; node; node = node.nextSibling ) {
if ( node.nodeType == 1 && node != stopAt) {
if (node.classList[0] == "line") {
// ^^ only to compensate for bug in bash version's source html
r.push( node );
}
} else if ( node == stopAt) {
break;
}
}
return r;
}
function getLaterNSiblings(node, n){
var r = [];
for ( ; node; node = node.nextSibling ) {
if ( r.length < n && node.nodeType == 1) {
// nodeType 3 is Text 1 is element.
r.push( node )
} else if (r.length == n) {
break;
}
}
return r;
}
function getSiblings(n) {
return getChildren(n.parentNode.firstChild, n);
}
function getEarlierSiblings(n){
return getChildrenUpTo(n.parentNode.firstChild, n);
}
function getPreviousNSiblings(node, count){
return getEarlierSiblings(node).slice((count * -1));
}
function expandPreviousSiblings(node, how_many){
var predecessors = getPreviousNSiblings(node, how_many);
for (var x = predecessors.length -1; x > -1 ; x--){
expandNodeIfUnchanged(predecessors[x]);
}
}
function expandLaterSiblings(node, how_many){
var antecessors = getLaterNSiblings(node, how_many);
for (var x = 0; x < antecessors.length; x++){
expandNodeIfUnchanged( antecessors[x])
}
}
function expandNodeIfUnchanged(node){
if (typeof(node) != "undefined" && node.nodeType == 1){
if (node.className.indexOf("unchanged") > -1) {
node.className = node.className + " expanded";
}
}
}
function getAllAnchors(){
return document.getElementsByClassName("linenum_link");
}
function getChangedLineDivs(){
var all_lines = document.getElementsByClassName("line");
var changed_lines = [];
for (var x = 0; x < all_lines.length; x++){
var node = all_lines[x];
if (node.className.indexOf("unchanged") == -1) {
changed_lines.push(node);
}
}
return changed_lines;
}
function addOnClickToAnchors(number_anchors){
for (var x = 0; x < number_anchors.length; x++){
var anchor = number_anchors[x]
anchor.addEventListener("click",
function(){expandPreviousSiblings(this.parentNode.parentNode,
defaultClickExpansionSize)}, false);
}
}
function expandAroundChangedLines(){
var changed_lines = getChangedLineDivs();
for (var x = 0; x < changed_lines.length; x++){
var node = changed_lines[x]
expandPreviousSiblings(node, 5);
expandLaterSiblings(node, 5);
}
}
function activateAnchors(){
var number_anchors = getAllAnchors();
addOnClickToAnchors(number_anchors);
expandAroundChangedLines()
}
Array.prototype.first = function(){
if (this.length > 1){
return this[0];
}
return null;
}
Array.prototype.rest = function(){
if (this.length > 1){
return this.slice(1)
}
return new Array();
}