| 1 |
import { |
| 2 |
PointerDragEvent, |
| 3 |
preventSelection, |
| 4 |
allowSelection, |
| 5 |
preventContextMenu, |
| 6 |
allowContextMenu, |
| 7 |
ElementDragging, |
| 8 |
} from '@fullcalendar/core/internal' |
| 9 |
import { PointerDragging } from './PointerDragging.js' |
| 10 |
import { ElementMirror } from './ElementMirror.js' |
| 11 |
import { AutoScroller } from './AutoScroller.js' |
| 12 |
|
| 13 |
/* |
| 14 |
Monitors dragging on an element. Has a number of high-level features: |
| 15 |
- minimum distance required before dragging |
| 16 |
- minimum wait time ("delay") before dragging |
| 17 |
- a mirror element that follows the pointer |
| 18 |
*/ |
| 19 |
export class FeaturefulElementDragging extends ElementDragging { |
| 20 |
pointer: PointerDragging |
| 21 |
mirror: ElementMirror |
| 22 |
autoScroller: AutoScroller |
| 23 |
|
| 24 |
// options that can be directly set by caller |
| 25 |
// the caller can also set the PointerDragging's options as well |
| 26 |
delay: number | null = null |
| 27 |
minDistance: number = 0 |
| 28 |
touchScrollAllowed: boolean = true // prevents drag from starting and blocks scrolling during drag |
| 29 |
|
| 30 |
mirrorNeedsRevert: boolean = false |
| 31 |
isInteracting: boolean = false // is the user validly moving the pointer? lasts until pointerup |
| 32 |
isDragging: boolean = false // is it INTENTFULLY dragging? lasts until after revert animation |
| 33 |
isDelayEnded: boolean = false |
| 34 |
isDistanceSurpassed: boolean = false |
| 35 |
delayTimeoutId: number | null = null |
| 36 |
|
| 37 |
constructor(private containerEl: HTMLElement, selector?: string) { |
| 38 |
super(containerEl) |
| 39 |
|
| 40 |
let pointer = this.pointer = new PointerDragging(containerEl) |
| 41 |
pointer.emitter.on('pointerdown', this.onPointerDown) |
| 42 |
pointer.emitter.on('pointermove', this.onPointerMove) |
| 43 |
pointer.emitter.on('pointerup', this.onPointerUp) |
| 44 |
|
| 45 |
if (selector) { |
| 46 |
pointer.selector = selector |
| 47 |
} |
| 48 |
|
| 49 |
this.mirror = new ElementMirror() |
| 50 |
this.autoScroller = new AutoScroller() |
| 51 |
} |
| 52 |
|
| 53 |
destroy() { |
| 54 |
this.pointer.destroy() |
| 55 |
|
| 56 |
// HACK: simulate a pointer-up to end the current drag |
| 57 |
// TODO: fire 'dragend' directly and stop interaction. discourage use of pointerup event (b/c might not fire) |
| 58 |
this.onPointerUp({} as any) |
| 59 |
} |
| 60 |
|
| 61 |
onPointerDown = (ev: PointerDragEvent) => { |
| 62 |
if (!this.isDragging) { // so new drag doesn't happen while revert animation is going |
| 63 |
this.isInteracting = true |
| 64 |
this.isDelayEnded = false |
| 65 |
this.isDistanceSurpassed = false |
| 66 |
|
| 67 |
preventSelection(document.body) |
| 68 |
preventContextMenu(document.body) |
| 69 |
|
| 70 |
// prevent links from being visited if there's an eventual drag. |
| 71 |
// also prevents selection in older browsers (maybe?). |
| 72 |
// not necessary for touch, besides, browser would complain about passiveness. |
| 73 |
if (!ev.isTouch) { |
| 74 |
ev.origEvent.preventDefault() |
| 75 |
} |
| 76 |
|
| 77 |
this.emitter.trigger('pointerdown', ev) |
| 78 |
|
| 79 |
if ( |
| 80 |
this.isInteracting && // not destroyed via pointerdown handler |
| 81 |
!this.pointer.shouldIgnoreMove |
| 82 |
) { |
| 83 |
// actions related to initiating dragstart+dragmove+dragend... |
| 84 |
|
| 85 |
this.mirror.setIsVisible(false) // reset. caller must set-visible |
| 86 |
this.mirror.start(ev.subjectEl as HTMLElement, ev.pageX, ev.pageY) // must happen on first pointer down |
| 87 |
|
| 88 |
this.startDelay(ev) |
| 89 |
|
| 90 |
if (!this.minDistance) { |
| 91 |
this.handleDistanceSurpassed(ev) |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
onPointerMove = (ev: PointerDragEvent) => { |
| 98 |
if (this.isInteracting) { |
| 99 |
this.emitter.trigger('pointermove', ev) |
| 100 |
|
| 101 |
if (!this.isDistanceSurpassed) { |
| 102 |
let minDistance = this.minDistance |
| 103 |
let distanceSq // current distance from the origin, squared |
| 104 |
let { deltaX, deltaY } = ev |
| 105 |
|
| 106 |
distanceSq = deltaX * deltaX + deltaY * deltaY |
| 107 |
if (distanceSq >= minDistance * minDistance) { // use pythagorean theorem |
| 108 |
this.handleDistanceSurpassed(ev) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (this.isDragging) { |
| 113 |
// a real pointer move? (not one simulated by scrolling) |
| 114 |
if (ev.origEvent.type !== 'scroll') { |
| 115 |
this.mirror.handleMove(ev.pageX, ev.pageY) |
| 116 |
this.autoScroller.handleMove(ev.pageX, ev.pageY) |
| 117 |
} |
| 118 |
|
| 119 |
this.emitter.trigger('dragmove', ev) |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
onPointerUp = (ev: PointerDragEvent) => { |
| 125 |
if (this.isInteracting) { |
| 126 |
this.isInteracting = false |
| 127 |
|
| 128 |
allowSelection(document.body) |
| 129 |
allowContextMenu(document.body) |
| 130 |
|
| 131 |
this.emitter.trigger('pointerup', ev) // can potentially set mirrorNeedsRevert |
| 132 |
|
| 133 |
if (this.isDragging) { |
| 134 |
this.autoScroller.stop() |
| 135 |
this.tryStopDrag(ev) // which will stop the mirror |
| 136 |
} |
| 137 |
|
| 138 |
if (this.delayTimeoutId) { |
| 139 |
clearTimeout(this.delayTimeoutId) |
| 140 |
this.delayTimeoutId = null |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
startDelay(ev: PointerDragEvent) { |
| 146 |
if (typeof this.delay === 'number') { |
| 147 |
this.delayTimeoutId = setTimeout(() => { |
| 148 |
this.delayTimeoutId = null |
| 149 |
this.handleDelayEnd(ev) |
| 150 |
}, this.delay) as any // not assignable to number! |
| 151 |
} else { |
| 152 |
this.handleDelayEnd(ev) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
handleDelayEnd(ev: PointerDragEvent) { |
| 157 |
this.isDelayEnded = true |
| 158 |
this.tryStartDrag(ev) |
| 159 |
} |
| 160 |
|
| 161 |
handleDistanceSurpassed(ev: PointerDragEvent) { |
| 162 |
this.isDistanceSurpassed = true |
| 163 |
this.tryStartDrag(ev) |
| 164 |
} |
| 165 |
|
| 166 |
tryStartDrag(ev: PointerDragEvent) { |
| 167 |
if (this.isDelayEnded && this.isDistanceSurpassed) { |
| 168 |
if (!this.pointer.wasTouchScroll || this.touchScrollAllowed) { |
| 169 |
this.isDragging = true |
| 170 |
this.mirrorNeedsRevert = false |
| 171 |
|
| 172 |
this.autoScroller.start(ev.pageX, ev.pageY, this.containerEl) |
| 173 |
this.emitter.trigger('dragstart', ev) |
| 174 |
|
| 175 |
if (this.touchScrollAllowed === false) { |
| 176 |
this.pointer.cancelTouchScroll() |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
tryStopDrag(ev: PointerDragEvent) { |
| 183 |
// .stop() is ALWAYS asynchronous, which we NEED because we want all pointerup events |
| 184 |
// that come from the document to fire beforehand. much more convenient this way. |
| 185 |
this.mirror.stop( |
| 186 |
this.mirrorNeedsRevert, |
| 187 |
this.stopDrag.bind(this, ev), // bound with args |
| 188 |
) |
| 189 |
} |
| 190 |
|
| 191 |
stopDrag(ev: PointerDragEvent) { |
| 192 |
this.isDragging = false |
| 193 |
this.emitter.trigger('dragend', ev) |
| 194 |
} |
| 195 |
|
| 196 |
// fill in the implementations... |
| 197 |
|
| 198 |
setIgnoreMove(bool: boolean) { |
| 199 |
this.pointer.shouldIgnoreMove = bool |
| 200 |
} |
| 201 |
|
| 202 |
setMirrorIsVisible(bool: boolean) { |
| 203 |
this.mirror.setIsVisible(bool) |
| 204 |
} |
| 205 |
|
| 206 |
setMirrorNeedsRevert(bool: boolean) { |
| 207 |
this.mirrorNeedsRevert = bool |
| 208 |
} |
| 209 |
|
| 210 |
setAutoScrollEnabled(bool: boolean) { |
| 211 |
this.autoScroller.isEnabled = bool |
| 212 |
} |
| 213 |
} |