X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;ds=sidebyside;f=dygraph-interaction-model.js;h=a3de6f76d96c8fe609bcbd37d57b3aff4484f906;hb=81cb07d653f397d31c063ff3688c5e682f1d6d82;hp=3029025b7198be989d5d049069811226b5323a62;hpb=88e95c462340958bdfd0ac4c0736b94c5fd21024;p=dygraphs.git diff --git a/dygraph-interaction-model.js b/dygraph-interaction-model.js index 3029025..a3de6f7 100644 --- a/dygraph-interaction-model.js +++ b/dygraph-interaction-model.js @@ -10,6 +10,9 @@ * @author Robert Konigsberg (konigsberg@google.com) */ +/*jshint globalstrict: true */ +/*global Dygraph:false */ +"use strict"; /** * A collection of functions to facilitate build custom interaction models. @@ -31,6 +34,7 @@ Dygraph.Interaction = {}; * dragStartX/dragStartY/etc. properties). This function modifies the context. */ Dygraph.Interaction.startPan = function(event, g, context) { + var i, axis; context.isPanning = true; var xRange = g.xAxisRange(); context.dateRange = xRange[1] - xRange[0]; @@ -51,8 +55,8 @@ Dygraph.Interaction.startPan = function(event, g, context) { var boundedValues = []; var maxYPixelsToDraw = g.height_ * g.attr_("panEdgeFraction"); - for (var i = 0; i < g.axes_.length; i++) { - var axis = g.axes_[i]; + for (i = 0; i < g.axes_.length; i++) { + axis = g.axes_[i]; var yExtremes = axis.extremeRange; var boundedTopY = g.toDomYCoord(yExtremes[0], i) + maxYPixelsToDraw; @@ -69,8 +73,8 @@ Dygraph.Interaction.startPan = function(event, g, context) { // Record the range of each y-axis at the start of the drag. // If any axis has a valueRange or valueWindow, then we want a 2D pan. context.is2DPan = false; - for (var i = 0; i < g.axes_.length; i++) { - var axis = g.axes_[i]; + for (i = 0; i < g.axes_.length; i++) { + axis = g.axes_[i]; var yRange = g.yAxisRange(i); // TODO(konigsberg): These values should be in |context|. // In log scale, initialTopValue, dragValueRange and unitsPerPixel are log scale. @@ -129,7 +133,7 @@ Dygraph.Interaction.movePan = function(event, g, context) { var pixelsDragged = context.dragEndY - context.dragStartY; var unitsDragged = pixelsDragged * axis.unitsPerPixel; - + var boundedValue = context.boundedValues ? context.boundedValues[i] : null; // In log scale, maxValue and minValue are the logs of those values. @@ -178,7 +182,7 @@ Dygraph.Interaction.endPan = function(event, g, context) { var regionHeight = Math.abs(context.dragEndY - context.dragStartY); if (regionWidth < 2 && regionHeight < 2 && - g.lastx_ != undefined && g.lastx_ != -1) { + g.lastx_ !== undefined && g.lastx_ != -1) { Dygraph.Interaction.treatMouseOpAsClick(g, event, context); } @@ -266,7 +270,8 @@ Dygraph.Interaction.treatMouseOpAsClick = function(g, event, context) { var p = g.selPoints_[i]; var distance = Math.pow(p.canvasx - context.dragEndX, 2) + Math.pow(p.canvasy - context.dragEndY, 2); - if (closestIdx == -1 || distance < closestDistance) { + if (!isNaN(distance) && + (closestIdx == -1 || distance < closestDistance)) { closestDistance = distance; closestIdx = i; } @@ -311,7 +316,7 @@ Dygraph.Interaction.endZoom = function(event, g, context) { var regionHeight = Math.abs(context.dragEndY - context.dragStartY); if (regionWidth < 2 && regionHeight < 2 && - g.lastx_ != undefined && g.lastx_ != -1) { + g.lastx_ !== undefined && g.lastx_ != -1) { Dygraph.Interaction.treatMouseOpAsClick(g, event, context); } @@ -322,7 +327,7 @@ Dygraph.Interaction.endZoom = function(event, g, context) { g.doZoomY_(Math.min(context.dragStartY, context.dragEndY), Math.max(context.dragStartY, context.dragEndY)); } else { - g.canvas_ctx_.clearRect(0, 0, g.canvas_.width, g.canvas_.height); + g.clearZoomRect_(); } context.dragStartX = null; context.dragStartY = null; @@ -408,8 +413,26 @@ Dygraph.Interaction.nonInteractiveModel_ = { var regionHeight = Math.abs(context.dragEndY - context.dragStartY); if (regionWidth < 2 && regionHeight < 2 && - g.lastx_ != undefined && g.lastx_ != -1) { + g.lastx_ !== undefined && g.lastx_ != -1) { Dygraph.Interaction.treatMouseOpAsClick(g, event, context); } } }; + +// Default interaction model when using the range selector. +Dygraph.Interaction.dragIsPanInteractionModel = { + mousedown: function(event, g, context) { + context.initializeMouseDown(event, g, context); + Dygraph.startPan(event, g, context); + }, + mousemove: function(event, g, context) { + if (context.isPanning) { + Dygraph.movePan(event, g, context); + } + }, + mouseup: function(event, g, context) { + if (context.isPanning) { + Dygraph.endPan(event, g, context); + } + } +};