From 1da8f823bfdaa63a06d27c46b61bfc4ad994358a Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Fri, 19 Jul 2013 19:09:33 +0200 Subject: [PATCH] Consolidate findPos{X,Y} -> findPos --- auto_tests/tests/DygraphOps.js | 5 +-- dygraph-utils.js | 75 +++++++++++++----------------------------- dygraph.js | 10 +++--- gallery/drawing.js | 7 ++-- gallery/interaction-api.js | 5 +-- tests/interaction.js | 5 +-- 6 files changed, 42 insertions(+), 65 deletions(-) diff --git a/auto_tests/tests/DygraphOps.js b/auto_tests/tests/DygraphOps.js index 779b845..52f5943 100644 --- a/auto_tests/tests/DygraphOps.js +++ b/auto_tests/tests/DygraphOps.js @@ -109,8 +109,9 @@ DygraphOps.dispatchDoubleClick = function(g, custom) { * type, screenX, screenY, clientX, clientY. */ DygraphOps.createOptsForPoint_ = function(g, type, x, y) { - var pageX = Dygraph.findPosX(g.canvas_) + x; - var pageY = Dygraph.findPosY(g.canvas_) + y; + var pos = Dygraph.findPos(g.canvas_); + var pageX = pos.x + x; + var pageY = pos.y + y; return { type : type, diff --git a/dygraph-utils.js b/dygraph-utils.js index b5cec4c..589cc3a 100644 --- a/dygraph-utils.js +++ b/dygraph-utils.js @@ -303,76 +303,46 @@ Dygraph.hsvToRGB = function (hue, saturation, value) { // ... 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}; }; /** @@ -1170,8 +1140,9 @@ Dygraph.IFrameTarp.prototype.cover = function() { 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; diff --git a/dygraph.js b/dygraph.js index eb998ef..458760d 100644 --- a/dygraph.js +++ b/dygraph.js @@ -1339,8 +1339,9 @@ Dygraph.prototype.createDragInterface_ = function() { 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; @@ -1693,8 +1694,9 @@ Dygraph.prototype.eventToDomCoords = function(event) { 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]; } }; diff --git a/gallery/drawing.js b/gallery/drawing.js index 6443cf4..b25341b 100644 --- a/gallery/drawing.js +++ b/gallery/drawing.js @@ -37,8 +37,9 @@ Gallery.register( 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(); @@ -179,4 +180,4 @@ Gallery.register( }); window.onmouseup = finishDraw; } - }); \ No newline at end of file + }); diff --git a/gallery/interaction-api.js b/gallery/interaction-api.js index 08efc6f..4b85211 100644 --- a/gallery/interaction-api.js +++ b/gallery/interaction-api.js @@ -149,8 +149,9 @@ function moveV4(event, g, context) { var RANGE = 7; if (v4Active) { - 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 rows = g.numRows(); // Row layout: diff --git a/tests/interaction.js b/tests/interaction.js index 08efc6f..4b85211 100644 --- a/tests/interaction.js +++ b/tests/interaction.js @@ -149,8 +149,9 @@ function moveV4(event, g, context) { var RANGE = 7; if (v4Active) { - 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 rows = g.numRows(); // Row layout: -- 2.7.4