1 |
|
2 |
|
3 |
|
4 | import { j as clamp } from './helpers.js';
|
5 | import { i as isRTL } from './dir.js';
|
6 | import { createGesture } from './index2.js';
|
7 |
|
8 | const createSwipeBackGesture = (el, canStartHandler, onStartHandler, onMoveHandler, onEndHandler) => {
|
9 | const win = el.ownerDocument.defaultView;
|
10 | const rtl = isRTL(el);
|
11 | |
12 |
|
13 |
|
14 |
|
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 |
|
35 | const delta = getDeltaX(detail);
|
36 | const stepValue = delta / win.innerWidth;
|
37 | onMoveHandler(stepValue);
|
38 | };
|
39 | const onEnd = (detail) => {
|
40 |
|
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 |
|
56 |
|
57 |
|
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 |
|
73 | export { createSwipeBackGesture };
|