-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
/
onTouchStart.mjs
166 lines (148 loc) · 4.8 KB
/
onTouchStart.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { getWindow, getDocument } from 'ssr-window';
import { now, elementIsChildOf } from '../../shared/utils.mjs';
// Modified from https://stackoverflow.com/questions/54520554/custom-element-getrootnode-closest-function-crossing-multiple-parent-shadowd
function closestElement(selector, base = this) {
function __closestFrom(el) {
if (!el || el === getDocument() || el === getWindow()) return null;
if (el.assignedSlot) el = el.assignedSlot;
const found = el.closest(selector);
if (!found && !el.getRootNode) {
return null;
}
return found || __closestFrom(el.getRootNode().host);
}
return __closestFrom(base);
}
function preventEdgeSwipe(swiper, event, startX) {
const window = getWindow();
const { params } = swiper;
const edgeSwipeDetection = params.edgeSwipeDetection;
const edgeSwipeThreshold = params.edgeSwipeThreshold;
if (
edgeSwipeDetection &&
(startX <= edgeSwipeThreshold || startX >= window.innerWidth - edgeSwipeThreshold)
) {
if (edgeSwipeDetection === 'prevent') {
event.preventDefault();
return true;
}
return false;
}
return true;
}
export default function onTouchStart(event) {
const swiper = this;
const document = getDocument();
let e = event;
if (e.originalEvent) e = e.originalEvent;
const data = swiper.touchEventsData;
if (e.type === 'pointerdown') {
if (data.pointerId !== null && data.pointerId !== e.pointerId) {
return;
}
data.pointerId = e.pointerId;
} else if (e.type === 'touchstart' && e.targetTouches.length === 1) {
data.touchId = e.targetTouches[0].identifier;
}
if (e.type === 'touchstart') {
// don't proceed touch event
preventEdgeSwipe(swiper, e, e.targetTouches[0].pageX);
return;
}
const { params, touches, enabled } = swiper;
if (!enabled) return;
if (!params.simulateTouch && e.pointerType === 'mouse') return;
if (swiper.animating && params.preventInteractionOnTransition) {
return;
}
if (!swiper.animating && params.cssMode && params.loop) {
swiper.loopFix();
}
let targetEl = e.target;
if (params.touchEventsTarget === 'wrapper') {
if (!elementIsChildOf(targetEl, swiper.wrapperEl)) return;
}
if ('which' in e && e.which === 3) return;
if ('button' in e && e.button > 0) return;
if (data.isTouched && data.isMoved) return;
// change target el for shadow root component
const swipingClassHasValue = !!params.noSwipingClass && params.noSwipingClass !== '';
// eslint-disable-next-line
const eventPath = e.composedPath ? e.composedPath() : e.path;
if (swipingClassHasValue && e.target && e.target.shadowRoot && eventPath) {
targetEl = eventPath[0];
}
const noSwipingSelector = params.noSwipingSelector
? params.noSwipingSelector
: `.${params.noSwipingClass}`;
const isTargetShadow = !!(e.target && e.target.shadowRoot);
// use closestElement for shadow root element to get the actual closest for nested shadow root element
if (
params.noSwiping &&
(isTargetShadow
? closestElement(noSwipingSelector, targetEl)
: targetEl.closest(noSwipingSelector))
) {
swiper.allowClick = true;
return;
}
if (params.swipeHandler) {
if (!targetEl.closest(params.swipeHandler)) return;
}
touches.currentX = e.pageX;
touches.currentY = e.pageY;
const startX = touches.currentX;
const startY = touches.currentY;
// Do NOT start if iOS edge swipe is detected. Otherwise iOS app cannot swipe-to-go-back anymore
if (!preventEdgeSwipe(swiper, e, startX)) {
return;
}
Object.assign(data, {
isTouched: true,
isMoved: false,
allowTouchCallbacks: true,
isScrolling: undefined,
startMoving: undefined,
});
touches.startX = startX;
touches.startY = startY;
data.touchStartTime = now();
swiper.allowClick = true;
swiper.updateSize();
swiper.swipeDirection = undefined;
if (params.threshold > 0) data.allowThresholdMove = false;
let preventDefault = true;
if (targetEl.matches(data.focusableElements)) {
preventDefault = false;
if (targetEl.nodeName === 'SELECT') {
data.isTouched = false;
}
}
if (
document.activeElement &&
document.activeElement.matches(data.focusableElements) &&
document.activeElement !== targetEl &&
(e.pointerType === 'mouse' ||
(e.pointerType !== 'mouse' && !targetEl.matches(data.focusableElements)))
) {
document.activeElement.blur();
}
const shouldPreventDefault =
preventDefault && swiper.allowTouchMove && params.touchStartPreventDefault;
if (
(params.touchStartForcePreventDefault || shouldPreventDefault) &&
!targetEl.isContentEditable
) {
e.preventDefault();
}
if (
params.freeMode &&
params.freeMode.enabled &&
swiper.freeMode &&
swiper.animating &&
!params.cssMode
) {
swiper.freeMode.onTouchStart();
}
swiper.emit('touchStart', e);
}