MapPointer.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* *
  2. *
  3. * (c) 2010-2021 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Pointer from '../Core/Pointer.js';
  12. import U from '../Core/Utilities.js';
  13. var extend = U.extend, pick = U.pick, wrap = U.wrap;
  14. /* eslint-disable no-invalid-this */
  15. var totalWheelDelta = 0;
  16. var totalWheelDeltaTimer;
  17. // Extend the Pointer
  18. extend(Pointer.prototype, {
  19. // The event handler for the doubleclick event
  20. onContainerDblClick: function (e) {
  21. var chart = this.chart;
  22. e = this.normalize(e);
  23. if (chart.options.mapNavigation.enableDoubleClickZoomTo) {
  24. if (chart.pointer.inClass(e.target, 'highcharts-tracker') &&
  25. chart.hoverPoint) {
  26. chart.hoverPoint.zoomTo();
  27. }
  28. }
  29. else if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  30. chart.mapZoom(0.5, chart.xAxis[0].toValue(e.chartX), chart.yAxis[0].toValue(e.chartY), e.chartX, e.chartY);
  31. }
  32. },
  33. // The event handler for the mouse scroll event
  34. onContainerMouseWheel: function (e) {
  35. var chart = this.chart;
  36. e = this.normalize(e);
  37. // Firefox uses e.deltaY or e.detail, WebKit and IE uses wheelDelta
  38. var delta = e.deltaY || e.detail || -(e.wheelDelta / 120);
  39. // Wheel zooming on trackpads have different behaviours in Firefox vs
  40. // WebKit. In Firefox the delta increments in steps by 1, so it is not
  41. // distinguishable from true mouse wheel. Therefore we use this timer
  42. // to avoid trackpad zooming going too fast and out of control. In
  43. // WebKit however, the delta is < 1, so we simply disable animation in
  44. // the `chart.mapZoom` call below.
  45. if (Math.abs(delta) >= 1) {
  46. totalWheelDelta += Math.abs(delta);
  47. if (totalWheelDeltaTimer) {
  48. clearTimeout(totalWheelDeltaTimer);
  49. }
  50. totalWheelDeltaTimer = setTimeout(function () {
  51. totalWheelDelta = 0;
  52. }, 50);
  53. }
  54. if (totalWheelDelta < 10 && chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  55. chart.mapZoom(Math.pow(chart.options.mapNavigation.mouseWheelSensitivity, delta), chart.xAxis[0].toValue(e.chartX), chart.yAxis[0].toValue(e.chartY), e.chartX, e.chartY,
  56. // Delta less than 1 indicates stepless/trackpad zooming, avoid
  57. // animation delaying the zoom
  58. Math.abs(delta) < 1 ? false : void 0);
  59. }
  60. }
  61. });
  62. // The pinchType is inferred from mapNavigation options.
  63. wrap(Pointer.prototype, 'zoomOption', function (proceed) {
  64. var mapNavigation = this.chart.options.mapNavigation;
  65. // Pinch status
  66. if (pick(mapNavigation.enableTouchZoom, mapNavigation.enabled)) {
  67. this.chart.options.chart.pinchType = 'xy';
  68. }
  69. proceed.apply(this, [].slice.call(arguments, 1));
  70. });
  71. // Extend the pinchTranslate method to preserve fixed ratio when zooming
  72. wrap(Pointer.prototype, 'pinchTranslate', function (proceed, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch) {
  73. var xBigger;
  74. proceed.call(this, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  75. // Keep ratio
  76. if (this.chart.options.chart.type === 'map' && this.hasZoom) {
  77. xBigger = transform.scaleX > transform.scaleY;
  78. this.pinchTranslateDirection(!xBigger, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch, xBigger ? transform.scaleX : transform.scaleY);
  79. }
  80. });