onTouchStart.js 2.49 KB
Newer Older
xhw committed
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
import {
	now
} from '../../shared/utils.js';

export default function onTouchStart(event) {
	const swiper = this;
	const data = swiper.touchEventsData;
	const {
		params,
		touches,
		enabled
	} = swiper;
	if (!enabled) return;

	if (swiper.animating && params.preventInteractionOnTransition) {
		return;
	}

	if (!swiper.animating && params.cssMode && params.loop) {
		swiper.loopFix();
	}

	let e = event;
	if (e.originalEvent) e = e.originalEvent;

	data.isTouchEvent = e.type === 'touchstart' || e.type === 'touchStart' || e.type === 'onTouchstart';
	if (!data.isTouchEvent && 'which' in e && e.which === 3) return;
	if (!data.isTouchEvent && '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 !== '';


	const noSwipingSelector = params.noSwipingSelector ? params.noSwipingSelector : `.${params.noSwipingClass}`;
	const isTargetShadow = !!(e.target && e.target
		.shadowRoot
	);

	if (params.noSwiping) {
		swiper.allowClick = true;
		return;
	}

	if (params.swipeHandler) {
		if (!$targetEl.closest(params.swipeHandler)[0]) return;
	}

	touches.currentX = (e.type === 'touchstart' || e.type === 'touchStart' || e.type === 'onTouchstart') ? e.touches[0]
		.pageX : e.pageX;
	touches.currentY = (e.type === 'touchstart' || e.type === 'touchStart' || e.type === 'onTouchstart') ? e.touches[0]
		.pageY : e.pageY;
	const startX = touches.currentX;
	const startY = touches
		.currentY;

	const edgeSwipeDetection = params.edgeSwipeDetection || params.iOSEdgeSwipeDetection;
	const edgeSwipeThreshold = params.edgeSwipeThreshold || params.iOSEdgeSwipeThreshold;

	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;
	// if (e.type !== 'touchstart' && e.type !== 'touchStart') {
	// let preventDefault = true;
	// if ($targetEl.is(data.focusableElements)) preventDefault = false;

	// const shouldPreventDefault = preventDefault && swiper.allowTouchMove && params.touchStartPreventDefault;

	// if ((params.touchStartForcePreventDefault || shouldPreventDefault) && !$targetEl[0].isContentEditable) {
	// e.preventDefault();
	// }
	// }

	swiper.emit('touch-start', e);
}