Add Dygraph.Interaction.nonInteractiveModel. This disables pan/zoom interactions...
authorDan Vanderkam <danvk@google.com>
Tue, 12 Jul 2011 19:55:55 +0000 (15:55 -0400)
committerDan Vanderkam <danvk@google.com>
Tue, 12 Jul 2011 19:55:55 +0000 (15:55 -0400)
auto_tests/tests/interaction_model.js
dygraph-interaction-model.js
tests/interaction.html

index 9147f1b..5d3c435 100644 (file)
@@ -136,6 +136,34 @@ InteractionModelTestCase.clickAt = function(g, x, y) {
 }
 
 /**
+ * This tests that clickCallback is still called with the nonInteractiveModel.
+ */
+InteractionModelTestCase.prototype.testClickCallbackIsCalledWithNonInteractiveModel = function() {
+  var clicked;
+
+  // TODO(danvk): also test pointClickCallback here.
+  var clickCallback = function(event, x) {
+    clicked = x;
+  };
+
+  var opts = {
+    width: 100,
+    height : 100,
+    clickCallback : clickCallback,
+    interactionModel : {}
+  };
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, data1, opts);
+
+  DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+  DygraphOps.dispatchMouseMove_Point(g, 10, 10);
+  DygraphOps.dispatchMouseUp_Point(g, 10, 10);
+
+  assertEquals(20, clicked);
+};
+
+/**
  * A sanity test to ensure pointClickCallback is called.
  */
 InteractionModelTestCase.prototype.testPointClickCallback = function() {
index 42f0eaf..645c258 100644 (file)
@@ -393,3 +393,20 @@ Dygraph.endPan = Dygraph.Interaction.endPan;
 Dygraph.movePan = Dygraph.Interaction.movePan;
 Dygraph.startPan = Dygraph.Interaction.startPan;
 
+Dygraph.Interaction.nonInteractiveModel = {
+  mousedown: function(event, g, context) {
+    context.initializeMouseDown(event, g, context);
+  },
+  mouseup: function(event, g, context) {
+    // TODO(danvk): this logic is repeated in Dygraph.Interaction.endZoom
+    context.dragEndX = g.dragGetX_(event, context);
+    context.dragEndY = g.dragGetY_(event, context);
+    var regionWidth = Math.abs(context.dragEndX - context.dragStartX);
+    var regionHeight = Math.abs(context.dragEndY - context.dragStartY);
+
+    if (regionWidth < 2 && regionHeight < 2 &&
+        g.lastx_ != undefined && g.lastx_ != -1) {
+      Dygraph.Interaction.treatMouseOpAsClick(g, event, context);
+    }
+  }
+};
index 8389ae7..58f3dac 100644 (file)
@@ -25,7 +25,9 @@
     <tr><td>
     <b>No interaction model</b>
     <div id="div_g2" style="width:600px; height:300px;"></div>
-    </td><td>Click and drag all you like, it won't do anything!</td></tr>
+    </td><td>Click and drag all you like, it won't do anything!
+    <div id="g2_console"></div>
+    </td></tr>
     <tr><td>
     <b>Custom interaction model</b>
 
       Dygraph.addEvent(document, "click", function() { lastClickedGraph = null; });
       var g = new Dygraph(document.getElementById("div_g"),
            NoisyData, { errorBars : true });
+      var s = document.getElementById("g2_console");
       var g2 = new Dygraph(document.getElementById("div_g2"),
-           NoisyData, { errorBars : true, interactionModel : {} });
+           NoisyData,
+           {
+             errorBars : true,
+             interactionModel: Dygraph.Interaction.nonInteractiveModel,
+             clickCallback: function(e, x, pts) {
+               s.innerHTML += "<b>Click</b> " + pts[0].name + ": " + pts[0].x + "<br/>";
+             },
+             pointClickCallback: function(e, p) {
+               s.innerHTML += "<b>Point Click</b> " + p.name + ": " + p.x + "<br/>";
+             },
+           });
       var g3 = new Dygraph(document.getElementById("div_g3"),
            NoisyData, { errorBars : true, interactionModel : {
         'mousedown' : downV3,