| 1 |
import { config, elementClosest, Emitter, PointerDragEvent } from '@fullcalendar/core/internal' |
| 2 |
|
| 3 |
config.touchMouseIgnoreWait = 500 |
| 4 |
|
| 5 |
let ignoreMouseDepth = 0 |
| 6 |
let listenerCnt = 0 |
| 7 |
let isWindowTouchMoveCancelled = false |
| 8 |
|
| 9 |
/* |
| 10 |
Uses a "pointer" abstraction, which monitors UI events for both mouse and touch. |
| 11 |
Tracks when the pointer "drags" on a certain element, meaning down+move+up. |
| 12 |
|
| 13 |
Also, tracks if there was touch-scrolling. |
| 14 |
Also, can prevent touch-scrolling from happening. |
| 15 |
Also, can fire pointermove events when scrolling happens underneath, even when no real pointer movement. |
| 16 |
|
| 17 |
emits: |
| 18 |
- pointerdown |
| 19 |
- pointermove |
| 20 |
- pointerup |
| 21 |
*/ |
| 22 |
export class PointerDragging { |
| 23 |
containerEl: EventTarget |
| 24 |
subjectEl: HTMLElement | null = null |
| 25 |
emitter: Emitter<any> |
| 26 |
|
| 27 |
// options that can be directly assigned by caller |
| 28 |
selector: string = '' // will cause subjectEl in all emitted events to be this element |
| 29 |
handleSelector: string = '' |
| 30 |
shouldIgnoreMove: boolean = false |
| 31 |
shouldWatchScroll: boolean = true // for simulating pointermove on scroll |
| 32 |
|
| 33 |
// internal states |
| 34 |
isDragging: boolean = false |
| 35 |
isTouchDragging: boolean = false |
| 36 |
wasTouchScroll: boolean = false |
| 37 |
origPageX: number |
| 38 |
origPageY: number |
| 39 |
prevPageX: number |
| 40 |
prevPageY: number |
| 41 |
prevScrollX: number // at time of last pointer pageX/pageY capture |
| 42 |
prevScrollY: number // " |
| 43 |
|
| 44 |
constructor(containerEl: EventTarget) { |
| 45 |
this.containerEl = containerEl |
| 46 |
this.emitter = new Emitter() |
| 47 |
containerEl.addEventListener('mousedown', this.handleMouseDown as EventListener) |
| 48 |
containerEl.addEventListener('touchstart', this.handleTouchStart as EventListener, { passive: true }) |
| 49 |
listenerCreated() |
| 50 |
} |
| 51 |
|
| 52 |
destroy() { |
| 53 |
this.containerEl.removeEventListener('mousedown', this.handleMouseDown as EventListener) |
| 54 |
this.containerEl.removeEventListener('touchstart', this.handleTouchStart as EventListener, { passive: true } as AddEventListenerOptions) |
| 55 |
listenerDestroyed() |
| 56 |
} |
| 57 |
|
| 58 |
tryStart(ev: UIEvent): boolean { |
| 59 |
let subjectEl = this.querySubjectEl(ev) |
| 60 |
let downEl = ev.target as HTMLElement |
| 61 |
|
| 62 |
if ( |
| 63 |
subjectEl && |
| 64 |
(!this.handleSelector || elementClosest(downEl, this.handleSelector)) |
| 65 |
) { |
| 66 |
this.subjectEl = subjectEl |
| 67 |
this.isDragging = true // do this first so cancelTouchScroll will work |
| 68 |
this.wasTouchScroll = false |
| 69 |
|
| 70 |
return true |
| 71 |
} |
| 72 |
|
| 73 |
return false |
| 74 |
} |
| 75 |
|
| 76 |
cleanup() { |
| 77 |
isWindowTouchMoveCancelled = false |
| 78 |
this.isDragging = false |
| 79 |
this.subjectEl = null |
| 80 |
// keep wasTouchScroll around for later access |
| 81 |
this.destroyScrollWatch() |
| 82 |
} |
| 83 |
|
| 84 |
querySubjectEl(ev: UIEvent): HTMLElement { |
| 85 |
if (this.selector) { |
| 86 |
return elementClosest(ev.target as HTMLElement, this.selector) |
| 87 |
} |
| 88 |
return this.containerEl as HTMLElement |
| 89 |
} |
| 90 |
|
| 91 |
// Mouse |
| 92 |
// ---------------------------------------------------------------------------------------------------- |
| 93 |
|
| 94 |
handleMouseDown = (ev: MouseEvent) => { |
| 95 |
if ( |
| 96 |
!this.shouldIgnoreMouse() && |
| 97 |
isPrimaryMouseButton(ev) && |
| 98 |
this.tryStart(ev) |
| 99 |
) { |
| 100 |
let pev = this.createEventFromMouse(ev, true) |
| 101 |
this.emitter.trigger('pointerdown', pev) |
| 102 |
this.initScrollWatch(pev) |
| 103 |
|
| 104 |
if (!this.shouldIgnoreMove) { |
| 105 |
document.addEventListener('mousemove', this.handleMouseMove) |
| 106 |
} |
| 107 |
|
| 108 |
document.addEventListener('mouseup', this.handleMouseUp) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
handleMouseMove = (ev: MouseEvent) => { |
| 113 |
let pev = this.createEventFromMouse(ev) |
| 114 |
this.recordCoords(pev) |
| 115 |
this.emitter.trigger('pointermove', pev) |
| 116 |
} |
| 117 |
|
| 118 |
handleMouseUp = (ev: MouseEvent) => { |
| 119 |
document.removeEventListener('mousemove', this.handleMouseMove) |
| 120 |
document.removeEventListener('mouseup', this.handleMouseUp) |
| 121 |
|
| 122 |
this.emitter.trigger('pointerup', this.createEventFromMouse(ev)) |
| 123 |
|
| 124 |
this.cleanup() // call last so that pointerup has access to props |
| 125 |
} |
| 126 |
|
| 127 |
shouldIgnoreMouse() { |
| 128 |
return ignoreMouseDepth || this.isTouchDragging |
| 129 |
} |
| 130 |
|
| 131 |
// Touch |
| 132 |
// ---------------------------------------------------------------------------------------------------- |
| 133 |
|
| 134 |
handleTouchStart = (ev: TouchEvent) => { |
| 135 |
if (this.tryStart(ev)) { |
| 136 |
this.isTouchDragging = true |
| 137 |
|
| 138 |
let pev = this.createEventFromTouch(ev, true) |
| 139 |
this.emitter.trigger('pointerdown', pev) |
| 140 |
this.initScrollWatch(pev) |
| 141 |
|
| 142 |
// unlike mouse, need to attach to target, not document |
| 143 |
// https://stackoverflow.com/a/45760014 |
| 144 |
let targetEl = ev.target as HTMLElement |
| 145 |
|
| 146 |
if (!this.shouldIgnoreMove) { |
| 147 |
targetEl.addEventListener('touchmove', this.handleTouchMove) |
| 148 |
} |
| 149 |
|
| 150 |
targetEl.addEventListener('touchend', this.handleTouchEnd) |
| 151 |
targetEl.addEventListener('touchcancel', this.handleTouchEnd) // treat it as a touch end |
| 152 |
|
| 153 |
// attach a handler to get called when ANY scroll action happens on the page. |
| 154 |
// this was impossible to do with normal on/off because 'scroll' doesn't bubble. |
| 155 |
// http://stackoverflow.com/a/32954565/96342 |
| 156 |
window.addEventListener( |
| 157 |
'scroll', |
| 158 |
this.handleTouchScroll, |
| 159 |
true, // useCapture |
| 160 |
) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
handleTouchMove = (ev: TouchEvent) => { |
| 165 |
let pev = this.createEventFromTouch(ev) |
| 166 |
this.recordCoords(pev) |
| 167 |
this.emitter.trigger('pointermove', pev) |
| 168 |
} |
| 169 |
|
| 170 |
handleTouchEnd = (ev: TouchEvent) => { |
| 171 |
if (this.isDragging) { // done to guard against touchend followed by touchcancel |
| 172 |
let targetEl = ev.target as HTMLElement |
| 173 |
|
| 174 |
targetEl.removeEventListener('touchmove', this.handleTouchMove) |
| 175 |
targetEl.removeEventListener('touchend', this.handleTouchEnd) |
| 176 |
targetEl.removeEventListener('touchcancel', this.handleTouchEnd) |
| 177 |
window.removeEventListener('scroll', this.handleTouchScroll, true) // useCaptured=true |
| 178 |
|
| 179 |
this.emitter.trigger('pointerup', this.createEventFromTouch(ev)) |
| 180 |
|
| 181 |
this.cleanup() // call last so that pointerup has access to props |
| 182 |
this.isTouchDragging = false |
| 183 |
startIgnoringMouse() |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
handleTouchScroll = () => { |
| 188 |
this.wasTouchScroll = true |
| 189 |
} |
| 190 |
|
| 191 |
// can be called by user of this class, to cancel touch-based scrolling for the current drag |
| 192 |
cancelTouchScroll() { |
| 193 |
if (this.isDragging) { |
| 194 |
isWindowTouchMoveCancelled = true |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// Scrolling that simulates pointermoves |
| 199 |
// ---------------------------------------------------------------------------------------------------- |
| 200 |
|
| 201 |
initScrollWatch(ev: PointerDragEvent) { |
| 202 |
if (this.shouldWatchScroll) { |
| 203 |
this.recordCoords(ev) |
| 204 |
window.addEventListener('scroll', this.handleScroll, true) // useCapture=true |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
recordCoords(ev: PointerDragEvent) { |
| 209 |
if (this.shouldWatchScroll) { |
| 210 |
this.prevPageX = (ev as any).pageX |
| 211 |
this.prevPageY = (ev as any).pageY |
| 212 |
this.prevScrollX = window.scrollX |
| 213 |
this.prevScrollY = window.scrollY |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
handleScroll = (ev: UIEvent) => { |
| 218 |
if (!this.shouldIgnoreMove) { |
| 219 |
let pageX = (window.scrollX - this.prevScrollX) + this.prevPageX |
| 220 |
let pageY = (window.scrollY - this.prevScrollY) + this.prevPageY |
| 221 |
|
| 222 |
this.emitter.trigger('pointermove', { |
| 223 |
origEvent: ev, |
| 224 |
isTouch: this.isTouchDragging, |
| 225 |
subjectEl: this.subjectEl, |
| 226 |
pageX, |
| 227 |
pageY, |
| 228 |
deltaX: pageX - this.origPageX, |
| 229 |
deltaY: pageY - this.origPageY, |
| 230 |
} as PointerDragEvent) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
destroyScrollWatch() { |
| 235 |
if (this.shouldWatchScroll) { |
| 236 |
window.removeEventListener('scroll', this.handleScroll, true) // useCaptured=true |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
// Event Normalization |
| 241 |
// ---------------------------------------------------------------------------------------------------- |
| 242 |
|
| 243 |
createEventFromMouse(ev: MouseEvent, isFirst?: boolean): PointerDragEvent { |
| 244 |
let deltaX = 0 |
| 245 |
let deltaY = 0 |
| 246 |
|
| 247 |
// TODO: repeat code |
| 248 |
if (isFirst) { |
| 249 |
this.origPageX = ev.pageX |
| 250 |
this.origPageY = ev.pageY |
| 251 |
} else { |
| 252 |
deltaX = ev.pageX - this.origPageX |
| 253 |
deltaY = ev.pageY - this.origPageY |
| 254 |
} |
| 255 |
|
| 256 |
return { |
| 257 |
origEvent: ev, |
| 258 |
isTouch: false, |
| 259 |
subjectEl: this.subjectEl, |
| 260 |
pageX: ev.pageX, |
| 261 |
pageY: ev.pageY, |
| 262 |
deltaX, |
| 263 |
deltaY, |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
createEventFromTouch(ev: TouchEvent, isFirst?: boolean): PointerDragEvent { |
| 268 |
let touches = ev.touches |
| 269 |
let pageX |
| 270 |
let pageY |
| 271 |
let deltaX = 0 |
| 272 |
let deltaY = 0 |
| 273 |
|
| 274 |
// if touch coords available, prefer, |
| 275 |
// because FF would give bad ev.pageX ev.pageY |
| 276 |
if (touches && touches.length) { |
| 277 |
pageX = touches[0].pageX |
| 278 |
pageY = touches[0].pageY |
| 279 |
} else { |
| 280 |
pageX = (ev as any).pageX |
| 281 |
pageY = (ev as any).pageY |
| 282 |
} |
| 283 |
|
| 284 |
// TODO: repeat code |
| 285 |
if (isFirst) { |
| 286 |
this.origPageX = pageX |
| 287 |
this.origPageY = pageY |
| 288 |
} else { |
| 289 |
deltaX = pageX - this.origPageX |
| 290 |
deltaY = pageY - this.origPageY |
| 291 |
} |
| 292 |
|
| 293 |
return { |
| 294 |
origEvent: ev, |
| 295 |
isTouch: true, |
| 296 |
subjectEl: this.subjectEl, |
| 297 |
pageX, |
| 298 |
pageY, |
| 299 |
deltaX, |
| 300 |
deltaY, |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
// Returns a boolean whether this was a left mouse click and no ctrl key (which means right click on Mac) |
| 306 |
function isPrimaryMouseButton(ev: MouseEvent) { |
| 307 |
return ev.button === 0 && !ev.ctrlKey |
| 308 |
} |
| 309 |
|
| 310 |
// Ignoring fake mouse events generated by touch |
| 311 |
// ---------------------------------------------------------------------------------------------------- |
| 312 |
|
| 313 |
function startIgnoringMouse() { // can be made non-class function |
| 314 |
ignoreMouseDepth += 1 |
| 315 |
|
| 316 |
setTimeout(() => { |
| 317 |
ignoreMouseDepth -= 1 |
| 318 |
}, config.touchMouseIgnoreWait) |
| 319 |
} |
| 320 |
|
| 321 |
// We want to attach touchmove as early as possible for Safari |
| 322 |
// ---------------------------------------------------------------------------------------------------- |
| 323 |
|
| 324 |
function listenerCreated() { |
| 325 |
listenerCnt += 1 |
| 326 |
|
| 327 |
if (listenerCnt === 1) { |
| 328 |
window.addEventListener('touchmove', onWindowTouchMove, { passive: false }) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
function listenerDestroyed() { |
| 333 |
listenerCnt -= 1 |
| 334 |
|
| 335 |
if (!listenerCnt) { |
| 336 |
window.removeEventListener('touchmove', onWindowTouchMove, { passive: false } as AddEventListenerOptions) |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
function onWindowTouchMove(ev: UIEvent) { |
| 341 |
if (isWindowTouchMoveCancelled) { |
| 342 |
ev.preventDefault() |
| 343 |
} |
| 344 |
} |