code review changes
[dygraphs.git] / dygraph-interaction-model.js
CommitLineData
88e95c46
DV
1/**
2 * @license
3 * Copyright 2011 Robert Konigsberg (konigsberg@google.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
5 */
846f3d2d 6
88e95c46 7/**
846f3d2d
DV
8 * @fileoverview The default interaction model for Dygraphs. This is kept out
9 * of dygraph.js for better navigability.
10 * @author Robert Konigsberg (konigsberg@google.com)
11 */
12
758a629f
DV
13/*jshint globalstrict: true */
14/*global Dygraph:false */
c0f54d4f
DV
15"use strict";
16
846f3d2d
DV
17/**
18 * A collection of functions to facilitate build custom interaction models.
19 * @class
20 */
21Dygraph.Interaction = {};
22
23/**
24 * Called in response to an interaction model operation that
25 * should start the default panning behavior.
26 *
27 * It's used in the default callback for "mousedown" operations.
28 * Custom interaction model builders can use it to provide the default
29 * panning behavior.
30 *
d67a4279
DV
31 * @param {Event} event the event object which led to the startPan call.
32 * @param {Dygraph} g The dygraph on which to act.
33 * @param {Object} context The dragging context object (with
34 * dragStartX/dragStartY/etc. properties). This function modifies the
35 * context.
846f3d2d
DV
36 */
37Dygraph.Interaction.startPan = function(event, g, context) {
758a629f 38 var i, axis;
846f3d2d
DV
39 context.isPanning = true;
40 var xRange = g.xAxisRange();
5b9b2142 41
5ec8b8ae 42 if (g.getOptionForAxis("logscale", "x")) {
5b9b2142
RK
43 context.initialLeftmostDate = Dygraph.log10(xRange[0]);
44 context.dateRange = Dygraph.log10(xRange[1]) - Dygraph.log10(xRange[0]);
45 } else {
46 context.initialLeftmostDate = xRange[0];
47 context.dateRange = xRange[1] - xRange[0];
48 }
846f3d2d
DV
49 context.xUnitsPerPixel = context.dateRange / (g.plotter_.area.w - 1);
50
b0963cdb
DV
51 if (g.getNumericOption("panEdgeFraction")) {
52 var maxXPixelsToDraw = g.width_ * g.getNumericOption("panEdgeFraction");
846f3d2d
DV
53 var xExtremes = g.xAxisExtremes(); // I REALLY WANT TO CALL THIS xTremes!
54
55 var boundedLeftX = g.toDomXCoord(xExtremes[0]) - maxXPixelsToDraw;
56 var boundedRightX = g.toDomXCoord(xExtremes[1]) + maxXPixelsToDraw;
57
58 var boundedLeftDate = g.toDataXCoord(boundedLeftX);
59 var boundedRightDate = g.toDataXCoord(boundedRightX);
60 context.boundedDates = [boundedLeftDate, boundedRightDate];
61
62 var boundedValues = [];
b0963cdb 63 var maxYPixelsToDraw = g.height_ * g.getNumericOption("panEdgeFraction");
846f3d2d 64
758a629f
DV
65 for (i = 0; i < g.axes_.length; i++) {
66 axis = g.axes_[i];
846f3d2d
DV
67 var yExtremes = axis.extremeRange;
68
69 var boundedTopY = g.toDomYCoord(yExtremes[0], i) + maxYPixelsToDraw;
70 var boundedBottomY = g.toDomYCoord(yExtremes[1], i) - maxYPixelsToDraw;
71
f93af875
RK
72 var boundedTopValue = g.toDataYCoord(boundedTopY, i);
73 var boundedBottomValue = g.toDataYCoord(boundedBottomY, i);
846f3d2d
DV
74
75 boundedValues[i] = [boundedTopValue, boundedBottomValue];
76 }
77 context.boundedValues = boundedValues;
78 }
79
80 // Record the range of each y-axis at the start of the drag.
81 // If any axis has a valueRange or valueWindow, then we want a 2D pan.
97583b90
DV
82 // We can't store data directly in g.axes_, because it does not belong to us
83 // and could change out from under us during a pan (say if there's a data
84 // update).
846f3d2d 85 context.is2DPan = false;
97583b90 86 context.axes = [];
758a629f
DV
87 for (i = 0; i < g.axes_.length; i++) {
88 axis = g.axes_[i];
97583b90 89 var axis_data = {};
846f3d2d
DV
90 var yRange = g.yAxisRange(i);
91 // TODO(konigsberg): These values should be in |context|.
92 // In log scale, initialTopValue, dragValueRange and unitsPerPixel are log scale.
1ca77443
RK
93 var logscale = g.attributes_.getForAxis("logscale", i);
94 if (logscale) {
97583b90
DV
95 axis_data.initialTopValue = Dygraph.log10(yRange[1]);
96 axis_data.dragValueRange = Dygraph.log10(yRange[1]) - Dygraph.log10(yRange[0]);
846f3d2d 97 } else {
97583b90
DV
98 axis_data.initialTopValue = yRange[1];
99 axis_data.dragValueRange = yRange[1] - yRange[0];
846f3d2d 100 }
97583b90
DV
101 axis_data.unitsPerPixel = axis_data.dragValueRange / (g.plotter_.area.h - 1);
102 context.axes.push(axis_data);
846f3d2d
DV
103
104 // While calculating axes, set 2dpan.
105 if (axis.valueWindow || axis.valueRange) context.is2DPan = true;
106 }
107};
108
109/**
110 * Called in response to an interaction model operation that
111 * responds to an event that pans the view.
112 *
113 * It's used in the default callback for "mousemove" operations.
114 * Custom interaction model builders can use it to provide the default
115 * panning behavior.
116 *
d67a4279
DV
117 * @param {Event} event the event object which led to the movePan call.
118 * @param {Dygraph} g The dygraph on which to act.
119 * @param {Object} context The dragging context object (with
120 * dragStartX/dragStartY/etc. properties). This function modifies the
121 * context.
846f3d2d
DV
122 */
123Dygraph.Interaction.movePan = function(event, g, context) {
806f92c1
DV
124 context.dragEndX = Dygraph.dragGetX_(event, context);
125 context.dragEndY = Dygraph.dragGetY_(event, context);
846f3d2d
DV
126
127 var minDate = context.initialLeftmostDate -
128 (context.dragEndX - context.dragStartX) * context.xUnitsPerPixel;
129 if (context.boundedDates) {
130 minDate = Math.max(minDate, context.boundedDates[0]);
131 }
132 var maxDate = minDate + context.dateRange;
133 if (context.boundedDates) {
134 if (maxDate > context.boundedDates[1]) {
135 // Adjust minDate, and recompute maxDate.
136 minDate = minDate - (maxDate - context.boundedDates[1]);
137 maxDate = minDate + context.dateRange;
138 }
139 }
140
5ec8b8ae 141 if (g.getOptionForAxis("logscale", "x")) {
5b9b2142
RK
142 g.dateWindow_ = [ Math.pow(Dygraph.LOG_SCALE, minDate),
143 Math.pow(Dygraph.LOG_SCALE, maxDate) ];
144 } else {
145 g.dateWindow_ = [minDate, maxDate];
146 }
846f3d2d
DV
147
148 // y-axis scaling is automatic unless this is a full 2D pan.
149 if (context.is2DPan) {
f93af875
RK
150
151 var pixelsDragged = context.dragEndY - context.dragStartY;
152
846f3d2d
DV
153 // Adjust each axis appropriately.
154 for (var i = 0; i < g.axes_.length; i++) {
155 var axis = g.axes_[i];
97583b90 156 var axis_data = context.axes[i];
97583b90 157 var unitsDragged = pixelsDragged * axis_data.unitsPerPixel;
920208fb 158
846f3d2d
DV
159 var boundedValue = context.boundedValues ? context.boundedValues[i] : null;
160
161 // In log scale, maxValue and minValue are the logs of those values.
97583b90 162 var maxValue = axis_data.initialTopValue + unitsDragged;
846f3d2d
DV
163 if (boundedValue) {
164 maxValue = Math.min(maxValue, boundedValue[1]);
165 }
97583b90 166 var minValue = maxValue - axis_data.dragValueRange;
846f3d2d
DV
167 if (boundedValue) {
168 if (minValue < boundedValue[0]) {
169 // Adjust maxValue, and recompute minValue.
170 maxValue = maxValue - (minValue - boundedValue[0]);
97583b90 171 minValue = maxValue - axis_data.dragValueRange;
846f3d2d
DV
172 }
173 }
5b9b2142 174 if (g.attributes_.getForAxis("logscale", i)) {
846f3d2d
DV
175 axis.valueWindow = [ Math.pow(Dygraph.LOG_SCALE, minValue),
176 Math.pow(Dygraph.LOG_SCALE, maxValue) ];
177 } else {
178 axis.valueWindow = [ minValue, maxValue ];
179 }
180 }
181 }
182
183 g.drawGraph_(false);
184};
185
186/**
187 * Called in response to an interaction model operation that
188 * responds to an event that ends panning.
189 *
190 * It's used in the default callback for "mouseup" operations.
191 * Custom interaction model builders can use it to provide the default
192 * panning behavior.
193 *
d67a4279
DV
194 * @param {Event} event the event object which led to the endPan call.
195 * @param {Dygraph} g The dygraph on which to act.
196 * @param {Object} context The dragging context object (with
197 * dragStartX/dragStartY/etc. properties). This function modifies the
198 * context.
846f3d2d
DV
199 */
200Dygraph.Interaction.endPan = function(event, g, context) {
806f92c1
DV
201 context.dragEndX = Dygraph.dragGetX_(event, context);
202 context.dragEndY = Dygraph.dragGetY_(event, context);
846f3d2d
DV
203
204 var regionWidth = Math.abs(context.dragEndX - context.dragStartX);
205 var regionHeight = Math.abs(context.dragEndY - context.dragStartY);
206
207 if (regionWidth < 2 && regionHeight < 2 &&
758a629f 208 g.lastx_ !== undefined && g.lastx_ != -1) {
846f3d2d
DV
209 Dygraph.Interaction.treatMouseOpAsClick(g, event, context);
210 }
211
846f3d2d
DV
212 // TODO(konigsberg): mouseup should just delete the
213 // context object, and mousedown should create a new one.
214 context.isPanning = false;
215 context.is2DPan = false;
216 context.initialLeftmostDate = null;
217 context.dateRange = null;
218 context.valueRange = null;
219 context.boundedDates = null;
220 context.boundedValues = null;
97583b90 221 context.axes = null;
846f3d2d
DV
222};
223
224/**
225 * Called in response to an interaction model operation that
226 * responds to an event that starts zooming.
227 *
228 * It's used in the default callback for "mousedown" operations.
229 * Custom interaction model builders can use it to provide the default
230 * zooming behavior.
231 *
d67a4279
DV
232 * @param {Event} event the event object which led to the startZoom call.
233 * @param {Dygraph} g The dygraph on which to act.
234 * @param {Object} context The dragging context object (with
235 * dragStartX/dragStartY/etc. properties). This function modifies the
236 * context.
846f3d2d
DV
237 */
238Dygraph.Interaction.startZoom = function(event, g, context) {
239 context.isZooming = true;
214083b4 240 context.zoomMoved = false;
846f3d2d
DV
241};
242
243/**
244 * Called in response to an interaction model operation that
245 * responds to an event that defines zoom boundaries.
246 *
247 * It's used in the default callback for "mousemove" operations.
248 * Custom interaction model builders can use it to provide the default
249 * zooming behavior.
250 *
d67a4279
DV
251 * @param {Event} event the event object which led to the moveZoom call.
252 * @param {Dygraph} g The dygraph on which to act.
253 * @param {Object} context The dragging context object (with
254 * dragStartX/dragStartY/etc. properties). This function modifies the
255 * context.
846f3d2d
DV
256 */
257Dygraph.Interaction.moveZoom = function(event, g, context) {
214083b4 258 context.zoomMoved = true;
806f92c1
DV
259 context.dragEndX = Dygraph.dragGetX_(event, context);
260 context.dragEndY = Dygraph.dragGetY_(event, context);
846f3d2d
DV
261
262 var xDelta = Math.abs(context.dragStartX - context.dragEndX);
263 var yDelta = Math.abs(context.dragStartY - context.dragEndY);
264
265 // drag direction threshold for y axis is twice as large as x axis
266 context.dragDirection = (xDelta < yDelta / 2) ? Dygraph.VERTICAL : Dygraph.HORIZONTAL;
267
268 g.drawZoomRect_(
269 context.dragDirection,
270 context.dragStartX,
271 context.dragEndX,
272 context.dragStartY,
273 context.dragEndY,
274 context.prevDragDirection,
275 context.prevEndX,
276 context.prevEndY);
277
278 context.prevEndX = context.dragEndX;
279 context.prevEndY = context.dragEndY;
280 context.prevDragDirection = context.dragDirection;
281};
282
d67a4279 283/**
6f5f0b2b 284 * TODO(danvk): move this logic into dygraph.js
d67a4279
DV
285 * @param {Dygraph} g
286 * @param {Event} event
287 * @param {Object} context
288 */
846f3d2d 289Dygraph.Interaction.treatMouseOpAsClick = function(g, event, context) {
b0963cdb
DV
290 var clickCallback = g.getFunctionOption('clickCallback');
291 var pointClickCallback = g.getFunctionOption('pointClickCallback');
846f3d2d
DV
292
293 var selectedPoint = null;
294
6f5f0b2b
DV
295 // Find out if the click occurs on a point.
296 var closestIdx = -1;
297 var closestDistance = Number.MAX_VALUE;
298
299 // check if the click was on a particular point.
300 for (var i = 0; i < g.selPoints_.length; i++) {
301 var p = g.selPoints_[i];
302 var distance = Math.pow(p.canvasx - context.dragEndX, 2) +
303 Math.pow(p.canvasy - context.dragEndY, 2);
304 if (!isNaN(distance) &&
305 (closestIdx == -1 || distance < closestDistance)) {
306 closestDistance = distance;
307 closestIdx = i;
846f3d2d 308 }
6f5f0b2b 309 }
846f3d2d 310
6f5f0b2b
DV
311 // Allow any click within two pixels of the dot.
312 var radius = g.getNumericOption('highlightCircleSize') + 2;
313 if (closestDistance <= radius * radius) {
314 selectedPoint = g.selPoints_[closestIdx];
846f3d2d
DV
315 }
316
317 if (selectedPoint) {
6f5f0b2b
DV
318 var e = {
319 cancelable: true,
320 point: selectedPoint,
321 canvasx: context.dragEndX,
322 canvasy: context.dragEndY
323 };
324 var defaultPrevented = g.cascadeEvents_('pointClick', e);
325 if (defaultPrevented) {
326 // Note: this also prevents click / clickCallback from firing.
327 return;
328 }
329 if (pointClickCallback) {
330 pointClickCallback.call(g, event, selectedPoint);
331 }
846f3d2d
DV
332 }
333
6f5f0b2b
DV
334 var e = {
335 xval: g.lastx_, // closest point by x value
336 pts: g.selPoints_,
337 canvasx: context.dragEndX,
338 canvasy: context.dragEndY
339 };
340 if (!g.cascadeEvents_('click', e)) {
341 if (clickCallback) {
342 // TODO(danvk): pass along more info about the points, e.g. 'x'
343 clickCallback.call(g, event, g.lastx_, g.selPoints_);
344 }
846f3d2d
DV
345 }
346};
347
348/**
349 * Called in response to an interaction model operation that
350 * responds to an event that performs a zoom based on previously defined
351 * bounds..
352 *
353 * It's used in the default callback for "mouseup" operations.
354 * Custom interaction model builders can use it to provide the default
355 * zooming behavior.
356 *
d67a4279
DV
357 * @param {Event} event the event object which led to the endZoom call.
358 * @param {Dygraph} g The dygraph on which to end the zoom.
359 * @param {Object} context The dragging context object (with
360 * dragStartX/dragStartY/etc. properties). This function modifies the
361 * context.
846f3d2d
DV
362 */
363Dygraph.Interaction.endZoom = function(event, g, context) {
364 context.isZooming = false;
806f92c1
DV
365 context.dragEndX = Dygraph.dragGetX_(event, context);
366 context.dragEndY = Dygraph.dragGetY_(event, context);
846f3d2d
DV
367 var regionWidth = Math.abs(context.dragEndX - context.dragStartX);
368 var regionHeight = Math.abs(context.dragEndY - context.dragStartY);
369
370 if (regionWidth < 2 && regionHeight < 2 &&
758a629f 371 g.lastx_ !== undefined && g.lastx_ != -1) {
846f3d2d
DV
372 Dygraph.Interaction.treatMouseOpAsClick(g, event, context);
373 }
374
b04888cc
DV
375 // The zoom rectangle is visibly clipped to the plot area, so its behavior
376 // should be as well.
377 // See http://code.google.com/p/dygraphs/issues/detail?id=280
378 var plotArea = g.getArea();
846f3d2d 379 if (regionWidth >= 10 && context.dragDirection == Dygraph.HORIZONTAL) {
b04888cc
DV
380 var left = Math.min(context.dragStartX, context.dragEndX),
381 right = Math.max(context.dragStartX, context.dragEndX);
5ee26cc1
DV
382 left = Math.max(left, plotArea.x);
383 right = Math.min(right, plotArea.x + plotArea.w);
384 if (left < right) {
385 g.doZoomX_(left, right);
386 }
421f1773 387 context.cancelNextDblclick = true;
846f3d2d 388 } else if (regionHeight >= 10 && context.dragDirection == Dygraph.VERTICAL) {
b04888cc
DV
389 var top = Math.min(context.dragStartY, context.dragEndY),
390 bottom = Math.max(context.dragStartY, context.dragEndY);
5ee26cc1
DV
391 top = Math.max(top, plotArea.y);
392 bottom = Math.min(bottom, plotArea.y + plotArea.h);
393 if (top < bottom) {
394 g.doZoomY_(top, bottom);
395 }
421f1773 396 context.cancelNextDblclick = true;
846f3d2d 397 } else {
42b3b41f 398 if (context.zoomMoved) g.clearZoomRect_();
846f3d2d
DV
399 }
400 context.dragStartX = null;
401 context.dragStartY = null;
402};
403
404/**
6125fedf
DV
405 * @private
406 */
407Dygraph.Interaction.startTouch = function(event, g, context) {
408 event.preventDefault(); // touch browsers are all nice.
b19d7411
DV
409 if (event.touches.length > 1) {
410 // If the user ever puts two fingers down, it's not a double tap.
411 context.startTimeForDoubleTapMs = null;
412 }
413
6125fedf
DV
414 var touches = [];
415 for (var i = 0; i < event.touches.length; i++) {
416 var t = event.touches[i];
417 // we dispense with 'dragGetX_' because all touchBrowsers support pageX
418 touches.push({
419 pageX: t.pageX,
420 pageY: t.pageY,
421 dataX: g.toDataXCoord(t.pageX),
422 dataY: g.toDataYCoord(t.pageY)
423 // identifier: t.identifier
424 });
425 }
426 context.initialTouches = touches;
427
428 if (touches.length == 1) {
429 // This is just a swipe.
430 context.initialPinchCenter = touches[0];
431 context.touchDirections = { x: true, y: true };
48cdd6c4 432 } else if (touches.length >= 2) {
6125fedf 433 // It's become a pinch!
48cdd6c4 434 // In case there are 3+ touches, we ignore all but the "first" two.
6125fedf
DV
435
436 // only screen coordinates can be averaged (data coords could be log scale).
437 context.initialPinchCenter = {
438 pageX: 0.5 * (touches[0].pageX + touches[1].pageX),
439 pageY: 0.5 * (touches[0].pageY + touches[1].pageY),
440
441 // TODO(danvk): remove
442 dataX: 0.5 * (touches[0].dataX + touches[1].dataX),
e446c952 443 dataY: 0.5 * (touches[0].dataY + touches[1].dataY)
6125fedf
DV
444 };
445
446 // Make pinches in a 45-degree swath around either axis 1-dimensional zooms.
447 var initialAngle = 180 / Math.PI * Math.atan2(
448 context.initialPinchCenter.pageY - touches[0].pageY,
449 touches[0].pageX - context.initialPinchCenter.pageX);
450
451 // use symmetry to get it into the first quadrant.
452 initialAngle = Math.abs(initialAngle);
453 if (initialAngle > 90) initialAngle = 90 - initialAngle;
454
455 context.touchDirections = {
456 x: (initialAngle < (90 - 45/2)),
457 y: (initialAngle > 45/2)
458 };
459 }
460
461 // save the full x & y ranges.
462 context.initialRange = {
463 x: g.xAxisRange(),
464 y: g.yAxisRange()
465 };
466};
467
468/**
469 * @private
470 */
471Dygraph.Interaction.moveTouch = function(event, g, context) {
b19d7411
DV
472 // If the tap moves, then it's definitely not part of a double-tap.
473 context.startTimeForDoubleTapMs = null;
474
e446c952
DV
475 var i, touches = [];
476 for (i = 0; i < event.touches.length; i++) {
6125fedf
DV
477 var t = event.touches[i];
478 touches.push({
479 pageX: t.pageX,
e446c952 480 pageY: t.pageY
6125fedf
DV
481 });
482 }
483 var initialTouches = context.initialTouches;
484
485 var c_now;
486
487 // old and new centers.
488 var c_init = context.initialPinchCenter;
489 if (touches.length == 1) {
490 c_now = touches[0];
491 } else {
492 c_now = {
493 pageX: 0.5 * (touches[0].pageX + touches[1].pageX),
494 pageY: 0.5 * (touches[0].pageY + touches[1].pageY)
495 };
496 }
497
498 // this is the "swipe" component
499 // we toss it out for now, but could use it in the future.
500 var swipe = {
501 pageX: c_now.pageX - c_init.pageX,
e446c952 502 pageY: c_now.pageY - c_init.pageY
6125fedf
DV
503 };
504 var dataWidth = context.initialRange.x[1] - context.initialRange.x[0];
505 var dataHeight = context.initialRange.y[0] - context.initialRange.y[1];
506 swipe.dataX = (swipe.pageX / g.plotter_.area.w) * dataWidth;
507 swipe.dataY = (swipe.pageY / g.plotter_.area.h) * dataHeight;
508 var xScale, yScale;
509
510 // The residual bits are usually split into scale & rotate bits, but we split
511 // them into x-scale and y-scale bits.
512 if (touches.length == 1) {
513 xScale = 1.0;
514 yScale = 1.0;
48cdd6c4 515 } else if (touches.length >= 2) {
6125fedf
DV
516 var initHalfWidth = (initialTouches[1].pageX - c_init.pageX);
517 xScale = (touches[1].pageX - c_now.pageX) / initHalfWidth;
518
519 var initHalfHeight = (initialTouches[1].pageY - c_init.pageY);
520 yScale = (touches[1].pageY - c_now.pageY) / initHalfHeight;
521 }
522
523 // Clip scaling to [1/8, 8] to prevent too much blowup.
524 xScale = Math.min(8, Math.max(0.125, xScale));
525 yScale = Math.min(8, Math.max(0.125, yScale));
526
b38afae5 527 var didZoom = false;
6125fedf
DV
528 if (context.touchDirections.x) {
529 g.dateWindow_ = [
530 c_init.dataX - swipe.dataX + (context.initialRange.x[0] - c_init.dataX) / xScale,
e446c952 531 c_init.dataX - swipe.dataX + (context.initialRange.x[1] - c_init.dataX) / xScale
6125fedf 532 ];
b38afae5 533 didZoom = true;
6125fedf
DV
534 }
535
536 if (context.touchDirections.y) {
e446c952 537 for (i = 0; i < 1 /*g.axes_.length*/; i++) {
6125fedf 538 var axis = g.axes_[i];
1ca77443
RK
539 var logscale = g.attributes_.getForAxis("logscale", i);
540 if (logscale) {
6125fedf
DV
541 // TODO(danvk): implement
542 } else {
543 axis.valueWindow = [
544 c_init.dataY - swipe.dataY + (context.initialRange.y[0] - c_init.dataY) / yScale,
e446c952 545 c_init.dataY - swipe.dataY + (context.initialRange.y[1] - c_init.dataY) / yScale
6125fedf 546 ];
b38afae5 547 didZoom = true;
6125fedf
DV
548 }
549 }
550 }
551
552 g.drawGraph_(false);
b38afae5
DV
553
554 // We only call zoomCallback on zooms, not pans, to mirror desktop behavior.
b0963cdb 555 if (didZoom && touches.length > 1 && g.getFunctionOption('zoomCallback')) {
b38afae5 556 var viewWindow = g.xAxisRange();
4ee251cb 557 g.getFunctionOption("zoomCallback").call(g, viewWindow[0], viewWindow[1], g.yAxisRanges());
b38afae5 558 }
6125fedf
DV
559};
560
561/**
562 * @private
563 */
564Dygraph.Interaction.endTouch = function(event, g, context) {
42a9ebb8 565 if (event.touches.length !== 0) {
6125fedf
DV
566 // this is effectively a "reset"
567 Dygraph.Interaction.startTouch(event, g, context);
b19d7411
DV
568 } else if (event.changedTouches.length == 1) {
569 // Could be part of a "double tap"
570 // The heuristic here is that it's a double-tap if the two touchend events
571 // occur within 500ms and within a 50x50 pixel box.
572 var now = new Date().getTime();
573 var t = event.changedTouches[0];
574 if (context.startTimeForDoubleTapMs &&
575 now - context.startTimeForDoubleTapMs < 500 &&
576 context.doubleTapX && Math.abs(context.doubleTapX - t.screenX) < 50 &&
577 context.doubleTapY && Math.abs(context.doubleTapY - t.screenY) < 50) {
578 g.resetZoom();
579 } else {
580 context.startTimeForDoubleTapMs = now;
581 context.doubleTapX = t.screenX;
582 context.doubleTapY = t.screenY;
583 }
6125fedf
DV
584 }
585};
586
587/**
846f3d2d
DV
588 * Default interation model for dygraphs. You can refer to specific elements of
589 * this when constructing your own interaction model, e.g.:
590 * g.updateOptions( {
591 * interactionModel: {
592 * mousedown: Dygraph.defaultInteractionModel.mousedown
593 * }
594 * } );
595 */
596Dygraph.Interaction.defaultModel = {
597 // Track the beginning of drag events
598 mousedown: function(event, g, context) {
e992d194
DV
599 // Right-click should not initiate a zoom.
600 if (event.button && event.button == 2) return;
601
846f3d2d
DV
602 context.initializeMouseDown(event, g, context);
603
604 if (event.altKey || event.shiftKey) {
605 Dygraph.startPan(event, g, context);
606 } else {
607 Dygraph.startZoom(event, g, context);
608 }
609 },
610
611 // Draw zoom rectangles when the mouse is down and the user moves around
612 mousemove: function(event, g, context) {
613 if (context.isZooming) {
614 Dygraph.moveZoom(event, g, context);
615 } else if (context.isPanning) {
616 Dygraph.movePan(event, g, context);
617 }
618 },
619
620 mouseup: function(event, g, context) {
621 if (context.isZooming) {
622 Dygraph.endZoom(event, g, context);
623 } else if (context.isPanning) {
624 Dygraph.endPan(event, g, context);
625 }
626 },
627
6125fedf
DV
628 touchstart: function(event, g, context) {
629 Dygraph.Interaction.startTouch(event, g, context);
630 },
631 touchmove: function(event, g, context) {
632 Dygraph.Interaction.moveTouch(event, g, context);
633 },
634 touchend: function(event, g, context) {
635 Dygraph.Interaction.endTouch(event, g, context);
636 },
637
846f3d2d
DV
638 // Temporarily cancel the dragging event when the mouse leaves the graph
639 mouseout: function(event, g, context) {
640 if (context.isZooming) {
641 context.dragEndX = null;
642 context.dragEndY = null;
b04888cc 643 g.clearZoomRect_();
846f3d2d
DV
644 }
645 },
646
647 // Disable zooming out if panning.
648 dblclick: function(event, g, context) {
421f1773
DV
649 if (context.cancelNextDblclick) {
650 context.cancelNextDblclick = false;
651 return;
652 }
6f5f0b2b
DV
653
654 // Give plugins a chance to grab this event.
655 var e = {
656 canvasx: context.dragEndX,
657 canvasy: context.dragEndY
658 };
659 if (g.cascadeEvents_('dblclick', e)) {
660 return;
661 }
662
846f3d2d
DV
663 if (event.altKey || event.shiftKey) {
664 return;
665 }
e4f6e11a 666 g.resetZoom();
846f3d2d
DV
667 }
668};
669
670Dygraph.DEFAULT_ATTRS.interactionModel = Dygraph.Interaction.defaultModel;
671
672// old ways of accessing these methods/properties
673Dygraph.defaultInteractionModel = Dygraph.Interaction.defaultModel;
674Dygraph.endZoom = Dygraph.Interaction.endZoom;
675Dygraph.moveZoom = Dygraph.Interaction.moveZoom;
676Dygraph.startZoom = Dygraph.Interaction.startZoom;
677Dygraph.endPan = Dygraph.Interaction.endPan;
678Dygraph.movePan = Dygraph.Interaction.movePan;
679Dygraph.startPan = Dygraph.Interaction.startPan;
680
0290d079 681Dygraph.Interaction.nonInteractiveModel_ = {
027e9e9b
DV
682 mousedown: function(event, g, context) {
683 context.initializeMouseDown(event, g, context);
684 },
685 mouseup: function(event, g, context) {
686 // TODO(danvk): this logic is repeated in Dygraph.Interaction.endZoom
806f92c1
DV
687 context.dragEndX = Dygraph.dragGetX_(event, context);
688 context.dragEndY = Dygraph.dragGetY_(event, context);
027e9e9b
DV
689 var regionWidth = Math.abs(context.dragEndX - context.dragStartX);
690 var regionHeight = Math.abs(context.dragEndY - context.dragStartY);
691
692 if (regionWidth < 2 && regionHeight < 2 &&
758a629f 693 g.lastx_ !== undefined && g.lastx_ != -1) {
027e9e9b
DV
694 Dygraph.Interaction.treatMouseOpAsClick(g, event, context);
695 }
696 }
697};
3417f017
DV
698
699// Default interaction model when using the range selector.
700Dygraph.Interaction.dragIsPanInteractionModel = {
701 mousedown: function(event, g, context) {
702 context.initializeMouseDown(event, g, context);
703 Dygraph.startPan(event, g, context);
704 },
705 mousemove: function(event, g, context) {
706 if (context.isPanning) {
707 Dygraph.movePan(event, g, context);
708 }
709 },
710 mouseup: function(event, g, context) {
711 if (context.isPanning) {
712 Dygraph.endPan(event, g, context);
713 }
714 }
715};