base.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * The parent class of interaction
  3. * @author sima.zhang1990@gmail.com
  4. */
  5. var Util = require('../util/common');
  6. var Chart = require('../chart/chart');
  7. var Hammer;
  8. if (!Util.isWx && !Util.isMy) {
  9. Hammer = require('hammerjs');
  10. }
  11. var TOUCH_EVENTS = ['touchstart', 'touchmove', 'touchend'];
  12. var Interaction =
  13. /*#__PURE__*/
  14. function () {
  15. var _proto = Interaction.prototype;
  16. _proto.getDefaultCfg = function getDefaultCfg() {
  17. return {
  18. startEvent: TOUCH_EVENTS[0],
  19. processEvent: TOUCH_EVENTS[1],
  20. endEvent: TOUCH_EVENTS[2],
  21. resetEvent: null
  22. };
  23. };
  24. _proto._start = function _start(ev) {
  25. this.preStart && this.preStart(ev);
  26. this.start(ev);
  27. this.onStart && this.onStart(ev);
  28. };
  29. _proto._process = function _process(ev) {
  30. this.preProcess && this.preProcess(ev);
  31. this.process(ev);
  32. this.onProcess && this.onProcess(ev);
  33. };
  34. _proto._end = function _end(ev) {
  35. this.preEnd && this.preEnd(ev);
  36. this.end(ev);
  37. this.onEnd && this.onEnd(ev);
  38. };
  39. _proto._reset = function _reset(ev) {
  40. this.preReset && this.preReset(ev);
  41. this.reset(ev);
  42. this.onReset && this.onReset(ev);
  43. }; // override
  44. _proto.start = function start() {}; // override
  45. _proto.process = function process() {}; // override
  46. _proto.end = function end() {}; // override
  47. _proto.reset = function reset() {};
  48. function Interaction(cfg, chart) {
  49. var defaultCfg = this.getDefaultCfg();
  50. Util.deepMix(this, defaultCfg, cfg);
  51. this.chart = chart;
  52. this.canvas = chart.get('canvas');
  53. this.el = chart.get('canvas').get('el');
  54. this._bindEvents();
  55. }
  56. _proto._bindEvents = function _bindEvents() {
  57. this._clearEvents(); // clear events
  58. var startEvent = this.startEvent,
  59. processEvent = this.processEvent,
  60. endEvent = this.endEvent,
  61. resetEvent = this.resetEvent,
  62. el = this.el;
  63. if (Hammer) {
  64. this.hammer = new Hammer(el);
  65. }
  66. this._bindEvent(startEvent, '_start');
  67. this._bindEvent(processEvent, '_process');
  68. this._bindEvent(endEvent, '_end');
  69. this._bindEvent(resetEvent, '_reset');
  70. };
  71. _proto._clearEvents = function _clearEvents() {
  72. var startEvent = this.startEvent,
  73. processEvent = this.processEvent,
  74. endEvent = this.endEvent,
  75. resetEvent = this.resetEvent;
  76. if (this.hammer) {
  77. this.hammer.destroy();
  78. this.hammer = null;
  79. }
  80. this._clearTouchEvent(startEvent, '_start');
  81. this._clearTouchEvent(processEvent, '_process');
  82. this._clearTouchEvent(endEvent, '_end');
  83. this._clearTouchEvent(resetEvent, '_reset');
  84. };
  85. _proto._bindEvent = function _bindEvent(eventName, methodName) {
  86. var el = this.el;
  87. if (eventName) {
  88. if (TOUCH_EVENTS.indexOf(eventName) !== -1) {
  89. Util.addEventListener(el, eventName, Util.wrapBehavior(this, methodName));
  90. } else if (this.hammer) {
  91. this.hammer.on(eventName, Util.wrapBehavior(this, methodName));
  92. }
  93. }
  94. };
  95. _proto._clearTouchEvent = function _clearTouchEvent(eventName, methodName) {
  96. var el = this.el;
  97. if (eventName && TOUCH_EVENTS.indexOf(eventName) !== -1) {
  98. Util.removeEventListener(el, eventName, Util.getWrapBehavior(this, methodName));
  99. }
  100. };
  101. _proto.destroy = function destroy() {
  102. this._clearEvents();
  103. };
  104. return Interaction;
  105. }();
  106. Chart._Interactions = {};
  107. Chart.registerInteraction = function (type, constructor) {
  108. Chart._Interactions[type] = constructor;
  109. };
  110. Chart.getInteraction = function (type) {
  111. return Chart._Interactions[type];
  112. };
  113. Chart.prototype.interaction = function (type, cfg) {
  114. var interactions = this._interactions || {};
  115. if (interactions[type]) {
  116. // if reprated, destroy last
  117. interactions[type].destroy();
  118. }
  119. var Ctor = Chart.getInteraction(type);
  120. var interact = new Ctor(cfg, this);
  121. interactions[type] = interact;
  122. this._interactions = interactions;
  123. return this;
  124. };
  125. Chart.prototype.clearInteraction = function (type) {
  126. var interactions = this._interactions;
  127. if (!interactions) return;
  128. if (type) {
  129. interactions[type] && interactions[type].destroy();
  130. delete interactions[type];
  131. } else {
  132. Util.each(interactions, function (interaction, key) {
  133. interaction.destroy();
  134. delete interactions[key];
  135. });
  136. }
  137. return this;
  138. };
  139. module.exports = Interaction;