gesture-controller-9436f482.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. 'use strict';
  5. class GestureController {
  6. constructor() {
  7. this.gestureId = 0;
  8. this.requestedStart = new Map();
  9. this.disabledGestures = new Map();
  10. this.disabledScroll = new Set();
  11. }
  12. /**
  13. * Creates a gesture delegate based on the GestureConfig passed
  14. */
  15. createGesture(config) {
  16. var _a;
  17. return new GestureDelegate(this, this.newID(), config.name, (_a = config.priority) !== null && _a !== void 0 ? _a : 0, !!config.disableScroll);
  18. }
  19. /**
  20. * Creates a blocker that will block any other gesture events from firing. Set in the ion-gesture component.
  21. */
  22. createBlocker(opts = {}) {
  23. return new BlockerDelegate(this, this.newID(), opts.disable, !!opts.disableScroll);
  24. }
  25. start(gestureName, id, priority) {
  26. if (!this.canStart(gestureName)) {
  27. this.requestedStart.delete(id);
  28. return false;
  29. }
  30. this.requestedStart.set(id, priority);
  31. return true;
  32. }
  33. capture(gestureName, id, priority) {
  34. if (!this.start(gestureName, id, priority)) {
  35. return false;
  36. }
  37. const requestedStart = this.requestedStart;
  38. let maxPriority = -10000;
  39. requestedStart.forEach((value) => {
  40. maxPriority = Math.max(maxPriority, value);
  41. });
  42. if (maxPriority === priority) {
  43. this.capturedId = id;
  44. requestedStart.clear();
  45. const event = new CustomEvent('ionGestureCaptured', { detail: { gestureName } });
  46. document.dispatchEvent(event);
  47. return true;
  48. }
  49. requestedStart.delete(id);
  50. return false;
  51. }
  52. release(id) {
  53. this.requestedStart.delete(id);
  54. if (this.capturedId === id) {
  55. this.capturedId = undefined;
  56. }
  57. }
  58. disableGesture(gestureName, id) {
  59. let set = this.disabledGestures.get(gestureName);
  60. if (set === undefined) {
  61. set = new Set();
  62. this.disabledGestures.set(gestureName, set);
  63. }
  64. set.add(id);
  65. }
  66. enableGesture(gestureName, id) {
  67. const set = this.disabledGestures.get(gestureName);
  68. if (set !== undefined) {
  69. set.delete(id);
  70. }
  71. }
  72. disableScroll(id) {
  73. this.disabledScroll.add(id);
  74. if (this.disabledScroll.size === 1) {
  75. document.body.classList.add(BACKDROP_NO_SCROLL);
  76. }
  77. }
  78. enableScroll(id) {
  79. this.disabledScroll.delete(id);
  80. if (this.disabledScroll.size === 0) {
  81. document.body.classList.remove(BACKDROP_NO_SCROLL);
  82. }
  83. }
  84. canStart(gestureName) {
  85. if (this.capturedId !== undefined) {
  86. // a gesture already captured
  87. return false;
  88. }
  89. if (this.isDisabled(gestureName)) {
  90. return false;
  91. }
  92. return true;
  93. }
  94. isCaptured() {
  95. return this.capturedId !== undefined;
  96. }
  97. isScrollDisabled() {
  98. return this.disabledScroll.size > 0;
  99. }
  100. isDisabled(gestureName) {
  101. const disabled = this.disabledGestures.get(gestureName);
  102. if (disabled && disabled.size > 0) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. newID() {
  108. this.gestureId++;
  109. return this.gestureId;
  110. }
  111. }
  112. class GestureDelegate {
  113. constructor(ctrl, id, name, priority, disableScroll) {
  114. this.id = id;
  115. this.name = name;
  116. this.disableScroll = disableScroll;
  117. this.priority = priority * 1000000 + id;
  118. this.ctrl = ctrl;
  119. }
  120. canStart() {
  121. if (!this.ctrl) {
  122. return false;
  123. }
  124. return this.ctrl.canStart(this.name);
  125. }
  126. start() {
  127. if (!this.ctrl) {
  128. return false;
  129. }
  130. return this.ctrl.start(this.name, this.id, this.priority);
  131. }
  132. capture() {
  133. if (!this.ctrl) {
  134. return false;
  135. }
  136. const captured = this.ctrl.capture(this.name, this.id, this.priority);
  137. if (captured && this.disableScroll) {
  138. this.ctrl.disableScroll(this.id);
  139. }
  140. return captured;
  141. }
  142. release() {
  143. if (this.ctrl) {
  144. this.ctrl.release(this.id);
  145. if (this.disableScroll) {
  146. this.ctrl.enableScroll(this.id);
  147. }
  148. }
  149. }
  150. destroy() {
  151. this.release();
  152. this.ctrl = undefined;
  153. }
  154. }
  155. class BlockerDelegate {
  156. constructor(ctrl, id, disable, disableScroll) {
  157. this.id = id;
  158. this.disable = disable;
  159. this.disableScroll = disableScroll;
  160. this.ctrl = ctrl;
  161. }
  162. block() {
  163. if (!this.ctrl) {
  164. return;
  165. }
  166. if (this.disable) {
  167. for (const gesture of this.disable) {
  168. this.ctrl.disableGesture(gesture, this.id);
  169. }
  170. }
  171. if (this.disableScroll) {
  172. this.ctrl.disableScroll(this.id);
  173. }
  174. }
  175. unblock() {
  176. if (!this.ctrl) {
  177. return;
  178. }
  179. if (this.disable) {
  180. for (const gesture of this.disable) {
  181. this.ctrl.enableGesture(gesture, this.id);
  182. }
  183. }
  184. if (this.disableScroll) {
  185. this.ctrl.enableScroll(this.id);
  186. }
  187. }
  188. destroy() {
  189. this.unblock();
  190. this.ctrl = undefined;
  191. }
  192. }
  193. const BACKDROP_NO_SCROLL = 'backdrop-no-scroll';
  194. const GESTURE_CONTROLLER = new GestureController();
  195. exports.BACKDROP_NO_SCROLL = BACKDROP_NO_SCROLL;
  196. exports.GESTURE_CONTROLLER = GESTURE_CONTROLLER;