123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { defaults } from '../util/util';
  2. import { PanRecognizer } from './recognizers';
  3. import { pointerCoord } from '../util/dom';
  4. import { UIEventManager } from './ui-event-manager';
  5. /**
  6. * @hidden
  7. */
  8. export class PanGesture {
  9. constructor(plt, element, opts = {}) {
  10. this.plt = plt;
  11. this.element = element;
  12. defaults(opts, {
  13. threshold: 20,
  14. maxAngle: 40,
  15. direction: 'x',
  16. zone: true,
  17. capture: false,
  18. passive: false,
  19. });
  20. this.events = new UIEventManager(plt);
  21. if (opts.domController) {
  22. this.debouncer = opts.domController.debouncer();
  23. }
  24. this.gestute = opts.gesture;
  25. this.direction = opts.direction;
  26. this.eventsConfig = {
  27. element: this.element,
  28. pointerDown: this.pointerDown.bind(this),
  29. pointerMove: this.pointerMove.bind(this),
  30. pointerUp: this.pointerUp.bind(this),
  31. zone: opts.zone,
  32. capture: opts.capture,
  33. passive: opts.passive
  34. };
  35. if (opts.threshold > 0) {
  36. this.detector = new PanRecognizer(opts.direction, opts.threshold, opts.maxAngle);
  37. }
  38. }
  39. listen() {
  40. if (!this.isListening) {
  41. this.pointerEvents = this.events.pointerEvents(this.eventsConfig);
  42. this.isListening = true;
  43. }
  44. }
  45. unlisten() {
  46. if (this.isListening) {
  47. this.gestute && this.gestute.release();
  48. this.events.unlistenAll();
  49. this.isListening = false;
  50. }
  51. }
  52. destroy() {
  53. this.gestute && this.gestute.destroy();
  54. this.gestute = null;
  55. this.unlisten();
  56. this.events.destroy();
  57. this.events = this.element = this.gestute = null;
  58. }
  59. pointerDown(ev) {
  60. if (this.started) {
  61. return;
  62. }
  63. if (!this.canStart(ev)) {
  64. return false;
  65. }
  66. if (this.gestute) {
  67. // Release fallback
  68. this.gestute.release();
  69. // Start gesture
  70. if (!this.gestute.start()) {
  71. return false;
  72. }
  73. }
  74. this.started = true;
  75. this.captured = false;
  76. const coord = pointerCoord(ev);
  77. if (this.detector) {
  78. this.detector.start(coord);
  79. }
  80. else {
  81. if (!this.tryToCapture(ev)) {
  82. this.started = false;
  83. this.captured = false;
  84. this.gestute.release();
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. pointerMove(ev) {
  91. (void 0) /* assert */;
  92. if (this.captured) {
  93. this.debouncer.write(() => {
  94. this.onDragMove(ev);
  95. });
  96. return;
  97. }
  98. (void 0) /* assert */;
  99. const coord = pointerCoord(ev);
  100. if (this.detector.detect(coord)) {
  101. if (this.detector.pan() !== 0) {
  102. if (!this.tryToCapture(ev)) {
  103. this.abort(ev);
  104. }
  105. }
  106. }
  107. }
  108. pointerUp(ev) {
  109. (void 0) /* assert */;
  110. this.debouncer.cancel();
  111. this.gestute && this.gestute.release();
  112. if (this.captured) {
  113. this.onDragEnd(ev);
  114. }
  115. else {
  116. this.notCaptured(ev);
  117. }
  118. this.captured = false;
  119. this.started = false;
  120. }
  121. tryToCapture(ev) {
  122. (void 0) /* assert */;
  123. (void 0) /* assert */;
  124. if (this.gestute && !this.gestute.capture()) {
  125. return false;
  126. }
  127. this.onDragStart(ev);
  128. this.captured = true;
  129. return true;
  130. }
  131. abort(ev) {
  132. this.started = false;
  133. this.captured = false;
  134. this.gestute.release();
  135. this.pointerEvents.stop();
  136. this.notCaptured(ev);
  137. }
  138. getNativeElement() {
  139. return this.element;
  140. }
  141. // Implemented in a subclass
  142. canStart(_ev) { return true; }
  143. onDragStart(_ev) { }
  144. onDragMove(_ev) { }
  145. onDragEnd(_ev) { }
  146. notCaptured(_ev) { }
  147. }
  148. //# sourceMappingURL=pan-gesture.js.map