Fix two inconsistencies in dygraph-externs.js (#859)
[dygraphs.git] / gallery / interaction-api.js
index 38a5b68..8f6d706 100644 (file)
@@ -1,3 +1,4 @@
+/*global Dygraph */
 // Code for a variety of interaction models. Used in interaction.html, but split out from
 // that file so they can be tested in isolation.
 //
@@ -50,9 +51,9 @@ function offsetToPercentage(g, offsetX, offsetY) {
   var h = g.toDomCoords(null, yar0[0])[1] - yOffset;
 
   // Percentage from the left.
-  var xPct = w == 0 ? 0 : (x / w);
+  var xPct = w === 0 ? 0 : (x / w);
   // Percentage from the top.
-  var yPct = h == 0 ? 0 : (y / h);
+  var yPct = h === 0 ? 0 : (y / h);
 
   // The (1-) part below changes it from "% distance down from the top"
   // to "% distance up from the bottom".
@@ -62,14 +63,20 @@ function offsetToPercentage(g, offsetX, offsetY) {
 function dblClickV3(event, g, context) {
   // Reducing by 20% makes it 80% the original size, which means
   // to restore to original size it must grow by 25%
+
+  if (!(event.offsetX && event.offsetY)){
+    event.offsetX = event.layerX - event.target.offsetLeft;
+    event.offsetY = event.layerY - event.target.offsetTop;
+  }
+
   var percentages = offsetToPercentage(g, event.offsetX, event.offsetY);
   var xPct = percentages[0];
   var yPct = percentages[1];
 
   if (event.ctrlKey) {
-    zoom(g, -.25, xPct, yPct);
+    zoom(g, -0.25, xPct, yPct);
   } else {
-    zoom(g, +.2, xPct, yPct);
+    zoom(g, +0.2, xPct, yPct);
   }
 }
 
@@ -77,7 +84,7 @@ var lastClickedGraph = null;
 
 function clickV3(event, g, context) {
   lastClickedGraph = g;
-  Dygraph.cancelEvent(event);
+  event.preventDefault();;
 }
 
 function scrollV3(event, g, context) {
@@ -89,12 +96,17 @@ function scrollV3(event, g, context) {
   // that verbatim, it would be a 7.5%.
   var percentage = normal / 50;
 
+  if (!(event.offsetX && event.offsetY)){
+    event.offsetX = event.layerX - event.target.offsetLeft;
+    event.offsetY = event.layerY - event.target.offsetTop;
+  }
+
   var percentages = offsetToPercentage(g, event.offsetX, event.offsetY);
   var xPct = percentages[0];
   var yPct = percentages[1];
 
   zoom(g, percentage, xPct, yPct);
-  Dygraph.cancelEvent(event);
+  event.preventDefault();
 }
 
 // Adjusts [x, y] toward each other by zoomInPercentage%
@@ -138,8 +150,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:
@@ -152,7 +165,7 @@ function moveV4(event, g, context) {
         for (var col = 1; col < 3; col++) {
           // TODO(konigsberg): these will throw exceptions as data is removed.
           var vals =  g.getValue(row, col);
-          if (vals == null) { continue; }
+          if (vals === null || vals === undefined) { continue; }
           var val = vals[0];
           var y = g.toDomCoords(null, val)[1];
           var diff2 = Math.abs(canvasy - y);