pointer-events.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @hidden
  3. */
  4. export class PointerEvents {
  5. constructor(plt, ele, pointerDown, pointerMove, pointerUp, option) {
  6. this.plt = plt;
  7. this.ele = ele;
  8. this.pointerDown = pointerDown;
  9. this.pointerMove = pointerMove;
  10. this.pointerUp = pointerUp;
  11. this.option = option;
  12. this.rmTouchStart = null;
  13. this.rmTouchMove = null;
  14. this.rmTouchEnd = null;
  15. this.rmTouchCancel = null;
  16. this.rmMouseStart = null;
  17. this.rmMouseMove = null;
  18. this.rmMouseUp = null;
  19. this.lastTouchEvent = 0;
  20. this.mouseWait = 2 * 1000;
  21. (void 0) /* assert */;
  22. (void 0) /* assert */;
  23. this.bindTouchEnd = this.handleTouchEnd.bind(this);
  24. this.bindMouseUp = this.handleMouseUp.bind(this);
  25. this.rmTouchStart = this.plt.registerListener(ele, 'touchstart', this.handleTouchStart.bind(this), option);
  26. this.rmMouseStart = this.plt.registerListener(ele, 'mousedown', this.handleMouseDown.bind(this), option);
  27. }
  28. handleTouchStart(ev) {
  29. (void 0) /* assert */;
  30. (void 0) /* assert */;
  31. this.lastTouchEvent = Date.now() + this.mouseWait;
  32. this.lastEventType = POINTER_EVENT_TYPE_TOUCH;
  33. if (!this.pointerDown(ev, POINTER_EVENT_TYPE_TOUCH)) {
  34. return;
  35. }
  36. if (!this.rmTouchMove && this.pointerMove) {
  37. this.rmTouchMove = this.plt.registerListener(this.ele, 'touchmove', this.pointerMove, this.option);
  38. }
  39. if (!this.rmTouchEnd) {
  40. this.rmTouchEnd = this.plt.registerListener(this.ele, 'touchend', this.bindTouchEnd, this.option);
  41. }
  42. if (!this.rmTouchCancel) {
  43. this.rmTouchCancel = this.plt.registerListener(this.ele, 'touchcancel', this.bindTouchEnd, this.option);
  44. }
  45. }
  46. handleMouseDown(ev) {
  47. (void 0) /* assert */;
  48. (void 0) /* assert */;
  49. if (this.lastTouchEvent > Date.now()) {
  50. (void 0) /* console.debug */;
  51. return;
  52. }
  53. this.lastEventType = POINTER_EVENT_TYPE_MOUSE;
  54. if (!this.pointerDown(ev, POINTER_EVENT_TYPE_MOUSE)) {
  55. return;
  56. }
  57. if (!this.rmMouseMove && this.pointerMove) {
  58. this.rmMouseMove = this.plt.registerListener(this.plt.doc(), 'mousemove', this.pointerMove, this.option);
  59. }
  60. if (!this.rmMouseUp) {
  61. this.rmMouseUp = this.plt.registerListener(this.plt.doc(), 'mouseup', this.bindMouseUp, this.option);
  62. }
  63. }
  64. handleTouchEnd(ev) {
  65. this.stopTouch();
  66. this.pointerUp && this.pointerUp(ev, POINTER_EVENT_TYPE_TOUCH);
  67. }
  68. handleMouseUp(ev) {
  69. this.stopMouse();
  70. this.pointerUp && this.pointerUp(ev, POINTER_EVENT_TYPE_MOUSE);
  71. }
  72. stopTouch() {
  73. this.rmTouchMove && this.rmTouchMove();
  74. this.rmTouchEnd && this.rmTouchEnd();
  75. this.rmTouchCancel && this.rmTouchCancel();
  76. this.rmTouchMove = this.rmTouchEnd = this.rmTouchCancel = null;
  77. }
  78. stopMouse() {
  79. this.rmMouseMove && this.rmMouseMove();
  80. this.rmMouseUp && this.rmMouseUp();
  81. this.rmMouseMove = this.rmMouseUp = null;
  82. }
  83. stop() {
  84. this.stopTouch();
  85. this.stopMouse();
  86. }
  87. destroy() {
  88. this.rmTouchStart && this.rmTouchStart();
  89. this.rmMouseStart && this.rmMouseStart();
  90. this.stop();
  91. this.ele = this.pointerUp = this.pointerMove = this.pointerDown = this.rmTouchStart = this.rmMouseStart = null;
  92. }
  93. }
  94. export const POINTER_EVENT_TYPE_MOUSE = 1;
  95. export const POINTER_EVENT_TYPE_TOUCH = 2;
  96. //# sourceMappingURL=pointer-events.js.map