Parent: [392f71] (diff)

Child: [0146ef] (diff)

Download this file

commit_browser.js    218 lines (206 with data), 6.3 kB

  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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// define indexOf for IE (code from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf)
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(searchElement /*, fromIndex */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (len === 0)
return -1;
var n = 0;
if (arguments.length > 0)
{
n = Number(arguments[1]);
if (n !== n) // shortcut for verifying if it's NaN
n = 0;
else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
if (n >= len)
return -1;
var k = n >= 0
? n
: Math.max(len - Math.abs(n), 0);
for (; k < len; k++)
{
if (k in t && t[k] === searchElement)
return k;
}
return -1;
};
}
if($('#commit_graph')){
$.getJSON(document.location.href+'_data', function(data) {
var tree = data['built_tree'];
var max_row = data['max_row'];
var next_column = data['next_column'];
// graph size settings
var x_space = 10;
var y_space = 20;
var point_offset = 5;
var point_size = 10;
// graph set up
var height = (max_row+1)*y_space;
var canvas = document.getElementById('commit_graph');
var ctx = canvas.getContext('2d');
canvas.height=height;
var taken_coords = {};
var commit_rows = [];
var max_x_pos = x_space*next_column;
ctx.fillStyle = "rgb(0,0,0)";
ctx.lineWidth = 1;
ctx.lineJoin = 'round';
ctx.textBaseline = "top";
ctx.font = "12px sans-serif";
// highlighter set up
var highlighter = document.getElementById('commit_highlighter');
var highlighter_ctx = highlighter.getContext('2d');
highlighter.height=height;
highlighter_ctx.fillStyle = "#ccc";
var active_ys = [0,0]
$(canvas).click(function(evt){
var y = Math.floor((evt.pageY-$(canvas).offset().top) / y_space);
var commit = commit_rows[y-1];
highlighter_ctx.clearRect(0,active_ys[0],750,active_ys[1]);
active_ys = [commit.y_pos-y_space/4,y_space]
highlighter_ctx.fillRect(0, active_ys[0], 750, active_ys[1]);
$.get(commit.url+'basic',function(result){
$('#commit_view').html(result);
});
});
// helper functions for laying out lines on the graph
var detect_collision = function(x,y1,y2){
if(taken_coords[x]){
for(var i=0,len=taken_coords[x].length;i<len;i++){
if(taken_coords[x][i] >= y1 && taken_coords[x][i] <= y2){
return true;
}
}
return false;
}
else{
return false;
}
};
var adjust_x = function(x, y1, y2){
if(detect_collision(x, y1, y2)){
return adjust_x(x+x_space, y1, y2)
}
else{
if(x > max_x_pos){
max_x_pos = x;
}
return x
}
}
var take_line_coords = function(x,y1,y2){
var taken_ys = taken_coords[x];
if(!taken_ys){
taken_ys = taken_coords[x] = [];
}
for(var i=y1;i<=y2;i=i+y_space){
if(taken_ys.indexOf(i) == -1){
taken_ys.push(i);
}
}
}
// map out where commit points will be
for(var c in tree){
var commit = tree[c];
var x_pos = x_space+(commit.column*x_space);
var y_pos = y_space+(commit.row*y_space);
if (!taken_coords[x_pos]){
taken_coords[x_pos] = [y_pos]
}
else if(taken_coords[x_pos].indexOf(y_pos) == -1){
taken_coords[x_pos].push(y_pos);
}
}
// draw lines
for(var c in tree){
var commit = tree[c];
var x_pos = x_space+(commit.column*x_space);
var y_pos = y_space+(commit.row*y_space);
for(var i=0,len=commit.parents.length;i<len;i++){
var parent = commit.parents[i];
ctx.beginPath();
ctx.moveTo(x_pos+point_offset, y_pos+point_offset);
var parent_x = x_space+tree[parent].column*x_space;
var parent_y = y_space+(tree[parent].row*y_space);
var series = commit.series;
if(tree[parent].column == commit.column){
ctx.lineTo(parent_x+point_offset, parent_y+point_offset);
take_line_coords(x_pos,y_pos,parent_y);
}
else if(tree[parent].column > commit.column){
var y1 = y_pos+point_offset*2;
var original_x = parent_x;
var adjusted_x = adjust_x(parent_x,y1,parent_y-1);
ctx.lineTo(adjusted_x+point_offset,y1);
ctx.lineTo(adjusted_x+point_offset,parent_y);
if(original_x != adjusted_x){
ctx.lineTo(adjusted_x+point_offset,parent_y);
ctx.lineTo(original_x+point_offset,parent_y+point_offset);
}
else{
ctx.lineTo(adjusted_x+point_offset,parent_y);
}
take_line_coords(adjusted_x,y_pos,parent_y);
series = tree[parent].series;
}
else{
var y1 = y_space+(tree[parent].row*y_space);
var original_x = x_pos;
var adjusted_x = adjust_x(x_pos,y_pos+point_offset*3,parent_y-point_offset);
if(original_x != adjusted_x){
ctx.lineTo(adjusted_x+point_offset, y_pos+point_offset*2);
}
ctx.lineTo(adjusted_x+point_offset, y1);
ctx.lineTo(parent_x+point_offset, parent_y+point_offset);
take_line_coords(adjusted_x,y_pos,parent_y);
if(i > 0){
series = tree[parent].series;
}
}
switch(series % 6){
case 0:
ctx.strokeStyle = "#a00";
break;
case 1:
ctx.strokeStyle = "#0a0";
break;
case 2:
ctx.strokeStyle = "#00a";
break;
case 3:
ctx.strokeStyle = "#aa0";
break;
case 4:
ctx.strokeStyle = "#0aa";
break;
default:
ctx.strokeStyle = "#f0f";
}
ctx.stroke();
}
}
// draw commit points and message text
ctx.fillStyle = "rgb(0,0,0)";
for(var c in tree){
var commit = tree[c];
var x_pos = x_space+(commit.column*x_space);
var y_pos = y_space+(commit.row*y_space);
ctx.fillRect(x_pos, y_pos, point_size, point_size);
for(var i=x_pos;i<=max_x_pos;i=i+x_space){
if(taken_coords[i].indexOf(y_pos) != -1){
x_pos = i + x_space*2;
}
}
ctx.fillText(commit.message, x_pos, y_pos);
commit_rows[commit.row]={'url':commit.url,'y_pos':y_pos};
}
});
}