swipe-back-0184f6b3.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { j as clamp } from './helpers-d94bc8ad.js';
  5. import { i as isRTL } from './dir-babeabeb.js';
  6. import { createGesture } from './index-39782642.js';
  7. import './index-cfd9c1f2.js';
  8. import './gesture-controller-314a54f6.js';
  9. const createSwipeBackGesture = (el, canStartHandler, onStartHandler, onMoveHandler, onEndHandler) => {
  10. const win = el.ownerDocument.defaultView;
  11. let rtl = isRTL(el);
  12. /**
  13. * Determine if a gesture is near the edge
  14. * of the screen. If true, then the swipe
  15. * to go back gesture should proceed.
  16. */
  17. const isAtEdge = (detail) => {
  18. const threshold = 50;
  19. const { startX } = detail;
  20. if (rtl) {
  21. return startX >= win.innerWidth - threshold;
  22. }
  23. return startX <= threshold;
  24. };
  25. const getDeltaX = (detail) => {
  26. return rtl ? -detail.deltaX : detail.deltaX;
  27. };
  28. const getVelocityX = (detail) => {
  29. return rtl ? -detail.velocityX : detail.velocityX;
  30. };
  31. const canStart = (detail) => {
  32. /**
  33. * The user's locale can change mid-session,
  34. * so we need to check text direction at
  35. * the beginning of every gesture.
  36. */
  37. rtl = isRTL(el);
  38. return isAtEdge(detail) && canStartHandler();
  39. };
  40. const onMove = (detail) => {
  41. // set the transition animation's progress
  42. const delta = getDeltaX(detail);
  43. const stepValue = delta / win.innerWidth;
  44. onMoveHandler(stepValue);
  45. };
  46. const onEnd = (detail) => {
  47. // the swipe back gesture has ended
  48. const delta = getDeltaX(detail);
  49. const width = win.innerWidth;
  50. const stepValue = delta / width;
  51. const velocity = getVelocityX(detail);
  52. const z = width / 2.0;
  53. const shouldComplete = velocity >= 0 && (velocity > 0.2 || delta > z);
  54. const missing = shouldComplete ? 1 - stepValue : stepValue;
  55. const missingDistance = missing * width;
  56. let realDur = 0;
  57. if (missingDistance > 5) {
  58. const dur = missingDistance / Math.abs(velocity);
  59. realDur = Math.min(dur, 540);
  60. }
  61. onEndHandler(shouldComplete, stepValue <= 0 ? 0.01 : clamp(0, stepValue, 0.9999), realDur);
  62. };
  63. return createGesture({
  64. el,
  65. gestureName: 'goback-swipe',
  66. /**
  67. * Swipe to go back should have priority over other horizontal swipe
  68. * gestures. These gestures have a priority of 100 which is why 101 was chosen here.
  69. */
  70. gesturePriority: 101,
  71. threshold: 10,
  72. canStart,
  73. onStart: onStartHandler,
  74. onMove,
  75. onEnd,
  76. });
  77. };
  78. export { createSwipeBackGesture };