gesture-controller.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { Inject, Injectable, forwardRef } from '@angular/core';
  2. import { App } from '../components/app/app';
  3. /** @hidden */
  4. export const GESTURE_GO_BACK_SWIPE = 'goback-swipe';
  5. /** @hidden */
  6. export const GESTURE_MENU_SWIPE = 'menu-swipe';
  7. /** @hidden */
  8. export const GESTURE_ITEM_SWIPE = 'item-swipe';
  9. /** @hidden */
  10. export const GESTURE_REFRESHER = 'refresher';
  11. /** @hidden */
  12. export const GESTURE_TOGGLE = 'toggle';
  13. /** @hidden */
  14. export const GESTURE_PRIORITY_SLIDING_ITEM = -10;
  15. /** @hidden */
  16. export const GESTURE_PRIORITY_REFRESHER = 0;
  17. /** @hidden */
  18. export const GESTURE_PRIORITY_MENU_SWIPE = 10;
  19. /** @hidden */
  20. export const GESTURE_PRIORITY_GO_BACK_SWIPE = 20;
  21. /** @hidden */
  22. export const GESTURE_PRIORITY_TOGGLE = 30;
  23. /**
  24. * @hidden
  25. */
  26. export const BLOCK_ALL = {
  27. disable: [GESTURE_MENU_SWIPE, GESTURE_GO_BACK_SWIPE],
  28. disableScroll: true
  29. };
  30. /**
  31. * @hidden
  32. */
  33. export class GestureController {
  34. constructor(_app) {
  35. this._app = _app;
  36. this.id = 1;
  37. this.requestedStart = {};
  38. this.disabledGestures = {};
  39. this.disabledScroll = new Set();
  40. this.capturedID = null;
  41. }
  42. createGesture(opts) {
  43. if (!opts.name) {
  44. throw new Error('name is undefined');
  45. }
  46. return new GestureDelegate(opts.name, this.newID(), this, opts.priority || 0, !!opts.disableScroll);
  47. }
  48. createBlocker(opts = {}) {
  49. return new BlockerDelegate(this.newID(), this, opts.disable, !!opts.disableScroll);
  50. }
  51. newID() {
  52. let id = this.id;
  53. this.id++;
  54. return id;
  55. }
  56. start(gestureName, id, priority) {
  57. if (!this.canStart(gestureName)) {
  58. delete this.requestedStart[id];
  59. return false;
  60. }
  61. this.requestedStart[id] = priority;
  62. return true;
  63. }
  64. capture(gestureName, id, priority) {
  65. if (!this.start(gestureName, id, priority)) {
  66. return false;
  67. }
  68. let requestedStart = this.requestedStart;
  69. let maxPriority = -10000;
  70. for (let gestureID in requestedStart) {
  71. maxPriority = Math.max(maxPriority, requestedStart[gestureID]);
  72. }
  73. if (maxPriority === priority) {
  74. this.capturedID = id;
  75. this.requestedStart = {};
  76. (void 0) /* console.debug */;
  77. return true;
  78. }
  79. delete requestedStart[id];
  80. (void 0) /* console.debug */;
  81. return false;
  82. }
  83. release(id) {
  84. delete this.requestedStart[id];
  85. if (this.capturedID && id === this.capturedID) {
  86. this.capturedID = null;
  87. }
  88. }
  89. disableGesture(gestureName, id) {
  90. let set = this.disabledGestures[gestureName];
  91. if (!set) {
  92. set = new Set();
  93. this.disabledGestures[gestureName] = set;
  94. }
  95. set.add(id);
  96. }
  97. enableGesture(gestureName, id) {
  98. let set = this.disabledGestures[gestureName];
  99. if (set) {
  100. set.delete(id);
  101. }
  102. }
  103. disableScroll(id) {
  104. let isEnabled = !this.isScrollDisabled();
  105. this.disabledScroll.add(id);
  106. if (this._app && isEnabled && this.isScrollDisabled()) {
  107. (void 0) /* console.debug */;
  108. this._app._setDisableScroll(true);
  109. }
  110. }
  111. enableScroll(id) {
  112. let isDisabled = this.isScrollDisabled();
  113. this.disabledScroll.delete(id);
  114. if (this._app && isDisabled && !this.isScrollDisabled()) {
  115. (void 0) /* console.debug */;
  116. this._app._setDisableScroll(false);
  117. }
  118. }
  119. canStart(gestureName) {
  120. if (this.capturedID) {
  121. (void 0) /* console.debug */;
  122. // a gesture already captured
  123. return false;
  124. }
  125. if (this.isDisabled(gestureName)) {
  126. (void 0) /* console.debug */;
  127. return false;
  128. }
  129. return true;
  130. }
  131. isCaptured() {
  132. return !!this.capturedID;
  133. }
  134. isScrollDisabled() {
  135. return this.disabledScroll.size > 0;
  136. }
  137. isDisabled(gestureName) {
  138. let disabled = this.disabledGestures[gestureName];
  139. return !!(disabled && disabled.size > 0);
  140. }
  141. }
  142. GestureController.decorators = [
  143. { type: Injectable },
  144. ];
  145. /** @nocollapse */
  146. GestureController.ctorParameters = () => [
  147. { type: App, decorators: [{ type: Inject, args: [forwardRef(() => App),] },] },
  148. ];
  149. /**
  150. * @hidden
  151. */
  152. export class GestureDelegate {
  153. constructor(name, id, controller, priority, disableScroll) {
  154. this.name = name;
  155. this.id = id;
  156. this.controller = controller;
  157. this.priority = priority;
  158. this.disableScroll = disableScroll;
  159. }
  160. canStart() {
  161. if (!this.controller) {
  162. (void 0) /* assert */;
  163. return false;
  164. }
  165. return this.controller.canStart(this.name);
  166. }
  167. start() {
  168. if (!this.controller) {
  169. (void 0) /* assert */;
  170. return false;
  171. }
  172. return this.controller.start(this.name, this.id, this.priority);
  173. }
  174. capture() {
  175. if (!this.controller) {
  176. (void 0) /* assert */;
  177. return false;
  178. }
  179. let captured = this.controller.capture(this.name, this.id, this.priority);
  180. if (captured && this.disableScroll) {
  181. this.controller.disableScroll(this.id);
  182. }
  183. return captured;
  184. }
  185. release() {
  186. if (!this.controller) {
  187. (void 0) /* assert */;
  188. return;
  189. }
  190. this.controller.release(this.id);
  191. if (this.disableScroll) {
  192. this.controller.enableScroll(this.id);
  193. }
  194. }
  195. destroy() {
  196. this.release();
  197. this.controller = null;
  198. }
  199. }
  200. /**
  201. * @hidden
  202. */
  203. export class BlockerDelegate {
  204. constructor(id, controller, disable, disableScroll) {
  205. this.id = id;
  206. this.controller = controller;
  207. this.disable = disable;
  208. this.disableScroll = disableScroll;
  209. this.blocked = false;
  210. }
  211. block() {
  212. if (!this.controller) {
  213. (void 0) /* assert */;
  214. return;
  215. }
  216. if (this.disable) {
  217. this.disable.forEach(gesture => {
  218. this.controller.disableGesture(gesture, this.id);
  219. });
  220. }
  221. if (this.disableScroll) {
  222. this.controller.disableScroll(this.id);
  223. }
  224. this.blocked = true;
  225. }
  226. unblock() {
  227. if (!this.controller) {
  228. (void 0) /* assert */;
  229. return;
  230. }
  231. if (this.disable) {
  232. this.disable.forEach(gesture => {
  233. this.controller.enableGesture(gesture, this.id);
  234. });
  235. }
  236. if (this.disableScroll) {
  237. this.controller.enableScroll(this.id);
  238. }
  239. this.blocked = false;
  240. }
  241. destroy() {
  242. this.unblock();
  243. this.controller = null;
  244. }
  245. }
  246. //# sourceMappingURL=gesture-controller.js.map