2012-12-05 00:57:21 +01:00
|
|
|
!function(){
|
2011-11-13 12:58:45 +01:00
|
|
|
|
2012-12-07 22:05:17 +01:00
|
|
|
var BranchGraph = function(element, options){
|
2012-12-07 19:06:46 +01:00
|
|
|
this.element = element;
|
2012-12-07 22:05:17 +01:00
|
|
|
this.options = options;
|
2012-12-05 00:57:21 +01:00
|
|
|
|
2012-12-07 22:05:17 +01:00
|
|
|
this.preparedCommits = {};
|
2012-12-05 00:57:21 +01:00
|
|
|
this.mtime = 0;
|
|
|
|
this.mspace = 0;
|
|
|
|
this.parents = {};
|
|
|
|
this.colors = ["#000"];
|
|
|
|
|
2012-12-07 19:06:46 +01:00
|
|
|
this.load();
|
2012-12-05 00:57:21 +01:00
|
|
|
};
|
|
|
|
|
2012-12-07 19:06:46 +01:00
|
|
|
BranchGraph.prototype.load = function(){
|
|
|
|
$.ajax({
|
2012-12-07 22:05:17 +01:00
|
|
|
url: this.options.url,
|
2012-12-07 19:06:46 +01:00
|
|
|
method: 'get',
|
|
|
|
dataType: 'json',
|
|
|
|
success: $.proxy(function(data){
|
|
|
|
$('.loading', this.element).hide();
|
|
|
|
this.prepareData(data.days, data.commits);
|
2012-12-07 22:05:17 +01:00
|
|
|
this.buildGraph();
|
2012-12-07 19:06:46 +01:00
|
|
|
}, this)
|
|
|
|
});
|
2012-12-07 22:05:17 +01:00
|
|
|
};
|
2012-12-07 19:06:46 +01:00
|
|
|
|
|
|
|
BranchGraph.prototype.prepareData = function(days, commits){
|
|
|
|
this.days = days;
|
2012-12-07 22:05:17 +01:00
|
|
|
this.dayCount = days.length;
|
2012-12-07 19:06:46 +01:00
|
|
|
this.commits = commits;
|
2012-12-07 22:05:17 +01:00
|
|
|
this.commitCount = commits.length;
|
|
|
|
|
|
|
|
this.collectParents();
|
|
|
|
|
2012-12-07 19:06:46 +01:00
|
|
|
this.mtime += 4;
|
|
|
|
this.mspace += 10;
|
2012-12-07 22:05:17 +01:00
|
|
|
for (var i = 0; i < this.commitCount; i++) {
|
2012-12-05 00:57:21 +01:00
|
|
|
if (this.commits[i].id in this.parents) {
|
|
|
|
this.commits[i].isParent = true;
|
2011-11-13 12:58:45 +01:00
|
|
|
}
|
2012-12-07 22:05:17 +01:00
|
|
|
this.preparedCommits[this.commits[i].id] = this.commits[i];
|
2012-12-05 00:57:21 +01:00
|
|
|
}
|
2012-12-07 22:05:17 +01:00
|
|
|
this.collectColors();
|
|
|
|
};
|
|
|
|
|
|
|
|
BranchGraph.prototype.collectParents = function(){
|
|
|
|
for (var i = 0; i < this.commitCount; i++) {
|
|
|
|
for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) {
|
|
|
|
this.parents[this.commits[i].parents[j][0]] = true;
|
2011-11-13 12:58:45 +01:00
|
|
|
}
|
2012-12-07 22:05:17 +01:00
|
|
|
this.mtime = Math.max(this.mtime, this.commits[i].time);
|
|
|
|
this.mspace = Math.max(this.mspace, this.commits[i].space);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BranchGraph.prototype.collectColors = function(){
|
2012-12-05 00:57:21 +01:00
|
|
|
for (var k = 0; k < this.mspace; k++) {
|
|
|
|
this.colors.push(Raphael.getColor());
|
|
|
|
}
|
|
|
|
};
|
2011-11-15 09:34:30 +01:00
|
|
|
|
2012-12-07 22:05:17 +01:00
|
|
|
BranchGraph.prototype.buildGraph = function(){
|
|
|
|
var graphWidth = $(this.element).width()
|
|
|
|
, ch = this.mspace * 20 + 20
|
|
|
|
, cw = Math.max(graphWidth, this.mtime * 20 + 20)
|
|
|
|
, r = Raphael(this.element.get(0), cw, ch)
|
2012-12-05 00:57:21 +01:00
|
|
|
, top = r.set()
|
|
|
|
, cuday = 0
|
2012-12-07 19:06:46 +01:00
|
|
|
, cumonth = ""
|
2012-12-07 22:05:17 +01:00
|
|
|
, offsetX = 20
|
|
|
|
, offsetY = 60
|
|
|
|
, barWidth = Math.max(graphWidth, this.dayCount * 20 + 80);
|
2012-12-07 19:06:46 +01:00
|
|
|
|
|
|
|
this.raphael = r;
|
2012-12-05 00:57:21 +01:00
|
|
|
|
2012-12-07 22:05:17 +01:00
|
|
|
r.rect(0, 0, barWidth, 20).attr({fill: "#222"});
|
|
|
|
r.rect(0, 20, barWidth, 20).attr({fill: "#444"});
|
2012-12-05 00:57:21 +01:00
|
|
|
|
2012-12-07 22:05:17 +01:00
|
|
|
for (mm = 0; mm < this.dayCount; mm++) {
|
2012-12-05 00:57:21 +01:00
|
|
|
if(this.days[mm] != null){
|
|
|
|
if(cuday != this.days[mm][0]){
|
2012-12-07 22:05:17 +01:00
|
|
|
// Dates
|
|
|
|
r.text(offsetX + mm * 20, 31, this.days[mm][0]).attr({
|
|
|
|
font: "12px Monaco, Arial",
|
2012-12-05 00:57:21 +01:00
|
|
|
fill: "#DDD"
|
|
|
|
});
|
|
|
|
cuday = this.days[mm][0];
|
2011-11-12 23:30:51 +01:00
|
|
|
}
|
2012-12-05 00:57:21 +01:00
|
|
|
if(cumonth != this.days[mm][1]){
|
2012-12-07 22:05:17 +01:00
|
|
|
// Months
|
|
|
|
r.text(offsetX + mm * 20, 11, this.days[mm][1]).attr({
|
|
|
|
font: "12px Monaco, Arial",
|
2012-12-05 00:57:21 +01:00
|
|
|
fill: "#EEE"
|
|
|
|
});
|
|
|
|
cumonth = this.days[mm][1];
|
2011-11-12 23:30:51 +01:00
|
|
|
}
|
2012-12-05 00:57:21 +01:00
|
|
|
}
|
2011-11-12 23:30:51 +01:00
|
|
|
}
|
2012-12-05 00:57:21 +01:00
|
|
|
|
2012-12-07 22:05:17 +01:00
|
|
|
for (i = 0; i < this.commitCount; i++) {
|
|
|
|
var x = offsetX + 20 * this.commits[i].time
|
|
|
|
, y = offsetY + 20 * this.commits[i].space;
|
2012-12-05 00:57:21 +01:00
|
|
|
r.circle(x, y, 3).attr({
|
|
|
|
fill: this.colors[this.commits[i].space],
|
|
|
|
stroke: "none"
|
|
|
|
});
|
|
|
|
if (this.commits[i].refs != null && this.commits[i].refs != "") {
|
|
|
|
var longrefs = this.commits[i].refs
|
|
|
|
, shortrefs = this.commits[i].refs;
|
|
|
|
if (shortrefs.length > 15){
|
|
|
|
shortrefs = shortrefs.substr(0,13) + "...";
|
2011-11-12 23:30:51 +01:00
|
|
|
}
|
2012-12-07 22:05:17 +01:00
|
|
|
var t = r.text(x+5, y+8, shortrefs).attr({
|
|
|
|
font: "12px Monaco, Arial",
|
|
|
|
fill: "#666",
|
|
|
|
title: longrefs,
|
|
|
|
cursor: "pointer",
|
|
|
|
rotation: "90"
|
2012-12-05 00:57:21 +01:00
|
|
|
});
|
2011-11-15 09:34:30 +01:00
|
|
|
|
2012-12-05 00:57:21 +01:00
|
|
|
var textbox = t.getBBox();
|
|
|
|
t.translate(textbox.height/-4, textbox.width/2);
|
|
|
|
}
|
2012-12-07 22:05:17 +01:00
|
|
|
var c;
|
2012-12-05 00:57:21 +01:00
|
|
|
for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) {
|
2012-12-07 22:05:17 +01:00
|
|
|
c = this.preparedCommits[this.commits[i].parents[j][0]];
|
2012-12-05 00:57:21 +01:00
|
|
|
if (c) {
|
2012-12-07 22:05:17 +01:00
|
|
|
var cx = offsetX + 20 * c.time
|
|
|
|
, cy = offsetY + 20 * c.space;
|
2012-12-05 00:57:21 +01:00
|
|
|
if (c.space == this.commits[i].space) {
|
2012-12-07 22:05:17 +01:00
|
|
|
r.path([
|
|
|
|
"M", x, y,
|
|
|
|
"L", x - 20 * (c.time + 1), y
|
|
|
|
]).attr({
|
2012-12-05 00:57:21 +01:00
|
|
|
stroke: this.colors[c.space],
|
|
|
|
"stroke-width": 2
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (c.space < this.commits[i].space) {
|
|
|
|
r.path(["M", x - 5, y + .0001, "l-5-2,0,4,5,-2C", x - 5, y, x - 17, y + 2, x - 20, y - 5, "L", cx, y - 5, cx, cy])
|
|
|
|
.attr({
|
|
|
|
stroke: this.colors[this.commits[i].space],
|
|
|
|
"stroke-width": 2
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
r.path(["M", x - 3, y + 6, "l-4,3,4,2,0,-5L", x - 10, y + 20, "L", x - 10, cy, cx, cy])
|
|
|
|
.attr({
|
|
|
|
stroke: this.colors[c.space],
|
|
|
|
"stroke-width": 2
|
|
|
|
});
|
|
|
|
}
|
2011-11-12 23:30:51 +01:00
|
|
|
}
|
2012-12-05 00:57:21 +01:00
|
|
|
}
|
2012-12-07 19:06:46 +01:00
|
|
|
this.appendAnchor(top, this.commits[i], x, y);
|
2011-11-12 23:30:51 +01:00
|
|
|
}
|
|
|
|
top.toFront();
|
2012-12-07 22:05:17 +01:00
|
|
|
this.element.scrollLeft(cw);
|
|
|
|
this.bindEvents();
|
|
|
|
};
|
|
|
|
|
|
|
|
BranchGraph.prototype.bindEvents = function(){
|
|
|
|
var drag = {}
|
|
|
|
, element = this.element;
|
2012-12-07 19:06:46 +01:00
|
|
|
|
2012-12-07 22:05:17 +01:00
|
|
|
var dragger = function(event){
|
|
|
|
element.scrollLeft(drag.sl - (event.clientX - drag.x));
|
|
|
|
element.scrollTop(drag.st - (event.clientY - drag.y));
|
2011-11-12 23:30:51 +01:00
|
|
|
};
|
2012-12-07 22:05:17 +01:00
|
|
|
|
|
|
|
element.on({
|
|
|
|
mousedown: function (event) {
|
|
|
|
drag = {
|
|
|
|
x: event.clientX,
|
|
|
|
y: event.clientY,
|
|
|
|
st: element.scrollTop(),
|
|
|
|
sl: element.scrollLeft()
|
|
|
|
};
|
|
|
|
$(window).on('mousemove', dragger);
|
2012-12-07 19:06:46 +01:00
|
|
|
}
|
2012-12-07 22:05:17 +01:00
|
|
|
});
|
|
|
|
$(window).on({
|
|
|
|
mouseup: function(){
|
|
|
|
//bars.animate({opacity: 0}, 300);
|
|
|
|
$(window).off('mousemove', dragger);
|
|
|
|
},
|
|
|
|
keydown: function(event){
|
|
|
|
if(event.keyCode == 37){
|
|
|
|
// left
|
|
|
|
element.scrollLeft( element.scrollLeft() - 50);
|
2011-11-12 23:30:51 +01:00
|
|
|
}
|
2012-12-07 22:05:17 +01:00
|
|
|
if(event.keyCode == 38){
|
|
|
|
// top
|
|
|
|
element.scrollTop( element.scrollTop() - 50);
|
|
|
|
}
|
|
|
|
if(event.keyCode == 39){
|
|
|
|
// right
|
|
|
|
element.scrollLeft( element.scrollLeft() + 50);
|
|
|
|
}
|
|
|
|
if(event.keyCode == 40){
|
|
|
|
// bottom
|
|
|
|
element.scrollTop( element.scrollTop() + 50);
|
|
|
|
}
|
2012-12-07 19:06:46 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BranchGraph.prototype.appendAnchor = function(top, c, x, y) {
|
2012-12-07 22:05:17 +01:00
|
|
|
var r = this.raphael
|
|
|
|
, options = this.options
|
|
|
|
, anchor;
|
|
|
|
anchor = r.circle(x, y, 10).attr({
|
2012-12-07 19:06:46 +01:00
|
|
|
fill: "#000",
|
|
|
|
opacity: 0,
|
|
|
|
cursor: "pointer"
|
|
|
|
})
|
|
|
|
.click(function(){
|
2012-12-07 22:05:17 +01:00
|
|
|
window.location = options.commit_url.replace('%s', c.id);
|
2012-12-07 19:06:46 +01:00
|
|
|
})
|
2012-12-07 22:05:17 +01:00
|
|
|
.hover(function(){
|
|
|
|
var text = r.text(100, 100, c.author + "\n \n" + c.id + "\n \n" + c.message).attr({
|
2012-12-07 19:06:46 +01:00
|
|
|
fill: "#fff"
|
|
|
|
});
|
2012-12-07 22:05:17 +01:00
|
|
|
this.popup = r.tooltip(x, y + 5, text, 0);
|
2012-12-07 19:06:46 +01:00
|
|
|
top.push(this.popup.insertBefore(this));
|
2012-12-07 22:05:17 +01:00
|
|
|
}, function(){
|
2012-12-07 19:06:46 +01:00
|
|
|
this.popup && this.popup.remove() && delete this.popup;
|
2012-12-07 22:05:17 +01:00
|
|
|
});
|
|
|
|
top.push(anchor);
|
2012-12-05 00:57:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this.BranchGraph = BranchGraph;
|
|
|
|
|
|
|
|
}(this);
|
2012-12-07 22:05:17 +01:00
|
|
|
Raphael.fn.tooltip = function (x, y, set, dir, size) {
|
|
|
|
dir = dir == null ? 2 : dir;
|
|
|
|
size = size || 5;
|
|
|
|
x = Math.round(x);
|
|
|
|
y = Math.round(y);
|
|
|
|
var mmax = Math.max
|
|
|
|
, bb = set.getBBox()
|
|
|
|
, w = Math.round(bb.width / 2)
|
|
|
|
, h = Math.round(bb.height / 2)
|
|
|
|
, dx = [0, w + size * 2, 0, -w - size * 2]
|
|
|
|
, dy = [-h * 2 - size * 3, -h - size, 0, -h - size]
|
|
|
|
, p = ["M", x - dx[dir], y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,
|
|
|
|
"l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,
|
|
|
|
"l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,
|
|
|
|
"l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,
|
|
|
|
"l", -mmax(w - size, 0), 0, "z"].join(",")
|
|
|
|
, xy = [{x: x, y: y + size * 2 + h}, {x: x - size * 2 - w, y: y}, {x: x, y: y - size * 2 - h}, {x: x + size * 2 + w, y: y}][dir];
|
|
|
|
set.translate(xy.x - w - bb.x, xy.y - h - bb.y);
|
|
|
|
return this.set(this.path(p).attr({fill: "#234", stroke: "none"}).insertBefore(set.node ? set : set[0]), set);
|
2011-11-12 23:30:51 +01:00
|
|
|
};
|