initSwipe()

in static/src/javascripts/projects/commercial/modules/hosted/gallery.js [156:217]


	initSwipe() {
		let threshold; // time in ms
		let ox;
		let dx;
		const updateTime = 20;
		this.$imagesContainer.style.setProperty(
			'width',
			`${this.$images.length}00%`,
		);

		this.$galleryEl.addEventListener('touchstart', (e) => {
			threshold = this.swipeContainerWidth * this.swipeThreshold;
			ox = e.touches[0].pageX;
			dx = 0;
		});

		const touchMove = (e) => {
			e.preventDefault();
			if (e.touches.length > 1 || (e.scale && e.scale !== 1)) {
				return;
			}
			dx = e.touches[0].pageX - ox;
			this.translateContent(this.index, dx, updateTime);
		};

		this.$galleryEl.addEventListener(
			'touchmove',
			throttle(touchMove, updateTime, {
				trailing: false,
			}),
		);

		this.$galleryEl.addEventListener('touchend', () => {
			let direction;
			if (Math.abs(dx) > threshold) {
				direction = dx > threshold ? 1 : -1;
			} else {
				direction = 0;
			}
			dx = 0;

			if (direction === 1) {
				if (this.index > 1) {
					this.trigger('prev', {
						nav: 'Swipe',
					});
				} else {
					this.trigger('reload');
				}
			} else if (direction === -1) {
				if (this.index < this.$images.length) {
					this.trigger('next', {
						nav: 'Swipe',
					});
				} else {
					this.trigger('reload');
				}
			} else {
				this.trigger('reload');
			}
		});
	}