aboutsummaryrefslogtreecommitdiff
path: root/static/presentations/2021-11-13/garage/js/controllers/touch.js
blob: 5ac6a10a82035f8ae44d5c61ac8d5f7264e852b0 (plain) (blame)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { isAndroid } from '../utils/device.js'
import { matches } from '../utils/util.js'

const SWIPE_THRESHOLD = 40;

/**
 * Controls all touch interactions and navigations for
 * a presentation.
 */
export default class Touch {

	constructor( Reveal ) {

		this.Reveal = Reveal;

		// Holds information about the currently ongoing touch interaction
		this.touchStartX = 0;
		this.touchStartY = 0;
		this.touchStartCount = 0;
		this.touchCaptured = false;

		this.onPointerDown = this.onPointerDown.bind( this );
		this.onPointerMove = this.onPointerMove.bind( this );
		this.onPointerUp = this.onPointerUp.bind( this );
		this.onTouchStart = this.onTouchStart.bind( this );
		this.onTouchMove = this.onTouchMove.bind( this );
		this.onTouchEnd = this.onTouchEnd.bind( this );

	}

	/**
	 *
	 */
	bind() {

		let revealElement = this.Reveal.getRevealElement();

		if( 'onpointerdown' in window ) {
			// Use W3C pointer events
			revealElement.addEventListener( 'pointerdown', this.onPointerDown, false );
			revealElement.addEventListener( 'pointermove', this.onPointerMove, false );
			revealElement.addEventListener( 'pointerup', this.onPointerUp, false );
		}
		else if( window.navigator.msPointerEnabled ) {
			// IE 10 uses prefixed version of pointer events
			revealElement.addEventListener( 'MSPointerDown', this.onPointerDown, false );
			revealElement.addEventListener( 'MSPointerMove', this.onPointerMove, false );
			revealElement.addEventListener( 'MSPointerUp', this.onPointerUp, false );
		}
		else {
			// Fall back to touch events
			revealElement.addEventListener( 'touchstart', this.onTouchStart, false );
			revealElement.addEventListener( 'touchmove', this.onTouchMove, false );
			revealElement.addEventListener( 'touchend', this.onTouchEnd, false );
		}

	}

	/**
	 *
	 */
	unbind() {

		let revealElement = this.Reveal.getRevealElement();

		revealElement.removeEventListener( 'pointerdown', this.onPointerDown, false );
		revealElement.removeEventListener( 'pointermove', this.onPointerMove, false );
		revealElement.removeEventListener( 'pointerup', this.onPointerUp, false );

		revealElement.removeEventListener( 'MSPointerDown', this.onPointerDown, false );
		revealElement.removeEventListener( 'MSPointerMove', this.onPointerMove, false );
		revealElement.removeEventListener( 'MSPointerUp', this.onPointerUp, false );

		revealElement.removeEventListener( 'touchstart', this.onTouchStart, false );
		revealElement.removeEventListener( 'touchmove', this.onTouchMove, false );
		revealElement.removeEventListener( 'touchend', this.onTouchEnd, false );

	}

	/**
	 * Checks if the target element prevents the triggering of
	 * swipe navigation.
	 */
	isSwipePrevented( target ) {

		// Prevent accidental swipes when scrubbing timelines
		if( matches( target, 'video, audio' ) ) return true;

		while( target && typeof target.hasAttribute === 'function' ) {
			if( target.hasAttribute( 'data-prevent-swipe' ) ) return true;
			target = target.parentNode;
		}

		return false;

	}

	/**
	 * Handler for the 'touchstart' event, enables support for
	 * swipe and pinch gestures.
	 *
	 * @param {object} event
	 */
	onTouchStart( event ) {

		if( this.isSwipePrevented( event.target ) ) return true;

		this.touchStartX = event.touches[0].clientX;
		this.touchStartY = event.touches[0].clientY;
		this.touchStartCount = event.touches.length;

	}

	/**
	 * Handler for the 'touchmove' event.
	 *
	 * @param {object} event
	 */
	onTouchMove( event ) {

		if( this.isSwipePrevented( event.target ) ) return true;

		let config = this.Reveal.getConfig();

		// Each touch should only trigger one action
		if( !this.touchCaptured ) {
			this.Reveal.onUserInput( event );

			let currentX = event.touches[0].clientX;
			let currentY = event.touches[0].clientY;

			// There was only one touch point, look for a swipe
			if( event.touches.length === 1 && this.touchStartCount !== 2 ) {

				let availableRoutes = this.Reveal.availableRoutes({ includeFragments: true });

				let deltaX = currentX - this.touchStartX,
					deltaY = currentY - this.touchStartY;

				if( deltaX > SWIPE_THRESHOLD && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
					this.touchCaptured = true;
					if( config.navigationMode === 'linear' ) {
						if( config.rtl ) {
							this.Reveal.next();
						}
						else {
							this.Reveal.prev();
						}
					}
					else {
						this.Reveal.left();
					}
				}
				else if( deltaX < -SWIPE_THRESHOLD && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
					this.touchCaptured = true;
					if( config.navigationMode === 'linear' ) {
						if( config.rtl ) {
							this.Reveal.prev();
						}
						else {
							this.Reveal.next();
						}
					}
					else {
						this.Reveal.right();
					}
				}
				else if( deltaY > SWIPE_THRESHOLD && availableRoutes.up ) {
					this.touchCaptured = true;
					if( config.navigationMode === 'linear' ) {
						this.Reveal.prev();
					}
					else {
						this.Reveal.up();
					}
				}
				else if( deltaY < -SWIPE_THRESHOLD && availableRoutes.down ) {
					this.touchCaptured = true;
					if( config.navigationMode === 'linear' ) {
						this.Reveal.next();
					}
					else {
						this.Reveal.down();
					}
				}

				// If we're embedded, only block touch events if they have
				// triggered an action
				if( config.embedded ) {
					if( this.touchCaptured || this.Reveal.isVerticalSlide() ) {
						event.preventDefault();
					}
				}
				// Not embedded? Block them all to avoid needless tossing
				// around of the viewport in iOS
				else {
					event.preventDefault();
				}

			}
		}
		// There's a bug with swiping on some Android devices unless
		// the default action is always prevented
		else if( isAndroid ) {
			event.preventDefault();
		}

	}

	/**
	 * Handler for the 'touchend' event.
	 *
	 * @param {object} event
	 */
	onTouchEnd( event ) {

		this.touchCaptured = false;

	}

	/**
	 * Convert pointer down to touch start.
	 *
	 * @param {object} event
	 */
	onPointerDown( event ) {

		if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
			event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
			this.onTouchStart( event );
		}

	}

	/**
	 * Convert pointer move to touch move.
	 *
	 * @param {object} event
	 */
	onPointerMove( event ) {

		if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" )  {
			event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
			this.onTouchMove( event );
		}

	}

	/**
	 * Convert pointer up to touch end.
	 *
	 * @param {object} event
	 */
	onPointerUp( event ) {

		if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" )  {
			event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
			this.onTouchEnd( event );
		}

	}

}