From 027e9e9b363e2330191001068818089477345b75 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Tue, 12 Jul 2011 15:55:55 -0400 Subject: [PATCH] Add Dygraph.Interaction.nonInteractiveModel. This disables pan/zoom interactions while preserving the expected behavior of clickCallback and pointClickCallback. Also includes a test and auto_test. --- auto_tests/tests/interaction_model.js | 28 ++++++++++++++++++++++++++++ dygraph-interaction-model.js | 17 +++++++++++++++++ tests/interaction.html | 17 +++++++++++++++-- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/auto_tests/tests/interaction_model.js b/auto_tests/tests/interaction_model.js index 9147f1b..5d3c435 100644 --- a/auto_tests/tests/interaction_model.js +++ b/auto_tests/tests/interaction_model.js @@ -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() { diff --git a/dygraph-interaction-model.js b/dygraph-interaction-model.js index 42f0eaf..645c258 100644 --- a/dygraph-interaction-model.js +++ b/dygraph-interaction-model.js @@ -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); + } + } +}; diff --git a/tests/interaction.html b/tests/interaction.html index 8389ae7..58f3dac 100644 --- a/tests/interaction.html +++ b/tests/interaction.html @@ -25,7 +25,9 @@ No interaction model
- Click and drag all you like, it won't do anything! + Click and drag all you like, it won't do anything! +
+ Custom interaction model @@ -53,8 +55,19 @@ 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 += "Click " + pts[0].name + ": " + pts[0].x + "
"; + }, + pointClickCallback: function(e, p) { + s.innerHTML += "Point Click " + p.name + ": " + p.x + "
"; + }, + }); var g3 = new Dygraph(document.getElementById("div_g3"), NoisyData, { errorBars : true, interactionModel : { 'mousedown' : downV3, -- 2.7.4