gesture.js 4.1 KB

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