1 | import { getDocument } from 'ssr-window';
|
2 | import onTouchStart from './onTouchStart.js';
|
3 | import onTouchMove from './onTouchMove.js';
|
4 | import onTouchEnd from './onTouchEnd.js';
|
5 | import onResize from './onResize.js';
|
6 | import onClick from './onClick.js';
|
7 | import onScroll from './onScroll.js';
|
8 | let dummyEventAttached = false;
|
9 |
|
10 | function dummyEventListener() {}
|
11 |
|
12 | const events = (swiper, method) => {
|
13 | const document = getDocument();
|
14 | const {
|
15 | params,
|
16 | touchEvents,
|
17 | el,
|
18 | wrapperEl,
|
19 | device,
|
20 | support
|
21 | } = swiper;
|
22 | const capture = !!params.nested;
|
23 | const domMethod = method === 'on' ? 'addEventListener' : 'removeEventListener';
|
24 | const swiperMethod = method;
|
25 |
|
26 | if (!support.touch) {
|
27 | el[domMethod](touchEvents.start, swiper.onTouchStart, false);
|
28 | document[domMethod](touchEvents.move, swiper.onTouchMove, capture);
|
29 | document[domMethod](touchEvents.end, swiper.onTouchEnd, false);
|
30 | } else {
|
31 | const passiveListener = touchEvents.start === 'touchstart' && support.passiveListener && params.passiveListeners ? {
|
32 | passive: true,
|
33 | capture: false
|
34 | } : false;
|
35 | el[domMethod](touchEvents.start, swiper.onTouchStart, passiveListener);
|
36 | el[domMethod](touchEvents.move, swiper.onTouchMove, support.passiveListener ? {
|
37 | passive: false,
|
38 | capture
|
39 | } : capture);
|
40 | el[domMethod](touchEvents.end, swiper.onTouchEnd, passiveListener);
|
41 |
|
42 | if (touchEvents.cancel) {
|
43 | el[domMethod](touchEvents.cancel, swiper.onTouchEnd, passiveListener);
|
44 | }
|
45 | }
|
46 |
|
47 |
|
48 | if (params.preventClicks || params.preventClicksPropagation) {
|
49 | el[domMethod]('click', swiper.onClick, true);
|
50 | }
|
51 |
|
52 | if (params.cssMode) {
|
53 | wrapperEl[domMethod]('scroll', swiper.onScroll);
|
54 | }
|
55 |
|
56 |
|
57 | if (params.updateOnWindowResize) {
|
58 | swiper[swiperMethod](device.ios || device.android ? 'resize orientationchange observerUpdate' : 'resize observerUpdate', onResize, true);
|
59 | } else {
|
60 | swiper[swiperMethod]('observerUpdate', onResize, true);
|
61 | }
|
62 | };
|
63 |
|
64 | function attachEvents() {
|
65 | const swiper = this;
|
66 | const document = getDocument();
|
67 | const {
|
68 | params,
|
69 | support
|
70 | } = swiper;
|
71 | swiper.onTouchStart = onTouchStart.bind(swiper);
|
72 | swiper.onTouchMove = onTouchMove.bind(swiper);
|
73 | swiper.onTouchEnd = onTouchEnd.bind(swiper);
|
74 |
|
75 | if (params.cssMode) {
|
76 | swiper.onScroll = onScroll.bind(swiper);
|
77 | }
|
78 |
|
79 | swiper.onClick = onClick.bind(swiper);
|
80 |
|
81 | if (support.touch && !dummyEventAttached) {
|
82 | document.addEventListener('touchstart', dummyEventListener);
|
83 | dummyEventAttached = true;
|
84 | }
|
85 |
|
86 | events(swiper, 'on');
|
87 | }
|
88 |
|
89 | function detachEvents() {
|
90 | const swiper = this;
|
91 | events(swiper, 'off');
|
92 | }
|
93 |
|
94 | export default {
|
95 | attachEvents,
|
96 | detachEvents
|
97 | }; |
\ | No newline at end of file |