gesture.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. "use strict";
  2. var Hammer = require('hammerjs'); // http://hammerjs.github.io/
  3. var Util = require('../util/common');
  4. var defaultOptions = {
  5. useCalculate: true,
  6. useOffset: false
  7. };
  8. var GestureController = /*#__PURE__*/function () {
  9. function GestureController(_ref) {
  10. var dom = _ref.dom,
  11. gesture = _ref.gesture,
  12. options = _ref.options,
  13. chart = _ref.chart,
  14. hammerOptions = _ref.hammerOptions;
  15. this.Hammer = Hammer;
  16. this.hammer = new Hammer(dom, hammerOptions);
  17. this.dom = dom;
  18. this.gesture = gesture;
  19. this.options = Util.deepMix({}, defaultOptions, options);
  20. this.hammerOptions = hammerOptions;
  21. this.chart = chart;
  22. this._unbindEvent = {};
  23. if (!options) {
  24. this.hammerOptionsHack(gesture, this.hammer);
  25. }
  26. }
  27. var _proto = GestureController.prototype;
  28. _proto.hammerOptionsHack = function hammerOptionsHack(gesture, hammer) {
  29. for (var key in gesture) {
  30. if (key.indexOf('swipe') !== -1 && hammer.get('swipe')) {
  31. hammer.get('swipe').set({
  32. enable: true
  33. });
  34. }
  35. if (key.indexOf('pinch') !== -1 && hammer.get('pinch')) {
  36. hammer.get('pinch').set({
  37. enable: true
  38. });
  39. }
  40. }
  41. };
  42. _proto.bindEvents = function bindEvents() {
  43. var _this = this;
  44. var gesture = this.gesture,
  45. hammer = this.hammer,
  46. dom = this.dom;
  47. var useCalculate = this.options.useCalculate;
  48. if (!hammer) {
  49. return;
  50. }
  51. var _loop = function _loop(key) {
  52. if (['touchstart', 'touchmove', 'touchend'].indexOf(key) !== -1) {
  53. var bindEvent = function bindEvent(event) {
  54. var records = useCalculate ? _this.getEventPostionRecords(event, true) : null;
  55. gesture[key](records, event);
  56. };
  57. Util.addEventListener(dom, key, bindEvent);
  58. _this._unbindEvent[key] = bindEvent;
  59. } else {
  60. hammer.on(key, function (event) {
  61. var records = useCalculate ? _this.getEventPostionRecords(event, false) : null;
  62. gesture[key](records, event);
  63. });
  64. }
  65. };
  66. for (var key in gesture) {
  67. _loop(key);
  68. }
  69. };
  70. _proto.getEventPostionRecords = function getEventPostionRecords(event, _isOrigin) {
  71. var useOffset = this.options.useOffset;
  72. var canvasDom = this.chart.get('canvas').get('el');
  73. var x;
  74. var y;
  75. if (_isOrigin) {
  76. var positionSource = event.targetTouches.length > 0 ? event.targetTouches[0] : event.changedTouches[0];
  77. if (useOffset) {
  78. x = positionSource.clientX - canvasDom.offsetLeft;
  79. y = positionSource.clientY - canvasDom.offsetTop;
  80. } else {
  81. x = positionSource.clientX;
  82. y = positionSource.clientY;
  83. }
  84. } else {
  85. if (useOffset) {
  86. x = event.center.x - canvasDom.offsetLeft;
  87. y = event.center.y - canvasDom.offsetTop;
  88. } else {
  89. x = event.center.x;
  90. y = event.center.y;
  91. }
  92. }
  93. return this.chart.getSnapRecords({
  94. x: x,
  95. y: y
  96. });
  97. };
  98. _proto.destroy = function destroy() {
  99. this.hammer.destroy();
  100. for (var key in this._unbindEvent) {
  101. var event = this._unbindEvent[key];
  102. Util.removeEventListener(this.dom, key, event);
  103. }
  104. };
  105. return GestureController;
  106. }();
  107. module.exports = {
  108. init: function init(chart) {
  109. chart.pluginGesture = function (_ref2) {
  110. var gesture = _ref2.gesture,
  111. options = _ref2.options,
  112. hammerOptions = _ref2.hammerOptions;
  113. var canvasDom = chart.get('canvas').get('el');
  114. var gestureController = new GestureController({
  115. dom: canvasDom,
  116. gesture: gesture,
  117. options: options,
  118. hammerOptions: hammerOptions,
  119. chart: chart
  120. });
  121. gestureController.bindEvents();
  122. chart.set('gestureController', gestureController);
  123. return gestureController;
  124. };
  125. },
  126. clear: function clear(chart) {
  127. var gestureController = chart.get('gestureController');
  128. gestureController && gestureController.destroy();
  129. }
  130. };