swipe.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  3. var Util = require('../util/common');
  4. var Interaction = require('./base');
  5. var Chart = require('../chart/chart');
  6. var FilterPlugin = require('../plugin/filter');
  7. var MoveMixin = require('./mixin/move');
  8. var UpdateScaleMixin = require('./mixin/update-scale');
  9. var Swipe =
  10. /*#__PURE__*/
  11. function (_Interaction) {
  12. _inheritsLoose(Swipe, _Interaction);
  13. var _proto = Swipe.prototype;
  14. _proto.getDefaultCfg = function getDefaultCfg() {
  15. var defaultCfg = _Interaction.prototype.getDefaultCfg.call(this);
  16. defaultCfg = Util.mix({}, defaultCfg, {
  17. startEvent: 'touchstart',
  18. processEvent: 'swipe',
  19. endEvent: 'touchend',
  20. currentDeltaX: null,
  21. threshold: 10,
  22. // Minimal distance required before recognizing.
  23. velocity: 0.3,
  24. // Minimal velocity required before recognizing, unit is in px per ms.
  25. limitRange: {},
  26. _timestamp: 0,
  27. _panCumulativeDelta: 0,
  28. speed: 5
  29. });
  30. return defaultCfg;
  31. };
  32. function Swipe(cfg, chart) {
  33. var _this;
  34. _this = _Interaction.call(this, cfg, chart) || this;
  35. var self = _assertThisInitialized(_assertThisInitialized(_this));
  36. var hammer = self.hammer,
  37. threshold = self.threshold,
  38. velocity = self.velocity;
  39. if (hammer) {
  40. hammer.get('swipe').set({
  41. direction: 6,
  42. // only support horizontal
  43. threshold: threshold,
  44. velocity: velocity
  45. });
  46. }
  47. chart.registerPlugins([FilterPlugin, {
  48. changeData: function changeData() {
  49. self.limitRange = {};
  50. },
  51. clear: function clear() {
  52. self.limitRange = {};
  53. }
  54. }]);
  55. self.mode = 'x';
  56. Util.mix(self, UpdateScaleMixin, MoveMixin);
  57. return _this;
  58. }
  59. _proto.process = function process(e) {
  60. this.currentDeltaX = 0;
  61. this._handleMove(e);
  62. };
  63. _proto.end = function end() {
  64. this.currentDeltaX = null;
  65. this._panCumulativeDelta = 0;
  66. };
  67. return Swipe;
  68. }(Interaction);
  69. Chart.registerInteraction('swipe', Swipe);
  70. module.exports = Swipe;