qtip2
Version:
Introducing... qTip2. The second generation of the advanced qTip plugin for the ever popular jQuery framework.
91 lines (78 loc) • 2.49 kB
JavaScript
PLUGINS.svg = function(api, svg, corner)
{
var elem = svg[0],
root = $(elem.ownerSVGElement),
ownerDocument = elem.ownerDocument,
strokeWidth2 = (parseInt(svg.css('stroke-width'), 10) || 0) / 2,
frameOffset, mtx, transformed,
len, next, i, points,
result, position;
// Ascend the parentNode chain until we find an element with getBBox()
while(!elem.getBBox) { elem = elem.parentNode; }
if(!elem.getBBox || !elem.parentNode) { return FALSE; }
// Determine which shape calculation to use
switch(elem.nodeName) {
case 'ellipse':
case 'circle':
result = PLUGINS.polys.ellipse(
elem.cx.baseVal.value,
elem.cy.baseVal.value,
(elem.rx || elem.r).baseVal.value + strokeWidth2,
(elem.ry || elem.r).baseVal.value + strokeWidth2,
corner
);
break;
case 'line':
case 'polygon':
case 'polyline':
// Determine points object (line has none, so mimic using array)
points = elem.points || [
{ x: elem.x1.baseVal.value, y: elem.y1.baseVal.value },
{ x: elem.x2.baseVal.value, y: elem.y2.baseVal.value }
];
for(result = [], i = -1, len = points.numberOfItems || points.length; ++i < len;) {
next = points.getItem ? points.getItem(i) : points[i];
result.push.apply(result, [next.x, next.y]);
}
result = PLUGINS.polys.polygon(result, corner);
break;
// Unknown shape or rectangle? Use bounding box
default:
result = elem.getBBox();
result = {
width: result.width,
height: result.height,
position: {
left: result.x,
top: result.y
}
};
break;
}
// Shortcut assignments
position = result.position;
root = root[0];
// Convert position into a pixel value
if(root.createSVGPoint) {
mtx = elem.getScreenCTM();
points = root.createSVGPoint();
points.x = position.left;
points.y = position.top;
transformed = points.matrixTransform( mtx );
position.left = transformed.x;
position.top = transformed.y;
}
// Check the element is not in a child document, and if so, adjust for frame elements offset
if(ownerDocument !== document && api.position.target !== 'mouse') {
frameOffset = $((ownerDocument.defaultView || ownerDocument.parentWindow).frameElement).offset();
if(frameOffset) {
position.left += frameOffset.left;
position.top += frameOffset.top;
}
}
// Adjust by scroll offset of owner document
ownerDocument = $(ownerDocument);
position.left += ownerDocument.scrollLeft();
position.top += ownerDocument.scrollTop();
return result;
};