index3.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { G as GESTURE_CONTROLLER } from './gesture-controller.js';
  5. export { G as GESTURE_CONTROLLER } from './gesture-controller.js';
  6. const addEventListener = (el, // TODO(FW-2832): type
  7. eventName, callback, opts) => {
  8. // use event listener options when supported
  9. // otherwise it's just a boolean for the "capture" arg
  10. const listenerOpts = supportsPassive(el)
  11. ? {
  12. capture: !!opts.capture,
  13. passive: !!opts.passive,
  14. }
  15. : !!opts.capture;
  16. let add;
  17. let remove;
  18. if (el['__zone_symbol__addEventListener']) {
  19. add = '__zone_symbol__addEventListener';
  20. remove = '__zone_symbol__removeEventListener';
  21. }
  22. else {
  23. add = 'addEventListener';
  24. remove = 'removeEventListener';
  25. }
  26. el[add](eventName, callback, listenerOpts);
  27. return () => {
  28. el[remove](eventName, callback, listenerOpts);
  29. };
  30. };
  31. const supportsPassive = (node) => {
  32. if (_sPassive === undefined) {
  33. try {
  34. const opts = Object.defineProperty({}, 'passive', {
  35. get: () => {
  36. _sPassive = true;
  37. },
  38. });
  39. node.addEventListener('optsTest', () => {
  40. return;
  41. }, opts);
  42. }
  43. catch (e) {
  44. _sPassive = false;
  45. }
  46. }
  47. return !!_sPassive;
  48. };
  49. let _sPassive;
  50. const MOUSE_WAIT = 2000;
  51. // TODO(FW-2832): types
  52. const createPointerEvents = (el, pointerDown, pointerMove, pointerUp, options) => {
  53. let rmTouchStart;
  54. let rmTouchMove;
  55. let rmTouchEnd;
  56. let rmTouchCancel;
  57. let rmMouseStart;
  58. let rmMouseMove;
  59. let rmMouseUp;
  60. let lastTouchEvent = 0;
  61. const handleTouchStart = (ev) => {
  62. lastTouchEvent = Date.now() + MOUSE_WAIT;
  63. if (!pointerDown(ev)) {
  64. return;
  65. }
  66. if (!rmTouchMove && pointerMove) {
  67. rmTouchMove = addEventListener(el, 'touchmove', pointerMove, options);
  68. }
  69. /**
  70. * Events are dispatched on the element that is tapped and bubble up to
  71. * the reference element in the gesture. In the event that the element this
  72. * event was first dispatched on is removed from the DOM, the event will no
  73. * longer bubble up to our reference element. This leaves the gesture in an
  74. * unusable state. To account for this, the touchend and touchcancel listeners
  75. * should be added to the event target so that they still fire even if the target
  76. * is removed from the DOM.
  77. */
  78. if (!rmTouchEnd) {
  79. rmTouchEnd = addEventListener(ev.target, 'touchend', handleTouchEnd, options);
  80. }
  81. if (!rmTouchCancel) {
  82. rmTouchCancel = addEventListener(ev.target, 'touchcancel', handleTouchEnd, options);
  83. }
  84. };
  85. const handleMouseDown = (ev) => {
  86. if (lastTouchEvent > Date.now()) {
  87. return;
  88. }
  89. if (!pointerDown(ev)) {
  90. return;
  91. }
  92. if (!rmMouseMove && pointerMove) {
  93. rmMouseMove = addEventListener(getDocument(el), 'mousemove', pointerMove, options);
  94. }
  95. if (!rmMouseUp) {
  96. rmMouseUp = addEventListener(getDocument(el), 'mouseup', handleMouseUp, options);
  97. }
  98. };
  99. const handleTouchEnd = (ev) => {
  100. stopTouch();
  101. if (pointerUp) {
  102. pointerUp(ev);
  103. }
  104. };
  105. const handleMouseUp = (ev) => {
  106. stopMouse();
  107. if (pointerUp) {
  108. pointerUp(ev);
  109. }
  110. };
  111. const stopTouch = () => {
  112. if (rmTouchMove) {
  113. rmTouchMove();
  114. }
  115. if (rmTouchEnd) {
  116. rmTouchEnd();
  117. }
  118. if (rmTouchCancel) {
  119. rmTouchCancel();
  120. }
  121. rmTouchMove = rmTouchEnd = rmTouchCancel = undefined;
  122. };
  123. const stopMouse = () => {
  124. if (rmMouseMove) {
  125. rmMouseMove();
  126. }
  127. if (rmMouseUp) {
  128. rmMouseUp();
  129. }
  130. rmMouseMove = rmMouseUp = undefined;
  131. };
  132. const stop = () => {
  133. stopTouch();
  134. stopMouse();
  135. };
  136. const enable = (isEnabled = true) => {
  137. if (!isEnabled) {
  138. if (rmTouchStart) {
  139. rmTouchStart();
  140. }
  141. if (rmMouseStart) {
  142. rmMouseStart();
  143. }
  144. rmTouchStart = rmMouseStart = undefined;
  145. stop();
  146. }
  147. else {
  148. if (!rmTouchStart) {
  149. rmTouchStart = addEventListener(el, 'touchstart', handleTouchStart, options);
  150. }
  151. if (!rmMouseStart) {
  152. rmMouseStart = addEventListener(el, 'mousedown', handleMouseDown, options);
  153. }
  154. }
  155. };
  156. const destroy = () => {
  157. enable(false);
  158. pointerUp = pointerMove = pointerDown = undefined;
  159. };
  160. return {
  161. enable,
  162. stop,
  163. destroy,
  164. };
  165. };
  166. const getDocument = (node) => {
  167. return node instanceof Document ? node : node.ownerDocument;
  168. };
  169. const createPanRecognizer = (direction, thresh, maxAngle) => {
  170. const radians = maxAngle * (Math.PI / 180);
  171. const isDirX = direction === 'x';
  172. const maxCosine = Math.cos(radians);
  173. const threshold = thresh * thresh;
  174. let startX = 0;
  175. let startY = 0;
  176. let dirty = false;
  177. let isPan = 0;
  178. return {
  179. start(x, y) {
  180. startX = x;
  181. startY = y;
  182. isPan = 0;
  183. dirty = true;
  184. },
  185. detect(x, y) {
  186. if (!dirty) {
  187. return false;
  188. }
  189. const deltaX = x - startX;
  190. const deltaY = y - startY;
  191. const distance = deltaX * deltaX + deltaY * deltaY;
  192. if (distance < threshold) {
  193. return false;
  194. }
  195. const hypotenuse = Math.sqrt(distance);
  196. const cosine = (isDirX ? deltaX : deltaY) / hypotenuse;
  197. if (cosine > maxCosine) {
  198. isPan = 1;
  199. }
  200. else if (cosine < -maxCosine) {
  201. isPan = -1;
  202. }
  203. else {
  204. isPan = 0;
  205. }
  206. dirty = false;
  207. return true;
  208. },
  209. isGesture() {
  210. return isPan !== 0;
  211. },
  212. getDirection() {
  213. return isPan;
  214. },
  215. };
  216. };
  217. // TODO(FW-2832): types
  218. const createGesture = (config) => {
  219. let hasCapturedPan = false;
  220. let hasStartedPan = false;
  221. let hasFiredStart = true;
  222. let isMoveQueued = false;
  223. const finalConfig = Object.assign({ disableScroll: false, direction: 'x', gesturePriority: 0, passive: true, maxAngle: 40, threshold: 10 }, config);
  224. const canStart = finalConfig.canStart;
  225. const onWillStart = finalConfig.onWillStart;
  226. const onStart = finalConfig.onStart;
  227. const onEnd = finalConfig.onEnd;
  228. const notCaptured = finalConfig.notCaptured;
  229. const onMove = finalConfig.onMove;
  230. const threshold = finalConfig.threshold;
  231. const passive = finalConfig.passive;
  232. const blurOnStart = finalConfig.blurOnStart;
  233. const detail = {
  234. type: 'pan',
  235. startX: 0,
  236. startY: 0,
  237. startTime: 0,
  238. currentX: 0,
  239. currentY: 0,
  240. velocityX: 0,
  241. velocityY: 0,
  242. deltaX: 0,
  243. deltaY: 0,
  244. currentTime: 0,
  245. event: undefined,
  246. data: undefined,
  247. };
  248. const pan = createPanRecognizer(finalConfig.direction, finalConfig.threshold, finalConfig.maxAngle);
  249. const gesture = GESTURE_CONTROLLER.createGesture({
  250. name: config.gestureName,
  251. priority: config.gesturePriority,
  252. disableScroll: config.disableScroll,
  253. });
  254. const pointerDown = (ev) => {
  255. const timeStamp = now(ev);
  256. if (hasStartedPan || !hasFiredStart) {
  257. return false;
  258. }
  259. updateDetail(ev, detail);
  260. detail.startX = detail.currentX;
  261. detail.startY = detail.currentY;
  262. detail.startTime = detail.currentTime = timeStamp;
  263. detail.velocityX = detail.velocityY = detail.deltaX = detail.deltaY = 0;
  264. detail.event = ev;
  265. // Check if gesture can start
  266. if (canStart && canStart(detail) === false) {
  267. return false;
  268. }
  269. // Release fallback
  270. gesture.release();
  271. // Start gesture
  272. if (!gesture.start()) {
  273. return false;
  274. }
  275. hasStartedPan = true;
  276. if (threshold === 0) {
  277. return tryToCapturePan();
  278. }
  279. pan.start(detail.startX, detail.startY);
  280. return true;
  281. };
  282. const pointerMove = (ev) => {
  283. // fast path, if gesture is currently captured
  284. // do minimum job to get user-land even dispatched
  285. if (hasCapturedPan) {
  286. if (!isMoveQueued && hasFiredStart) {
  287. isMoveQueued = true;
  288. calcGestureData(detail, ev);
  289. requestAnimationFrame(fireOnMove);
  290. }
  291. return;
  292. }
  293. // gesture is currently being detected
  294. calcGestureData(detail, ev);
  295. if (pan.detect(detail.currentX, detail.currentY)) {
  296. if (!pan.isGesture() || !tryToCapturePan()) {
  297. abortGesture();
  298. }
  299. }
  300. };
  301. const fireOnMove = () => {
  302. // Since fireOnMove is called inside a RAF, onEnd() might be called,
  303. // we must double check hasCapturedPan
  304. if (!hasCapturedPan) {
  305. return;
  306. }
  307. isMoveQueued = false;
  308. if (onMove) {
  309. onMove(detail);
  310. }
  311. };
  312. const tryToCapturePan = () => {
  313. if (!gesture.capture()) {
  314. return false;
  315. }
  316. hasCapturedPan = true;
  317. hasFiredStart = false;
  318. // reset start position since the real user-land event starts here
  319. // If the pan detector threshold is big, not resetting the start position
  320. // will cause a jump in the animation equal to the detector threshold.
  321. // the array of positions used to calculate the gesture velocity does not
  322. // need to be cleaned, more points in the positions array always results in a
  323. // more accurate value of the velocity.
  324. detail.startX = detail.currentX;
  325. detail.startY = detail.currentY;
  326. detail.startTime = detail.currentTime;
  327. if (onWillStart) {
  328. onWillStart(detail).then(fireOnStart);
  329. }
  330. else {
  331. fireOnStart();
  332. }
  333. return true;
  334. };
  335. const blurActiveElement = () => {
  336. if (typeof document !== 'undefined') {
  337. const activeElement = document.activeElement;
  338. if (activeElement === null || activeElement === void 0 ? void 0 : activeElement.blur) {
  339. activeElement.blur();
  340. }
  341. }
  342. };
  343. const fireOnStart = () => {
  344. if (blurOnStart) {
  345. blurActiveElement();
  346. }
  347. if (onStart) {
  348. onStart(detail);
  349. }
  350. hasFiredStart = true;
  351. };
  352. const reset = () => {
  353. hasCapturedPan = false;
  354. hasStartedPan = false;
  355. isMoveQueued = false;
  356. hasFiredStart = true;
  357. gesture.release();
  358. };
  359. // END *************************
  360. const pointerUp = (ev) => {
  361. const tmpHasCaptured = hasCapturedPan;
  362. const tmpHasFiredStart = hasFiredStart;
  363. reset();
  364. if (!tmpHasFiredStart) {
  365. return;
  366. }
  367. calcGestureData(detail, ev);
  368. // Try to capture press
  369. if (tmpHasCaptured) {
  370. if (onEnd) {
  371. onEnd(detail);
  372. }
  373. return;
  374. }
  375. // Not captured any event
  376. if (notCaptured) {
  377. notCaptured(detail);
  378. }
  379. };
  380. const pointerEvents = createPointerEvents(finalConfig.el, pointerDown, pointerMove, pointerUp, {
  381. capture: false,
  382. passive,
  383. });
  384. const abortGesture = () => {
  385. reset();
  386. pointerEvents.stop();
  387. if (notCaptured) {
  388. notCaptured(detail);
  389. }
  390. };
  391. return {
  392. enable(enable = true) {
  393. if (!enable) {
  394. if (hasCapturedPan) {
  395. pointerUp(undefined);
  396. }
  397. reset();
  398. }
  399. pointerEvents.enable(enable);
  400. },
  401. destroy() {
  402. gesture.destroy();
  403. pointerEvents.destroy();
  404. },
  405. };
  406. };
  407. const calcGestureData = (detail, ev) => {
  408. if (!ev) {
  409. return;
  410. }
  411. const prevX = detail.currentX;
  412. const prevY = detail.currentY;
  413. const prevT = detail.currentTime;
  414. updateDetail(ev, detail);
  415. const currentX = detail.currentX;
  416. const currentY = detail.currentY;
  417. const timestamp = (detail.currentTime = now(ev));
  418. const timeDelta = timestamp - prevT;
  419. if (timeDelta > 0 && timeDelta < 100) {
  420. const velocityX = (currentX - prevX) / timeDelta;
  421. const velocityY = (currentY - prevY) / timeDelta;
  422. detail.velocityX = velocityX * 0.7 + detail.velocityX * 0.3;
  423. detail.velocityY = velocityY * 0.7 + detail.velocityY * 0.3;
  424. }
  425. detail.deltaX = currentX - detail.startX;
  426. detail.deltaY = currentY - detail.startY;
  427. detail.event = ev;
  428. };
  429. const updateDetail = (ev, detail) => {
  430. // get X coordinates for either a mouse click
  431. // or a touch depending on the given event
  432. let x = 0;
  433. let y = 0;
  434. if (ev) {
  435. const changedTouches = ev.changedTouches;
  436. if (changedTouches && changedTouches.length > 0) {
  437. const touch = changedTouches[0];
  438. x = touch.clientX;
  439. y = touch.clientY;
  440. }
  441. else if (ev.pageX !== undefined) {
  442. x = ev.pageX;
  443. y = ev.pageY;
  444. }
  445. }
  446. detail.currentX = x;
  447. detail.currentY = y;
  448. };
  449. const now = (ev) => {
  450. return ev.timeStamp || Date.now();
  451. };
  452. export { createGesture };