Skip to content

Commit 5f99a3b

Browse files
committed
0.13dev: Avoid clipping in [[Workflow()]] macro.
Patch by Peter Suter. Closes #10270. git-svn-id: http://trac.edgewall.org/intertrac/log:/trunk@10768 af82e41b-90c4-0310-8c96-b1721e28e2e2
1 parent 3dee3ec commit 5f99a3b

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

trac/htdocs/js/workflow_graph.js

+52-45
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,61 @@
11
(function($){
22

3+
function computeBoundsAndJustify(ctx, nodes, width, height) {
4+
var borderx = 20, bordery = 20, r = 7, h = 13;
5+
ctx.font = '13px Arial';
6+
var bws = [], xs = [], ys = [];
7+
$.each(nodes, function (index, node) {
8+
var w = ctx.measureText(node.label).width;
9+
node.labelWidth = w;
10+
node.bw = w + r + r;
11+
node.bh = h + r + r;
12+
node.br = r;
13+
bws.push(node.bw);
14+
xs.push(node.x);
15+
ys.push(node.y);
16+
});
17+
var maxbw = Math.max.apply(Math, bws);
18+
var maxbh = h + r + r;
19+
var minx = Math.min.apply(Math, xs);
20+
var maxx = Math.max.apply(Math, xs);
21+
var miny = Math.min.apply(Math, ys);
22+
var maxy = Math.max.apply(Math, ys);
23+
var bx = borderx + maxbw / 2;
24+
var by = bordery + maxbh / 2;
25+
if(minx == maxx) {
26+
minx -= 1;
27+
maxx += 1;
28+
}
29+
if(miny == maxy) {
30+
miny -= 1;
31+
maxy += 1;
32+
}
33+
$.each(nodes, function (index, node) {
34+
node.x = bx + (node.x - minx) * (width - 2 * bx) / (maxx - minx);
35+
node.y = by + (node.y - miny) * (height - 2 * by) / (maxy - miny);
36+
node.bx = node.x - node.bw / 2;
37+
node.by = node.y - node.bh / 2;
38+
});
39+
}
40+
341
function drawNode(ctx, node) {
4-
ctx.textBaseline = 'middle';
542
ctx.font = '13px Arial';
6-
var measured = ctx.measureText(node.label);
7-
var w = measured.width, h = 13;
8-
var x = node.x - w / 2, y = node.y - h / 2, r = 7;
9-
ctx.fillText(node.label, x, y + h / 2);
43+
ctx.textBaseline = 'middle';
44+
var x = node.bx, y = node.by, r = node.br, w = node.bw, h = node.bh;
45+
ctx.fillText(node.label, x + r, y + h / 2);
1046
ctx.lineWidth = 2;
11-
ctx.beginPath();
12-
ctx.moveTo(x, y - r);
13-
ctx.lineTo(x + w, y - r);
14-
ctx.quadraticCurveTo(x + w + r, y - r, x + w + r, y);
15-
ctx.lineTo(x + w + r, y + h);
16-
ctx.quadraticCurveTo(x + w + r, y + h + r, x + w, y + h + r);
17-
ctx.lineTo(x, y + h + r);
18-
ctx.quadraticCurveTo(x - r, y + h + r, x - r, y + h);
19-
ctx.lineTo(x - r, y);
20-
ctx.quadraticCurveTo(x - r, y - r, x, y - r);
47+
ctx.beginPath();
48+
ctx.moveTo(x + r, y);
49+
ctx.lineTo(x + w - r, y);
50+
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
51+
ctx.lineTo(x + w, y + h - r);
52+
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
53+
ctx.lineTo(x + r, y + h);
54+
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
55+
ctx.lineTo(x, y + r);
56+
ctx.quadraticCurveTo(x, y, x + r, y);
2157
ctx.closePath();
2258
ctx.stroke();
23-
24-
node.bx = x - r;
25-
node.by = y - r;
26-
node.bw = w + r + r;
27-
node.bh = h + r + r;
2859
}
2960

3061
function lerp(a, b, t) {
@@ -173,23 +204,11 @@
173204
node.x = Math.cos(radian);
174205
node.y = Math.sin(radian);
175206
});
176-
if (graph.nodes.length == 1) {
177-
graph.minx = 1;
178-
graph.maxx = 1;
179-
graph.miny = 1;
180-
graph.maxy = 1;
181-
} else {
182-
graph.minx = -1;
183-
graph.maxx = 1;
184-
graph.miny = -1;
185-
graph.maxy = 1;
186-
}
187207
}
188208

189209
$(document).ready(function() {
190210
$('.trac-workflow-graph').each(function (index) {
191211
var data = window['graph_' + this.id.slice(-8)];
192-
var borderx = 50, bordery = 50;
193212
var width = data.width, height = data.height;
194213
var nodes = [], actions = [], edges = [];
195214
for (var i = 0; i < data.nodes.length; ++i)
@@ -218,21 +237,9 @@
218237

219238
var graph = {nodes: nodes, edges: edges};
220239
layoutCircular(graph);
221-
222-
if(graph.minx == graph.maxx) {
223-
graph.minx -= 1;
224-
graph.maxx += 1;
225-
}
226-
if(graph.miny == graph.maxy) {
227-
graph.miny -= 1;
228-
graph.maxy += 1;
229-
}
240+
computeBoundsAndJustify(ctx, nodes, width, height);
230241

231242
$.each(nodes, function (index, node) {
232-
node.x = borderx + (node.x - graph.minx) * (width - 2 * borderx)
233-
/ (graph.maxx - graph.minx);
234-
node.y = bordery + (node.y - graph.miny) * (height - 2 * bordery)
235-
/ (graph.maxy - graph.miny);
236243
drawNode(ctx, node);
237244
});
238245
$.each(edges, function (index, edge) {

0 commit comments

Comments
 (0)