-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
/
index.js
86 lines (71 loc) · 2.41 KB
/
index.js
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
import { getDocument } from 'ssr-window';
import onTouchStart from './onTouchStart.js';
import onTouchMove from './onTouchMove.js';
import onTouchEnd from './onTouchEnd.js';
import onResize from './onResize.js';
import onClick from './onClick.js';
import onScroll from './onScroll.js';
import onLoad from './onLoad.js';
let dummyEventAttached = false;
function dummyEventListener() {}
const events = (swiper, method) => {
const document = getDocument();
const { params, el, wrapperEl, device } = swiper;
const capture = !!params.nested;
const domMethod = method === 'on' ? 'addEventListener' : 'removeEventListener';
const swiperMethod = method;
// Touch Events
el[domMethod]('pointerdown', swiper.onTouchStart, { passive: false });
document[domMethod]('pointermove', swiper.onTouchMove, { passive: false, capture });
document[domMethod]('pointerup', swiper.onTouchEnd, { passive: true });
document[domMethod]('pointercancel', swiper.onTouchEnd, { passive: true });
document[domMethod]('pointerout', swiper.onTouchEnd, { passive: true });
document[domMethod]('pointerleave', swiper.onTouchEnd, { passive: true });
// Prevent Links Clicks
if (params.preventClicks || params.preventClicksPropagation) {
el[domMethod]('click', swiper.onClick, true);
}
if (params.cssMode) {
wrapperEl[domMethod]('scroll', swiper.onScroll);
}
// Resize handler
if (params.updateOnWindowResize) {
swiper[swiperMethod](
device.ios || device.android
? 'resize orientationchange observerUpdate'
: 'resize observerUpdate',
onResize,
true,
);
} else {
swiper[swiperMethod]('observerUpdate', onResize, true);
}
// Images loader
el[domMethod]('load', swiper.onLoad, { capture: true });
};
function attachEvents() {
const swiper = this;
const document = getDocument();
const { params } = swiper;
swiper.onTouchStart = onTouchStart.bind(swiper);
swiper.onTouchMove = onTouchMove.bind(swiper);
swiper.onTouchEnd = onTouchEnd.bind(swiper);
if (params.cssMode) {
swiper.onScroll = onScroll.bind(swiper);
}
swiper.onClick = onClick.bind(swiper);
swiper.onLoad = onLoad.bind(swiper);
if (!dummyEventAttached) {
document.addEventListener('touchstart', dummyEventListener);
dummyEventAttached = true;
}
events(swiper, 'on');
}
function detachEvents() {
const swiper = this;
events(swiper, 'off');
}
export default {
attachEvents,
detachEvents,
};