UNPKG

1.73 kBJavaScriptView Raw
1'use strict';
2
3const helpers = require('./helpers-d381ec4d.js');
4const index = require('./index-a1dd5c93.js');
5require('./gesture-controller-29adda71.js');
6
7const createSwipeBackGesture = (el, canStartHandler, onStartHandler, onMoveHandler, onEndHandler) => {
8 const win = el.ownerDocument.defaultView;
9 const canStart = (detail) => {
10 return detail.startX <= 50 && canStartHandler();
11 };
12 const onMove = (detail) => {
13 // set the transition animation's progress
14 const delta = detail.deltaX;
15 const stepValue = delta / win.innerWidth;
16 onMoveHandler(stepValue);
17 };
18 const onEnd = (detail) => {
19 // the swipe back gesture has ended
20 const delta = detail.deltaX;
21 const width = win.innerWidth;
22 const stepValue = delta / width;
23 const velocity = detail.velocityX;
24 const z = width / 2.0;
25 const shouldComplete = velocity >= 0 && (velocity > 0.2 || detail.deltaX > z);
26 const missing = shouldComplete ? 1 - stepValue : stepValue;
27 const missingDistance = missing * width;
28 let realDur = 0;
29 if (missingDistance > 5) {
30 const dur = missingDistance / Math.abs(velocity);
31 realDur = Math.min(dur, 540);
32 }
33 /**
34 * TODO: stepValue can sometimes return negative values
35 * or values greater than 1 which should not be possible.
36 * Need to investigate more to find where the issue is.
37 */
38 onEndHandler(shouldComplete, (stepValue <= 0) ? 0.01 : helpers.clamp(0, stepValue, 0.9999), realDur);
39 };
40 return index.createGesture({
41 el,
42 gestureName: 'goback-swipe',
43 gesturePriority: 40,
44 threshold: 10,
45 canStart,
46 onStart: onStartHandler,
47 onMove,
48 onEnd
49 });
50};
51
52exports.createSwipeBackGesture = createSwipeBackGesture;