forked from JustinPealing/html-query-plan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qp.js
80 lines (70 loc) · 3.09 KB
/
qp.js
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
if (typeof (QP) == "undefined" || !QP) {
var QP = {}
};
(function() {
/* Draws the lines linking nodes in query plan diagram.
root - The document element in which the diagram is contained. */
QP.drawLines = function(root) {
if (root === null || root === undefined) {
// Try and find it ourselves
root = $(".qp-root").parent();
} else {
// Make sure the object passed is jQuery wrapped
root = $(root);
}
internalDrawLines(root);
};
/* Internal implementaiton of drawLines. */
function internalDrawLines(root) {
var canvas = getCanvas(root);
var canvasElm = canvas[0];
// Check for browser compatability
if (canvasElm.getContext !== null && canvasElm.getContext !== undefined) {
// Chrome is usually too quick with document.ready
window.setTimeout(function() {
var context = canvasElm.getContext("2d");
// The first root node may be smaller than the full query plan if using overflow
var firstNode = $(".qp-tr", root);
canvasElm.width = firstNode.outerWidth(true);
canvasElm.height = firstNode.outerHeight(true);
var offset = canvas.offset();
$(".qp-tr", root).each(function() {
var from = $("> * > .qp-node", $(this));
$("> * > .qp-tr > * > .qp-node", $(this)).each(function() {
drawLine(context, offset, from, $(this));
});
});
context.stroke();
}, 1);
}
}
/* Locates or creates the canvas element to use to draw lines for a given root element. */
function getCanvas(root) {
var returnValue = $("canvas", root);
if (returnValue.length == 0) {
root.prepend($("<canvas></canvas>")
.css("position", "absolute")
.css("top", 0).css("left", 0)
);
returnValue = $("canvas", root);
}
return returnValue;
}
/* Draws a line between two nodes.
context - The canvas context with which to draw.
offset - Canvas offset in the document.
from - The document jQuery object from which to draw the line.
to - The document jQuery object to which to draw the line. */
function drawLine(context, offset, from, to) {
var fromOffset = from.offset();
fromOffset.top += from.outerHeight() / 2;
fromOffset.left += from.outerWidth();
var toOffset = to.offset();
toOffset.top += to.outerHeight() / 2;
var midOffsetLeft = fromOffset.left / 2 + toOffset.left / 2;
context.moveTo(fromOffset.left - offset.left, fromOffset.top - offset.top);
context.lineTo(midOffsetLeft - offset.left, fromOffset.top - offset.top);
context.lineTo(midOffsetLeft - offset.left, toOffset.top - offset.top);
context.lineTo(toOffset.left - offset.left, toOffset.top - offset.top);
}
})();