/
graph.html
181 lines (167 loc) · 7.7 KB
/
graph.html
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
$def with (changeset_url, root_url, branches, svn_logs, copyfrom)
<html>
<head>
<title>svn_branch_graph for ${', '.join(branches)}</title>
<style type="text/css">
body {
font-family: Arial;
}
</style>
<script type="text/javascript">
/**
* Parameters:
* - canvas: the <canvas> element
* - numBranches: number of branches
* - svnLogs: a list of logs in the format of
* [{revision: ..., author: ..., msg: ...}, ...]
* - copyFrom: a hash of branch -> revision that the branch is
* copied from
* - changesetUrl: a url to go to when a commit is clicked, "%s"
* will be replaced by the revision.
* - author: a html element to write the author in
* - revision: a html element to write the revision in
* - dateHtml: a html element to write the date in
* - msg: a html element to write the message in
*/
function SvnBranchGraph(canvas, numBranches, svnLogs, copyFrom,
changesetUrl, author, revision, dateHtml, msg) {
this.canvas = canvas;
this.canvas.addEventListener('mousemove', this.mouseHandler(),
false);
this.canvas.addEventListener('mousedown', this.mouseHandler(),
false);
this.ctx = this.canvas.getContext('2d');
this.ctx.font = '18px Arial';
this.numBranches = numBranches;
this.svnLogs = svnLogs;
this.copyFrom = copyFrom;
this.changesetUrl = changesetUrl;
this.author = author;
this.revision = revision;
this.date = dateHtml;
this.msg = msg;
/* mouse event handlers */
this.mousemove = ['displayInfo'];
this.mouseup = [];
this.mousedown = ['openChangesetUrl'];
/* radius of dots representing commits */
this.radius = 4;
/* the x coordinate to start drawing commits */
this.startX = 200;
/* the y coordinate to start drawing */
this.startY = 15;
/* the amount to increment y after each branch */
this.incrementY = 25;
}
SvnBranchGraph.prototype.mouseHandler = function() {
var this_ = this;
return function(ev) {
var x = ev.layerX - this_.canvas.offsetLeft;
var y = ev.layerY - this_.canvas.offsetTop;
for (var revision in this_.revisions) {
var info = this_.revisions[revision];
if (x >= info.x - this_.radius &&
x <= info.x + this_.radius &&
y >= info.y - this_.radius &&
y <= info.y + this_.radius) {
for (var i = 0; i < this_[ev.type].length; i++) {
this_[this_[ev.type][i]](info.info);
}
}
}
};
}
SvnBranchGraph.prototype.openChangesetUrl = function(commit) {
if (this.revision.innerHTML == commit.revision) {
window.open(this.changesetUrl.replace('%s',
commit.revision));
}
}
SvnBranchGraph.prototype.displayInfo = function(commit) {
this.author.innerHTML = commit.author;
this.revision.innerHTML = commit.revision;
this.date.innerHTML = commit.date;
this.msg.innerHTML = commit.msg;
}
SvnBranchGraph.prototype.draw = function() {
/* adjust the size of the canvas */
this.canvas.height = this.startY + this.incrementY * this.numBranches;
this.canvas.width = this.startX + this.radius * 3 * this.svnLogs.length;
var branches = {};
this.revisions = {};
var x = this.startX;
var y = this.startY;
var previousBranch = '';
for (var i = 0; i < this.svnLogs.length; i++) {
var info = this.svnLogs[i];
// try to compress the number of dots to display
if (info.branch == previousBranch) {
x += this.radius / 2;
} else {
x += this.radius * 3;
}
previousBranch = info.branch;
if (branches[info.branch] === undefined) {
branches[info.branch] = {'x': 0, 'y': y - this.radius};
this.ctx.fillText(info.branch, 10, y);
y += this.incrementY;
}
branches[info.branch].x = x;
this.drawCommit(x, branches[info.branch].y);
this.revisions[info.revision] = {
'x': x, 'y': branches[info.branch].y, 'info': info};
}
for (var branch in branches) {
var pos = branches[branch];
this.ctx.beginPath();
var copyfrom = this.copyFrom[branch];
if (copyfrom !== undefined &&
this.revisions[copyfrom] !== undefined) {
moveToX = this.revisions[copyfrom].x;
moveToY = this.revisions[copyfrom].y;
} else {
moveToX = this.startX;
moveToY = pos.y;
}
this.ctx.moveTo(moveToX, moveToY);
this.ctx.lineTo(moveToX, pos.y);
this.ctx.lineTo(pos.x, pos.y);
this.ctx.moveTo(-moveToX, -moveToY);
this.ctx.stroke();
this.ctx.closePath();
}
}
SvnBranchGraph.prototype.drawCommit = function(x, y) {
// x, y is the center of the circle
this.ctx.beginPath();
this.ctx.arc(x, y, this.radius, 0, Math.PI * 2, true);
this.ctx.fill();
this.ctx.closePath();
}
function bodyOnload() {
var graph = new SvnBranchGraph(document.getElementById('canvas'),
${len(branches)},
eval('$:{svn_logs.replace("'", "\\'")}'),
eval('($:{copyfrom.replace("'", "\\'")})'),
eval('$:changeset_url'),
document.getElementById('author'),
document.getElementById('revision'),
document.getElementById('date'),
document.getElementById('msg'));
graph.draw();
}
</script>
</head>
<body onload="bodyOnload();">
<p>
<a href="$root_url">I want to view other branches.</a>
</p>
<div>
<p><strong>Author:</strong> <span id="author"></span></p>
<p><strong>Revision:</strong> <span id="revision"></span></p>
<p><strong>Date:</strong> <span id="date"></span></p>
<p style="height: 50px"><strong>Message:</strong> <span id="msg"></span></p>
</div>
<canvas id="canvas" width="2048" height="800"></canvas>
</body>
</html>