2016-03-29 15:16:08 +05:30
|
|
|
var db = require('./gitGraphAst');
|
2016-03-31 10:40:35 +05:30
|
|
|
var _ = require('lodash');
|
2016-03-29 15:16:08 +05:30
|
|
|
var gitGraphParser = require('./parser/gitGraph');
|
2016-03-29 08:33:38 +05:30
|
|
|
var d3 = require('../../d3');
|
|
|
|
var Logger = require('../../logger');
|
|
|
|
|
|
|
|
var log = new Logger.Log();
|
2016-03-31 10:40:35 +05:30
|
|
|
var allCommitsDict = {};
|
2016-04-01 14:17:21 +05:30
|
|
|
var branchNum;
|
|
|
|
var config = {
|
2016-04-26 22:52:52 +05:30
|
|
|
nodeSpacing: 75,
|
2016-04-27 09:27:56 +05:30
|
|
|
nodeFillColor: 'yellow',
|
|
|
|
nodeStrokeWidth: 2,
|
|
|
|
nodeStrokeColor: 'grey',
|
2016-04-01 14:17:21 +05:30
|
|
|
lineStrokeWidth: 4,
|
2016-04-26 22:52:52 +05:30
|
|
|
branchOffset: 50,
|
2016-04-25 12:38:48 +05:30
|
|
|
lineColor: 'grey',
|
2016-04-02 11:51:04 +05:30
|
|
|
leftMargin: 50,
|
2016-04-27 09:27:56 +05:30
|
|
|
branchColors: ['#442f74', '#983351', '#609732', '#AA9A39'],
|
2016-04-26 22:52:52 +05:30
|
|
|
nodeRadius: 15,
|
2016-04-02 11:51:04 +05:30
|
|
|
nodeLabel: {
|
2016-04-25 13:01:17 +05:30
|
|
|
width: 75,
|
2016-04-02 11:51:04 +05:30
|
|
|
height: 100,
|
|
|
|
x: -25,
|
2016-04-25 13:01:17 +05:30
|
|
|
y: 15
|
2016-04-02 11:51:04 +05:30
|
|
|
}
|
2016-04-01 14:17:21 +05:30
|
|
|
}
|
2016-04-01 20:01:46 +05:30
|
|
|
var apiConfig = {};
|
|
|
|
exports.setConf = function(c) {
|
|
|
|
apiConfig = c;
|
2016-03-29 15:16:08 +05:30
|
|
|
}
|
2016-03-29 08:33:38 +05:30
|
|
|
|
2016-04-01 14:17:21 +05:30
|
|
|
|
2016-03-31 10:40:35 +05:30
|
|
|
function svgCreateDefs(svg) {
|
2016-04-02 11:51:04 +05:30
|
|
|
svg
|
2016-04-25 12:38:48 +05:30
|
|
|
.append('defs')
|
|
|
|
.append('g')
|
|
|
|
.attr('id', 'def-commit')
|
|
|
|
.append('circle')
|
2016-04-26 22:52:52 +05:30
|
|
|
.attr('r', config.nodeRadius)
|
2016-04-25 12:38:48 +05:30
|
|
|
.attr('cx', 0)
|
|
|
|
.attr('cy', 0);
|
|
|
|
svg.select('#def-commit')
|
2016-04-01 12:05:59 +05:30
|
|
|
.append('foreignObject')
|
2016-04-02 11:51:04 +05:30
|
|
|
.attr('width', config.nodeLabel.width)
|
|
|
|
.attr('height', config.nodeLabel.height)
|
|
|
|
.attr('x', config.nodeLabel.x)
|
|
|
|
.attr('y', config.nodeLabel.y)
|
2016-04-25 12:38:48 +05:30
|
|
|
.attr('class', 'node-label')
|
2016-04-01 12:05:59 +05:30
|
|
|
.attr('requiredFeatures', 'http://www.w3.org/TR/SVG11/feature#Extensibility')
|
|
|
|
.append('xhtml:p')
|
2016-04-27 09:47:39 +05:30
|
|
|
.html('');
|
2016-03-31 10:40:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-27 09:27:56 +05:30
|
|
|
function svgDrawLine(svg, points, colorIdx, interpolate) {
|
2016-04-25 12:38:48 +05:30
|
|
|
interpolate = interpolate || 'basis';
|
2016-04-27 09:27:56 +05:30
|
|
|
var color = config.branchColors[colorIdx % config.branchColors.length];
|
2016-03-31 10:40:35 +05:30
|
|
|
var lineGen = d3.svg.line()
|
2016-04-01 12:05:59 +05:30
|
|
|
.x(function(d) {
|
|
|
|
return Math.round(d.x)
|
|
|
|
})
|
|
|
|
.y(function(d) {
|
|
|
|
return Math.round(d.y)
|
|
|
|
})
|
|
|
|
.interpolate(interpolate);
|
2016-03-31 10:40:35 +05:30
|
|
|
|
|
|
|
svg
|
2016-04-25 12:38:48 +05:30
|
|
|
.append('svg:path')
|
|
|
|
.attr('d', lineGen(points))
|
2016-04-27 09:27:56 +05:30
|
|
|
.style('stroke', color)
|
2016-04-25 12:38:48 +05:30
|
|
|
.style('stroke-width', config.lineStrokeWidth)
|
|
|
|
.style('fill', 'none');
|
2016-03-31 10:40:35 +05:30
|
|
|
}
|
2016-04-01 12:05:59 +05:30
|
|
|
// Pass in the element and its pre-transform coords
|
|
|
|
function getElementCoords(element, coords) {
|
|
|
|
coords = coords || element.node().getBBox();
|
|
|
|
var ctm = element.node().getCTM(),
|
|
|
|
xn = ctm.e + coords.x * ctm.a,
|
|
|
|
yn = ctm.f + coords.y * ctm.d;
|
|
|
|
//log.debug(ctm, coords);
|
|
|
|
return {
|
|
|
|
left: xn,
|
|
|
|
top: yn,
|
|
|
|
width: coords.width,
|
|
|
|
height: coords.height
|
|
|
|
};
|
2016-04-25 12:38:48 +05:30
|
|
|
}
|
2016-03-31 10:40:35 +05:30
|
|
|
|
2016-04-27 09:27:56 +05:30
|
|
|
function svgDrawLineForCommits(svg, fromId, toId, direction, color) {
|
2016-04-25 12:38:48 +05:30
|
|
|
log.debug('svgDrawLineForCommits: ', fromId, toId);
|
|
|
|
var fromBbox = getElementCoords(svg.select('#node-' + fromId + ' circle'));
|
|
|
|
var toBbox = getElementCoords(svg.select('#node-' + toId + ' circle'));
|
|
|
|
//log.debug('svgDrawLineForCommits: ', fromBbox, toBbox);
|
2016-04-26 22:52:52 +05:30
|
|
|
switch (direction) {
|
|
|
|
case 'LR':
|
2016-04-27 09:27:56 +05:30
|
|
|
// (toBbox)
|
|
|
|
// +--------
|
|
|
|
// + (fromBbox)
|
2016-04-26 22:52:52 +05:30
|
|
|
if (fromBbox.left - toBbox.left > config.nodeSpacing) {
|
|
|
|
var lineStart = { x: fromBbox.left - config.nodeSpacing, y: toBbox.top + toBbox.height/2};
|
|
|
|
var lineEnd ={ x: toBbox.left + toBbox.width, y: toBbox.top + toBbox.height/2 };
|
2016-04-27 09:27:56 +05:30
|
|
|
svgDrawLine(svg, [lineStart , lineEnd], color, 'linear')
|
2016-04-26 22:52:52 +05:30
|
|
|
svgDrawLine(svg, [
|
|
|
|
{x: fromBbox.left, y: fromBbox.top + fromBbox.height/2},
|
|
|
|
{x: fromBbox.left - config.nodeSpacing/2, y: fromBbox.top + fromBbox.height/2},
|
|
|
|
{x: fromBbox.left - config.nodeSpacing/2, y: lineStart.y},
|
2016-04-27 09:27:56 +05:30
|
|
|
lineStart], color);
|
2016-04-26 22:52:52 +05:30
|
|
|
} else {
|
|
|
|
svgDrawLine(svg, [{
|
|
|
|
'x': fromBbox.left,
|
|
|
|
'y': fromBbox.top + fromBbox.height / 2
|
|
|
|
}, {
|
|
|
|
'x': fromBbox.left - config.nodeSpacing/2,
|
|
|
|
'y': fromBbox.top + fromBbox.height / 2
|
|
|
|
}, {
|
|
|
|
'x': fromBbox.left - config.nodeSpacing/2,
|
|
|
|
'y': toBbox.top + toBbox.height / 2
|
|
|
|
}, {
|
|
|
|
'x': toBbox.left + toBbox.width,
|
|
|
|
'y': toBbox.top + toBbox.height / 2
|
2016-04-27 09:27:56 +05:30
|
|
|
}], color);
|
2016-04-26 22:52:52 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'BT':
|
|
|
|
// + (fromBbox)
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// + (toBbox)
|
|
|
|
if (toBbox.top - fromBbox.top > config.nodeSpacing) {
|
|
|
|
lineStart = { x: toBbox.left + toBbox.width/2, y: fromBbox.top + fromBbox.height + config.nodeSpacing};
|
|
|
|
lineEnd ={ x: toBbox.left + toBbox.width/2, y: toBbox.top };
|
2016-04-27 09:27:56 +05:30
|
|
|
svgDrawLine(svg, [lineStart , lineEnd], color, 'linear')
|
2016-04-26 22:52:52 +05:30
|
|
|
svgDrawLine(svg, [
|
|
|
|
{x: fromBbox.left + fromBbox.width/2, y: fromBbox.top + fromBbox.height},
|
|
|
|
{x: fromBbox.left + fromBbox.width/2, y: fromBbox.top + fromBbox.height + config.nodeSpacing/2},
|
|
|
|
{x: toBbox.left + toBbox.width/2, y: lineStart.y - config.nodeSpacing/2},
|
2016-04-27 09:27:56 +05:30
|
|
|
lineStart], color);
|
2016-04-26 22:52:52 +05:30
|
|
|
} else {
|
|
|
|
svgDrawLine(svg, [{
|
|
|
|
'x': fromBbox.left + fromBbox.width/2,
|
|
|
|
'y': fromBbox.top + fromBbox.height
|
|
|
|
}, {
|
|
|
|
'x': fromBbox.left + fromBbox.width/2,
|
|
|
|
'y': fromBbox.top + config.nodeSpacing/2
|
|
|
|
}, {
|
|
|
|
'x': toBbox.left + toBbox.width/2,
|
|
|
|
'y': toBbox.top - config.nodeSpacing/2
|
|
|
|
}, {
|
|
|
|
'x': toBbox.left + toBbox.width/2,
|
|
|
|
'y': toBbox.top
|
2016-04-27 09:27:56 +05:30
|
|
|
}], color);
|
2016-04-26 22:52:52 +05:30
|
|
|
}
|
|
|
|
break;
|
2016-04-01 12:05:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function cloneNode(svg, selector) {
|
|
|
|
return svg.select(selector).node().cloneNode(true);
|
2016-03-31 10:40:35 +05:30
|
|
|
}
|
2016-04-01 12:05:59 +05:30
|
|
|
|
2016-04-01 14:17:21 +05:30
|
|
|
function renderCommitHistory(svg, commitid, branches, direction) {
|
2016-03-31 10:40:35 +05:30
|
|
|
var commit;
|
2016-04-26 22:52:52 +05:30
|
|
|
var numCommits = Object.keys(allCommitsDict).length;
|
2016-03-31 10:40:35 +05:30
|
|
|
if (_.isString(commitid)) {
|
|
|
|
do {
|
|
|
|
commit = allCommitsDict[commitid];
|
2016-04-25 12:38:48 +05:30
|
|
|
log.debug('in renderCommitHistory', commit.id, commit.seq);
|
|
|
|
if (svg.select('#node-' + commitid).size() > 0) {
|
2016-04-26 22:52:52 +05:30
|
|
|
return;
|
2016-04-01 14:17:21 +05:30
|
|
|
}
|
2016-03-31 10:40:35 +05:30
|
|
|
svg
|
2016-04-01 12:05:59 +05:30
|
|
|
.append(function() {
|
2016-04-25 12:38:48 +05:30
|
|
|
return cloneNode(svg, '#def-commit');
|
2016-04-01 12:05:59 +05:30
|
|
|
})
|
2016-04-25 12:38:48 +05:30
|
|
|
.attr('class', 'commit')
|
|
|
|
.attr('id', function() {
|
|
|
|
return 'node-' + commit.id;
|
2016-04-01 12:05:59 +05:30
|
|
|
})
|
2016-04-25 12:38:48 +05:30
|
|
|
.attr('transform', function() {
|
2016-04-26 22:52:52 +05:30
|
|
|
switch (direction) {
|
|
|
|
case 'LR':
|
|
|
|
return 'translate(' + (commit.seq * config.nodeSpacing + config.leftMargin) + ', '
|
|
|
|
+ (branchNum * config.branchOffset) + ')';
|
|
|
|
case 'BT':
|
|
|
|
return 'translate(' + (branchNum * config.branchOffset + config.leftMargin) + ', '
|
|
|
|
+ ((numCommits - commit.seq) * config.nodeSpacing) + ')';
|
|
|
|
}
|
2016-03-31 10:40:35 +05:30
|
|
|
})
|
2016-04-27 09:27:56 +05:30
|
|
|
.attr('fill', config.nodeFillColor)
|
|
|
|
.attr('stroke', config.nodeStrokeColor)
|
|
|
|
.attr('stroke-width', config.nodeStrokeWidth);
|
2016-03-31 10:40:35 +05:30
|
|
|
|
2016-04-25 12:38:48 +05:30
|
|
|
var branch = _.find(branches, ['commit', commit]);
|
2016-04-02 11:51:04 +05:30
|
|
|
if (branch) {
|
2016-04-25 12:38:48 +05:30
|
|
|
log.debug('found branch ', branch.name);
|
2016-04-26 22:52:52 +05:30
|
|
|
svg.select('#node-' + commit.id + ' p')
|
2016-04-27 09:47:39 +05:30
|
|
|
.append('xhtml:span')
|
|
|
|
.attr('class', 'branch-label')
|
|
|
|
.text(branch.name + ', ');
|
|
|
|
}
|
|
|
|
svg.select('#node-' + commit.id + ' p')
|
2016-04-26 22:52:52 +05:30
|
|
|
.append('xhtml:span')
|
2016-04-27 09:47:39 +05:30
|
|
|
.attr('class', 'commit-id')
|
|
|
|
.text(commit.id);
|
|
|
|
if (commit.message !== '' && direction === 'BT') {
|
|
|
|
svg.select('#node-' + commit.id + ' p')
|
|
|
|
.append('xhtml:span')
|
|
|
|
.attr('class', 'commit-msg')
|
|
|
|
.text( ', ' + commit.message);
|
2016-04-02 11:51:04 +05:30
|
|
|
}
|
2016-03-31 10:40:35 +05:30
|
|
|
commitid = commit.parent
|
|
|
|
} while (commitid && allCommitsDict[commitid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isArray(commitid)) {
|
2016-04-25 12:38:48 +05:30
|
|
|
log.debug('found merge commmit', commitid);
|
2016-04-01 14:17:21 +05:30
|
|
|
renderCommitHistory(svg, commitid[0], branches, direction);
|
|
|
|
branchNum++;
|
|
|
|
renderCommitHistory(svg, commitid[1], branches, direction);
|
2016-03-31 10:40:35 +05:30
|
|
|
}
|
|
|
|
}
|
2016-03-31 14:44:36 +05:30
|
|
|
|
2016-04-27 09:27:56 +05:30
|
|
|
function renderLines(svg, commit, direction, branchColor) {
|
|
|
|
branchColor = branchColor || 0;
|
|
|
|
while (commit.seq > 0 && !commit.lineDrawn) {
|
2016-03-31 14:44:36 +05:30
|
|
|
if (_.isString(commit.parent)) {
|
2016-04-27 09:27:56 +05:30
|
|
|
svgDrawLineForCommits(svg, commit.id, commit.parent, direction, branchColor);
|
|
|
|
commit.lineDrawn = true;
|
2016-03-31 14:44:36 +05:30
|
|
|
commit = allCommitsDict[commit.parent];
|
|
|
|
} else if (_.isArray(commit.parent)) {
|
2016-04-27 09:27:56 +05:30
|
|
|
svgDrawLineForCommits(svg, commit.id, commit.parent[0], direction, branchColor)
|
|
|
|
svgDrawLineForCommits(svg, commit.id, commit.parent[1], direction, branchColor + 1)
|
|
|
|
renderLines(svg, allCommitsDict[commit.parent[1]], direction, branchColor + 1);
|
|
|
|
commit.lineDrawn = true;
|
2016-03-31 14:44:36 +05:30
|
|
|
commit = allCommitsDict[commit.parent[0]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 12:05:59 +05:30
|
|
|
exports.draw = function(txt, id, ver) {
|
2016-03-30 14:36:39 +05:30
|
|
|
try {
|
2016-03-29 15:16:08 +05:30
|
|
|
var parser;
|
|
|
|
parser = gitGraphParser.parser;
|
|
|
|
parser.yy = db;
|
2016-03-29 08:33:38 +05:30
|
|
|
|
2016-03-29 15:16:08 +05:30
|
|
|
log.debug('in gitgraph renderer', txt, id, ver);
|
|
|
|
// Parse the graph definition
|
2016-04-25 12:38:48 +05:30
|
|
|
parser.parse(txt + '\n');
|
2016-04-01 20:01:46 +05:30
|
|
|
|
|
|
|
config = _.extend(config, apiConfig, db.getOptions());
|
2016-04-25 12:38:48 +05:30
|
|
|
log.debug('effective options', config);
|
2016-03-30 14:36:39 +05:30
|
|
|
var direction = db.getDirection();
|
2016-04-27 09:27:56 +05:30
|
|
|
allCommitsDict = db.getCommits();
|
|
|
|
var branches = db.getBranchesAsObjArray();
|
2016-04-26 22:52:52 +05:30
|
|
|
if (direction === 'BT') {
|
2016-04-27 09:27:56 +05:30
|
|
|
config.nodeLabel.x = branches.length * config.branchOffset;
|
2016-04-27 09:47:39 +05:30
|
|
|
config.nodeLabel.width = '100%';
|
2016-04-26 22:52:52 +05:30
|
|
|
config.nodeLabel.y = -1 * 2* config.nodeRadius;
|
|
|
|
}
|
2016-03-30 14:36:39 +05:30
|
|
|
var svg = d3.select('#' + id);
|
2016-03-31 10:40:35 +05:30
|
|
|
svgCreateDefs(svg);
|
2016-04-01 14:17:21 +05:30
|
|
|
branchNum = 1;
|
2016-04-25 12:38:48 +05:30
|
|
|
_.each(branches, function(v) {
|
2016-04-01 14:17:21 +05:30
|
|
|
renderCommitHistory(svg, v.commit.id, branches, direction);
|
2016-04-26 22:52:52 +05:30
|
|
|
renderLines(svg, v.commit, direction);
|
2016-03-31 15:47:38 +05:30
|
|
|
branchNum++;
|
2016-04-27 09:27:56 +05:30
|
|
|
});
|
2016-03-30 14:36:39 +05:30
|
|
|
} catch (e) {
|
2016-04-25 12:38:48 +05:30
|
|
|
log.error('Error while rendering gitgraph');
|
2016-03-29 15:16:08 +05:30
|
|
|
log.error(e.message);
|
|
|
|
}
|
2016-03-29 08:33:38 +05:30
|
|
|
};
|