mermaid/src/diagrams/gitGraph/gitGraphRenderer.js

187 lines
6.0 KiB
JavaScript
Raw Normal View History

var db = require('./gitGraphAst');
2016-03-31 10:40:35 +05:30
var _ = require('lodash');
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 12:05:59 +05:30
exports.setConf = function(config) {
}
2016-03-29 08:33:38 +05:30
2016-03-31 10:40:35 +05:30
function svgCreateDefs(svg) {
svg.append("defs")
2016-04-01 12:05:59 +05:30
.append("g")
2016-03-31 10:40:35 +05:30
.attr("id", "def-commit")
2016-04-01 12:05:59 +05:30
.append("circle")
2016-03-31 10:40:35 +05:30
.attr("r", 15)
.attr("cx", 0)
.attr("cy", 0);
2016-04-01 12:05:59 +05:30
svg.select("#def-commit")
.append('foreignObject')
.attr('width', 100)
.attr('height', 100)
.attr('x', 50)
.attr('y', 50)
.attr('requiredFeatures', 'http://www.w3.org/TR/SVG11/feature#Extensibility')
.append('xhtml:p')
.text("a big chunk of text that should wrap");
//.attr("requiredExtensions", "http://www.w3.org/1999/xhtml")
//.attr("width", 50)
//.attr("height", 30)
//.attr("x", 30)
//.attr("y", 30)
//.append("xhtml:body")
//.append("xhtml:p")
//.text("something")
2016-03-31 10:40:35 +05:30
}
2016-04-01 12:05:59 +05:30
function svgDrawLine(svg, points, interpolate) {
interpolate = interpolate || "basis";
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-01 12:05:59 +05:30
.append("svg:path")
.attr("d", lineGen(points))
.style("stroke", "grey")
.style("stroke-width", "4")
.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-03-31 10:40:35 +05:30
function svgDrawLineForCommits(svg, fromId, toId) {
log.debug("svgDrawLineForCommits: ", fromId, toId);
2016-04-01 12:05:59 +05:30
var fromBbox = getElementCoords(svg.select("#node-" + fromId + " circle"));
var toBbox = getElementCoords(svg.select("#node-" + toId + " circle"));
//log.debug("svgDrawLineForCommits: ", fromBbox, toBbox);
2016-04-01 12:05:59 +05:30
if (fromBbox.left - toBbox.left > 100) {
var lineStart = { x: fromBbox.left - 100, y: toBbox.top + toBbox.height/2};
var lineEnd ={ x: toBbox.left + toBbox.width, y: toBbox.top + toBbox.height/2 };
svgDrawLine(svg, [lineStart , lineEnd], "linear")
svgDrawLine(svg, [
{x: fromBbox.left, y: fromBbox.top + fromBbox.height/2},
{x: fromBbox.left - 50, y: fromBbox.top + fromBbox.height/2},
{x: fromBbox.left - 50, y: lineStart.y},
lineStart]);
} else {
svgDrawLine(svg, [{
"x": fromBbox.left,
"y": fromBbox.top + fromBbox.height / 2
}, {
"x": fromBbox.left - 50,
"y": fromBbox.top + fromBbox.height / 2
}, {
"x": fromBbox.left - 50,
"y": toBbox.top + toBbox.height / 2
}, {
"x": toBbox.left + toBbox.width,
"y": toBbox.top + toBbox.height / 2
}]);
}
}
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-03-31 10:40:35 +05:30
function renderCommitHistory(svg, commitid, branches, direction, branchNum) {
var commit;
branchNum = branchNum || 1;
if (_.isString(commitid)) {
do {
commit = allCommitsDict[commitid];
log.debug("in renderCommitHistory", commit.id, commit.seq);
2016-04-01 12:05:59 +05:30
if (svg.select("#node-" + commitid).size() > 0) return;
2016-03-31 10:40:35 +05:30
svg
2016-04-01 12:05:59 +05:30
.append(function() {
return cloneNode(svg, "#def-commit");
})
2016-03-31 10:40:35 +05:30
.attr("class", "commit")
2016-04-01 12:05:59 +05:30
.attr("id", function() {
return "node-" + commit.id;
})
//.append("use")
2016-03-31 10:40:35 +05:30
.attr("transform", function() {
2016-04-01 12:05:59 +05:30
return "translate(" + (commit.seq * 100 + 50) + ", " + (branchNum * 50) + ")";
2016-03-31 10:40:35 +05:30
})
2016-04-01 12:05:59 +05:30
//.attr("xlink:href", "#def-commit")
2016-03-31 10:40:35 +05:30
.attr("fill", "yellow")
.attr("stroke", "grey")
.attr("stroke-width", "2");
commitid = commit.parent
} while (commitid && allCommitsDict[commitid]);
}
if (_.isArray(commitid)) {
log.debug("found merge commmit", commitid);
renderCommitHistory(svg, commitid[0], branches, direction, branchNum);
renderCommitHistory(svg, commitid[1], branches, direction, ++branchNum);
}
}
function renderLines(svg, commit) {
while (commit.seq > 0) {
if (_.isString(commit.parent)) {
svgDrawLineForCommits(svg, commit.id, commit.parent);
commit = allCommitsDict[commit.parent];
} else if (_.isArray(commit.parent)) {
svgDrawLineForCommits(svg, commit.id, commit.parent[0])
svgDrawLineForCommits(svg, commit.id, commit.parent[1])
renderLines(svg, allCommitsDict[commit.parent[1]]);
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 {
var parser;
parser = gitGraphParser.parser;
parser.yy = db;
2016-03-29 08:33:38 +05:30
log.debug('in gitgraph renderer', txt, id, ver);
// Parse the graph definition
parser.parse(txt + "\n");
2016-03-30 14:36:39 +05:30
var direction = db.getDirection();
2016-03-31 10:40:35 +05:30
allCommitsDict = db.getCommits();
var branches = db.getBranchesAsObjArray();
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 12:05:59 +05:30
var branchNum = 1;
_.each(branches, function(v, k) {
renderCommitHistory(svg, v.commit.id, branches, direction, branchNum);
renderLines(svg, v.commit);
branchNum++;
})
2016-03-29 08:33:38 +05:30
2016-03-30 14:36:39 +05:30
svg.attr('height', 900);
svg.attr('width', 1200);
} catch (e) {
log.error("Error while rendering gitgraph");
log.error(e.message);
}
2016-03-29 08:33:38 +05:30
};