Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame^] | 1 | import { isAndroid } from '../utils/device.js' |
| 2 | import { matches } from '../utils/util.js' |
| 3 | |
| 4 | const SWIPE_THRESHOLD = 40; |
| 5 | |
| 6 | /** |
| 7 | * Controls all touch interactions and navigations for |
| 8 | * a presentation. |
| 9 | */ |
| 10 | export default class Touch { |
| 11 | |
| 12 | constructor( Reveal ) { |
| 13 | |
| 14 | this.Reveal = Reveal; |
| 15 | |
| 16 | // Holds information about the currently ongoing touch interaction |
| 17 | this.touchStartX = 0; |
| 18 | this.touchStartY = 0; |
| 19 | this.touchStartCount = 0; |
| 20 | this.touchCaptured = false; |
| 21 | |
| 22 | this.onPointerDown = this.onPointerDown.bind( this ); |
| 23 | this.onPointerMove = this.onPointerMove.bind( this ); |
| 24 | this.onPointerUp = this.onPointerUp.bind( this ); |
| 25 | this.onTouchStart = this.onTouchStart.bind( this ); |
| 26 | this.onTouchMove = this.onTouchMove.bind( this ); |
| 27 | this.onTouchEnd = this.onTouchEnd.bind( this ); |
| 28 | |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * |
| 33 | */ |
| 34 | bind() { |
| 35 | |
| 36 | let revealElement = this.Reveal.getRevealElement(); |
| 37 | |
| 38 | if( 'onpointerdown' in window ) { |
| 39 | // Use W3C pointer events |
| 40 | revealElement.addEventListener( 'pointerdown', this.onPointerDown, false ); |
| 41 | revealElement.addEventListener( 'pointermove', this.onPointerMove, false ); |
| 42 | revealElement.addEventListener( 'pointerup', this.onPointerUp, false ); |
| 43 | } |
| 44 | else if( window.navigator.msPointerEnabled ) { |
| 45 | // IE 10 uses prefixed version of pointer events |
| 46 | revealElement.addEventListener( 'MSPointerDown', this.onPointerDown, false ); |
| 47 | revealElement.addEventListener( 'MSPointerMove', this.onPointerMove, false ); |
| 48 | revealElement.addEventListener( 'MSPointerUp', this.onPointerUp, false ); |
| 49 | } |
| 50 | else { |
| 51 | // Fall back to touch events |
| 52 | revealElement.addEventListener( 'touchstart', this.onTouchStart, false ); |
| 53 | revealElement.addEventListener( 'touchmove', this.onTouchMove, false ); |
| 54 | revealElement.addEventListener( 'touchend', this.onTouchEnd, false ); |
| 55 | } |
| 56 | |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | */ |
| 62 | unbind() { |
| 63 | |
| 64 | let revealElement = this.Reveal.getRevealElement(); |
| 65 | |
| 66 | revealElement.removeEventListener( 'pointerdown', this.onPointerDown, false ); |
| 67 | revealElement.removeEventListener( 'pointermove', this.onPointerMove, false ); |
| 68 | revealElement.removeEventListener( 'pointerup', this.onPointerUp, false ); |
| 69 | |
| 70 | revealElement.removeEventListener( 'MSPointerDown', this.onPointerDown, false ); |
| 71 | revealElement.removeEventListener( 'MSPointerMove', this.onPointerMove, false ); |
| 72 | revealElement.removeEventListener( 'MSPointerUp', this.onPointerUp, false ); |
| 73 | |
| 74 | revealElement.removeEventListener( 'touchstart', this.onTouchStart, false ); |
| 75 | revealElement.removeEventListener( 'touchmove', this.onTouchMove, false ); |
| 76 | revealElement.removeEventListener( 'touchend', this.onTouchEnd, false ); |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Checks if the target element prevents the triggering of |
| 82 | * swipe navigation. |
| 83 | */ |
| 84 | isSwipePrevented( target ) { |
| 85 | |
| 86 | // Prevent accidental swipes when scrubbing timelines |
| 87 | if( matches( target, 'video, audio' ) ) return true; |
| 88 | |
| 89 | while( target && typeof target.hasAttribute === 'function' ) { |
| 90 | if( target.hasAttribute( 'data-prevent-swipe' ) ) return true; |
| 91 | target = target.parentNode; |
| 92 | } |
| 93 | |
| 94 | return false; |
| 95 | |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Handler for the 'touchstart' event, enables support for |
| 100 | * swipe and pinch gestures. |
| 101 | * |
| 102 | * @param {object} event |
| 103 | */ |
| 104 | onTouchStart( event ) { |
| 105 | |
| 106 | if( this.isSwipePrevented( event.target ) ) return true; |
| 107 | |
| 108 | this.touchStartX = event.touches[0].clientX; |
| 109 | this.touchStartY = event.touches[0].clientY; |
| 110 | this.touchStartCount = event.touches.length; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Handler for the 'touchmove' event. |
| 116 | * |
| 117 | * @param {object} event |
| 118 | */ |
| 119 | onTouchMove( event ) { |
| 120 | |
| 121 | if( this.isSwipePrevented( event.target ) ) return true; |
| 122 | |
| 123 | let config = this.Reveal.getConfig(); |
| 124 | |
| 125 | // Each touch should only trigger one action |
| 126 | if( !this.touchCaptured ) { |
| 127 | this.Reveal.onUserInput( event ); |
| 128 | |
| 129 | let currentX = event.touches[0].clientX; |
| 130 | let currentY = event.touches[0].clientY; |
| 131 | |
| 132 | // There was only one touch point, look for a swipe |
| 133 | if( event.touches.length === 1 && this.touchStartCount !== 2 ) { |
| 134 | |
| 135 | let availableRoutes = this.Reveal.availableRoutes({ includeFragments: true }); |
| 136 | |
| 137 | let deltaX = currentX - this.touchStartX, |
| 138 | deltaY = currentY - this.touchStartY; |
| 139 | |
| 140 | if( deltaX > SWIPE_THRESHOLD && Math.abs( deltaX ) > Math.abs( deltaY ) ) { |
| 141 | this.touchCaptured = true; |
| 142 | if( config.navigationMode === 'linear' ) { |
| 143 | if( config.rtl ) { |
| 144 | this.Reveal.next(); |
| 145 | } |
| 146 | else { |
| 147 | this.Reveal.prev(); |
| 148 | } |
| 149 | } |
| 150 | else { |
| 151 | this.Reveal.left(); |
| 152 | } |
| 153 | } |
| 154 | else if( deltaX < -SWIPE_THRESHOLD && Math.abs( deltaX ) > Math.abs( deltaY ) ) { |
| 155 | this.touchCaptured = true; |
| 156 | if( config.navigationMode === 'linear' ) { |
| 157 | if( config.rtl ) { |
| 158 | this.Reveal.prev(); |
| 159 | } |
| 160 | else { |
| 161 | this.Reveal.next(); |
| 162 | } |
| 163 | } |
| 164 | else { |
| 165 | this.Reveal.right(); |
| 166 | } |
| 167 | } |
| 168 | else if( deltaY > SWIPE_THRESHOLD && availableRoutes.up ) { |
| 169 | this.touchCaptured = true; |
| 170 | if( config.navigationMode === 'linear' ) { |
| 171 | this.Reveal.prev(); |
| 172 | } |
| 173 | else { |
| 174 | this.Reveal.up(); |
| 175 | } |
| 176 | } |
| 177 | else if( deltaY < -SWIPE_THRESHOLD && availableRoutes.down ) { |
| 178 | this.touchCaptured = true; |
| 179 | if( config.navigationMode === 'linear' ) { |
| 180 | this.Reveal.next(); |
| 181 | } |
| 182 | else { |
| 183 | this.Reveal.down(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // If we're embedded, only block touch events if they have |
| 188 | // triggered an action |
| 189 | if( config.embedded ) { |
| 190 | if( this.touchCaptured || this.Reveal.isVerticalSlide() ) { |
| 191 | event.preventDefault(); |
| 192 | } |
| 193 | } |
| 194 | // Not embedded? Block them all to avoid needless tossing |
| 195 | // around of the viewport in iOS |
| 196 | else { |
| 197 | event.preventDefault(); |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | } |
| 202 | // There's a bug with swiping on some Android devices unless |
| 203 | // the default action is always prevented |
| 204 | else if( isAndroid ) { |
| 205 | event.preventDefault(); |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Handler for the 'touchend' event. |
| 212 | * |
| 213 | * @param {object} event |
| 214 | */ |
| 215 | onTouchEnd( event ) { |
| 216 | |
| 217 | this.touchCaptured = false; |
| 218 | |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Convert pointer down to touch start. |
| 223 | * |
| 224 | * @param {object} event |
| 225 | */ |
| 226 | onPointerDown( event ) { |
| 227 | |
| 228 | if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) { |
| 229 | event.touches = [{ clientX: event.clientX, clientY: event.clientY }]; |
| 230 | this.onTouchStart( event ); |
| 231 | } |
| 232 | |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Convert pointer move to touch move. |
| 237 | * |
| 238 | * @param {object} event |
| 239 | */ |
| 240 | onPointerMove( event ) { |
| 241 | |
| 242 | if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) { |
| 243 | event.touches = [{ clientX: event.clientX, clientY: event.clientY }]; |
| 244 | this.onTouchMove( event ); |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Convert pointer up to touch end. |
| 251 | * |
| 252 | * @param {object} event |
| 253 | */ |
| 254 | onPointerUp( event ) { |
| 255 | |
| 256 | if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) { |
| 257 | event.touches = [{ clientX: event.clientX, clientY: event.clientY }]; |
| 258 | this.onTouchEnd( event ); |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | } |