UNPKG

2.27 kBJavaScriptView Raw
1/*!
2 * (C) Ionic http://ionicframework.com - MIT License
3 */
4import { j as clamp } from './helpers.js';
5import { i as isRTL } from './dir.js';
6import { createGesture } from './index2.js';
7
8const createSwipeBackGesture = (el, canStartHandler, onStartHandler, onMoveHandler, onEndHandler) => {
9 const win = el.ownerDocument.defaultView;
10 const rtl = isRTL(el);
11 /**
12 * Determine if a gesture is near the edge
13 * of the screen. If true, then the swipe
14 * to go back gesture should proceed.
15 */
16 const isAtEdge = (detail) => {
17 const threshold = 50;
18 const { startX } = detail;
19 if (rtl) {
20 return startX >= win.innerWidth - threshold;
21 }
22 return startX <= threshold;
23 };
24 const getDeltaX = (detail) => {
25 return rtl ? -detail.deltaX : detail.deltaX;
26 };
27 const getVelocityX = (detail) => {
28 return rtl ? -detail.velocityX : detail.velocityX;
29 };
30 const canStart = (detail) => {
31 return isAtEdge(detail) && canStartHandler();
32 };
33 const onMove = (detail) => {
34 // set the transition animation's progress
35 const delta = getDeltaX(detail);
36 const stepValue = delta / win.innerWidth;
37 onMoveHandler(stepValue);
38 };
39 const onEnd = (detail) => {
40 // the swipe back gesture has ended
41 const delta = getDeltaX(detail);
42 const width = win.innerWidth;
43 const stepValue = delta / width;
44 const velocity = getVelocityX(detail);
45 const z = width / 2.0;
46 const shouldComplete = velocity >= 0 && (velocity > 0.2 || delta > z);
47 const missing = shouldComplete ? 1 - stepValue : stepValue;
48 const missingDistance = missing * width;
49 let realDur = 0;
50 if (missingDistance > 5) {
51 const dur = missingDistance / Math.abs(velocity);
52 realDur = Math.min(dur, 540);
53 }
54 /**
55 * TODO: stepValue can sometimes return negative values
56 * or values greater than 1 which should not be possible.
57 * Need to investigate more to find where the issue is.
58 */
59 onEndHandler(shouldComplete, (stepValue <= 0) ? 0.01 : clamp(0, stepValue, 0.9999), realDur);
60 };
61 return createGesture({
62 el,
63 gestureName: 'goback-swipe',
64 gesturePriority: 40,
65 threshold: 10,
66 canStart,
67 onStart: onStartHandler,
68 onMove,
69 onEnd
70 });
71};
72
73export { createSwipeBackGesture };