// ... and modifications to support scrolling divs.
/**
- * Find the x-coordinate of the supplied object relative to the left side
- * of the page.
+ * Find the coordinates of an object relative to the top left of the page.
* TODO(danvk): change obj type from Node -> !Node
* @param {Node} obj
- * @return {number}
+ * @return {{x:number,y:number}}
* @private
*/
-Dygraph.findPosX = function(obj) {
- var curleft = 0;
- if(obj.offsetParent) {
+Dygraph.findPos = function(obj) {
+ var curleft = 0, curtop = 0;
+ if (obj.offsetParent) {
var copyObj = obj;
- while(1) {
+ while (1) {
// NOTE: the if statement here is for IE8.
- var borderLeft = "0";
+ var borderLeft = "0", borderTop = "0";
if (window.getComputedStyle) {
- borderLeft = window.getComputedStyle(copyObj, null).borderLeft || "0";
+ var computedStyle = window.getComputedStyle(copyObj, null);
+ borderLeft = computedStyle.borderLeft || "0";
+ borderTop = computedStyle.borderTop || "0";
}
curleft += parseInt(borderLeft, 10) ;
- curleft += copyObj.offsetLeft;
- if(!copyObj.offsetParent) {
- break;
- }
- copyObj = copyObj.offsetParent;
- }
- } else if(obj.x) {
- curleft += obj.x;
- }
- // This handles the case where the object is inside a scrolled div.
- while(obj && obj != document.body) {
- curleft -= obj.scrollLeft;
- obj = obj.parentNode;
- }
- return curleft;
-};
-
-/**
- * Find the y-coordinate of the supplied object relative to the top of the
- * page.
- * TODO(danvk): change obj type from Node -> !Node
- * TODO(danvk): consolidate with findPosX and return an {x, y} object.
- * @param {Node} obj
- * @return {number}
- * @private
- */
-Dygraph.findPosY = function(obj) {
- var curtop = 0;
- if(obj.offsetParent) {
- var copyObj = obj;
- while(1) {
- // NOTE: the if statement here is for IE8.
- var borderTop = "0";
- if (window.getComputedStyle) {
- borderTop = window.getComputedStyle(copyObj, null).borderTop || "0";
- }
curtop += parseInt(borderTop, 10) ;
+ curleft += copyObj.offsetLeft;
curtop += copyObj.offsetTop;
- if(!copyObj.offsetParent) {
+ if (!copyObj.offsetParent) {
break;
}
copyObj = copyObj.offsetParent;
}
- } else if(obj.y) {
- curtop += obj.y;
+ } else {
+ // TODO(danvk): why would obj ever have these properties?
+ if (obj.x) curleft += obj.x;
+ if (obj.y) curtop += obj.y;
}
+
// This handles the case where the object is inside a scrolled div.
- while(obj && obj != document.body) {
+ while (obj && obj != document.body) {
+ curleft -= obj.scrollLeft;
curtop -= obj.scrollTop;
obj = obj.parentNode;
}
- return curtop;
+ return {x: curleft, y: curtop};
};
/**
var iframes = document.getElementsByTagName("iframe");
for (var i = 0; i < iframes.length; i++) {
var iframe = iframes[i];
- var x = Dygraph.findPosX(iframe),
- y = Dygraph.findPosY(iframe),
+ var pos = Dygraph.findPos(iframe),
+ x = pos.x,
+ y = pos.y,
width = iframe.offsetWidth,
height = iframe.offsetHeight;
event.cancelBubble = true;
}
- contextB.px = Dygraph.findPosX(g.canvas_);
- contextB.py = Dygraph.findPosY(g.canvas_);
+ var canvasPos = Dygraph.findPos(g.canvas_);
+ contextB.px = canvasPos.x;
+ contextB.py = canvasPos.y;
contextB.dragStartX = g.dragGetX_(event, contextB);
contextB.dragStartY = g.dragGetY_(event, contextB);
contextB.cancelNextDblclick = false;
if (event.offsetX && event.offsetY) {
return [ event.offsetX, event.offsetY ];
} else {
- var canvasx = Dygraph.pageX(event) - Dygraph.findPosX(this.mouseEventElement_);
- var canvasy = Dygraph.pageY(event) - Dygraph.findPosY(this.mouseEventElement_);
+ var eventElementPos = Dygraph.findPos(this.mouseEventElement_);
+ var canvasx = Dygraph.pageX(event) - eventElementPos.x;
+ var canvasy = Dygraph.pageY(event) - eventElementPos.y;
return [canvasx, canvasy];
}
};
var valueRange = [0, 100];
function setPoint(event, g, context) {
- var canvasx = Dygraph.pageX(event) - Dygraph.findPosX(g.graphDiv);
- var canvasy = Dygraph.pageY(event) - Dygraph.findPosY(g.graphDiv);
+ var graphPos = Dygraph.findPos(g.graphDiv);
+ var canvasx = Dygraph.pageX(event) - graphPos.x;
+ var canvasy = Dygraph.pageY(event) - graphPos.y;
var xy = g.toDataCoords(canvasx, canvasy);
var x = xy[0], value = xy[1];
var rows = g.numRows();
});
window.onmouseup = finishDraw;
}
- });
\ No newline at end of file
+ });