|
| 1 | +/** |
| 2 | + * touch.ts 5.1.1 |
| 3 | + * Copyright (c) 2021 Alain Dumesny - see GridStack root license |
| 4 | + */ |
| 5 | + |
| 6 | +import { DDManager } from "./dd-manager"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Detect touch support - Windows Surface devices and other touch devices |
| 10 | + */ |
| 11 | +export const isTouch: boolean = ( 'ontouchstart' in document |
| 12 | + || 'ontouchstart' in window |
| 13 | + || !!window.TouchEvent |
| 14 | + || ((window as any).DocumentTouch && document instanceof (window as any).DocumentTouch) |
| 15 | + || navigator.maxTouchPoints > 0 |
| 16 | + || (navigator as any).msMaxTouchPoints > 0 |
| 17 | +); |
| 18 | + |
| 19 | + |
| 20 | +// interface TouchCoord {x: number, y: number}; |
| 21 | + |
| 22 | +class DDTouch { |
| 23 | + public static touchHandled: boolean; |
| 24 | + public static pointerLeaveTimeout: number; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | +* Get the x,y position of a touch event |
| 29 | +*/ |
| 30 | +// function getTouchCoords(e: TouchEvent): TouchCoord { |
| 31 | +// return { |
| 32 | +// x: e.changedTouches[0].pageX, |
| 33 | +// y: e.changedTouches[0].pageY |
| 34 | +// }; |
| 35 | +// } |
| 36 | + |
| 37 | +/** |
| 38 | + * Simulate a mouse event based on a corresponding touch event |
| 39 | + * @param {Object} e A touch event |
| 40 | + * @param {String} simulatedType The corresponding mouse event |
| 41 | + */ |
| 42 | +function simulateMouseEvent(e: TouchEvent, simulatedType: string) { |
| 43 | + |
| 44 | + // Ignore multi-touch events |
| 45 | + if (e.touches.length > 1) return; |
| 46 | + |
| 47 | + // Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors |
| 48 | + if (e.cancelable) e.preventDefault(); |
| 49 | + |
| 50 | + const touch = e.changedTouches[0], |
| 51 | + simulatedEvent = document.createEvent('MouseEvents'); |
| 52 | + |
| 53 | + // Initialize the simulated mouse event using the touch event's coordinates |
| 54 | + simulatedEvent.initMouseEvent( |
| 55 | + simulatedType, // type |
| 56 | + true, // bubbles |
| 57 | + true, // cancelable |
| 58 | + window, // view |
| 59 | + 1, // detail |
| 60 | + touch.screenX, // screenX |
| 61 | + touch.screenY, // screenY |
| 62 | + touch.clientX, // clientX |
| 63 | + touch.clientY, // clientY |
| 64 | + false, // ctrlKey |
| 65 | + false, // altKey |
| 66 | + false, // shiftKey |
| 67 | + false, // metaKey |
| 68 | + 0, // button |
| 69 | + null // relatedTarget |
| 70 | + ); |
| 71 | + |
| 72 | + // Dispatch the simulated event to the target element |
| 73 | + e.target.dispatchEvent(simulatedEvent); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Simulate a mouse event based on a corresponding Pointer event |
| 78 | + * @param {Object} e A pointer event |
| 79 | + * @param {String} simulatedType The corresponding mouse event |
| 80 | + */ |
| 81 | + function simulatePointerMouseEvent(e: PointerEvent, simulatedType: string) { |
| 82 | + |
| 83 | + // Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors |
| 84 | + if (e.cancelable) e.preventDefault(); |
| 85 | + |
| 86 | + const simulatedEvent = document.createEvent('MouseEvents'); |
| 87 | + |
| 88 | + // Initialize the simulated mouse event using the touch event's coordinates |
| 89 | + simulatedEvent.initMouseEvent( |
| 90 | + simulatedType, // type |
| 91 | + true, // bubbles |
| 92 | + true, // cancelable |
| 93 | + window, // view |
| 94 | + 1, // detail |
| 95 | + e.screenX, // screenX |
| 96 | + e.screenY, // screenY |
| 97 | + e.clientX, // clientX |
| 98 | + e.clientY, // clientY |
| 99 | + false, // ctrlKey |
| 100 | + false, // altKey |
| 101 | + false, // shiftKey |
| 102 | + false, // metaKey |
| 103 | + 0, // button |
| 104 | + null // relatedTarget |
| 105 | + ); |
| 106 | + |
| 107 | + // Dispatch the simulated event to the target element |
| 108 | + e.target.dispatchEvent(simulatedEvent); |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +/** |
| 113 | + * Handle the touchstart events |
| 114 | + * @param {Object} e The widget element's touchstart event |
| 115 | + */ |
| 116 | + export function touchstart(e: TouchEvent) { |
| 117 | + // Ignore the event if another widget is already being handled |
| 118 | + if (DDTouch.touchHandled) return; DDTouch.touchHandled = true; |
| 119 | + |
| 120 | + // Simulate the mouse events |
| 121 | + // simulateMouseEvent(e, 'mouseover'); |
| 122 | + // simulateMouseEvent(e, 'mousemove'); |
| 123 | + simulateMouseEvent(e, 'mousedown'); |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * Handle the touchmove events |
| 128 | + * @param {Object} e The document's touchmove event |
| 129 | + */ |
| 130 | +export function touchmove(e: TouchEvent) { |
| 131 | + // Ignore event if not handled by us |
| 132 | + if (!DDTouch.touchHandled) return; |
| 133 | + |
| 134 | + simulateMouseEvent(e, 'mousemove'); |
| 135 | +}; |
| 136 | + |
| 137 | +/** |
| 138 | + * Handle the touchend events |
| 139 | + * @param {Object} e The document's touchend event |
| 140 | + */ |
| 141 | +export function touchend(e: TouchEvent) { |
| 142 | + |
| 143 | + // Ignore event if not handled |
| 144 | + if (!DDTouch.touchHandled) return; |
| 145 | + |
| 146 | + // cancel delayed leave event when we release on ourself which happens BEFORE we get this! |
| 147 | + if (DDTouch.pointerLeaveTimeout) { |
| 148 | + window.clearTimeout(DDTouch.pointerLeaveTimeout); |
| 149 | + delete DDTouch.pointerLeaveTimeout; |
| 150 | + } |
| 151 | + |
| 152 | + const wasDragging = !!DDManager.dragElement; |
| 153 | + |
| 154 | + // Simulate the mouseup event |
| 155 | + simulateMouseEvent(e, 'mouseup'); |
| 156 | + // simulateMouseEvent(event, 'mouseout'); |
| 157 | + |
| 158 | + // If the touch interaction did not move, it should trigger a click |
| 159 | + if (!wasDragging) { |
| 160 | + simulateMouseEvent(e, 'click'); |
| 161 | + } |
| 162 | + |
| 163 | + // Unset the flag to allow other widgets to inherit the touch event |
| 164 | + DDTouch.touchHandled = false; |
| 165 | +}; |
| 166 | + |
| 167 | +/** |
| 168 | + * Note we don't get touchenter/touchleave (which are deprecated) |
| 169 | + * see https://stackoverflow.com/questions/27908339/js-touch-equivalent-for-mouseenter |
| 170 | + * so instead of PointerEvent to still get enter/leave and send the matching mouse event. |
| 171 | + */ |
| 172 | +export function pointerdown(e: PointerEvent) { |
| 173 | + (e.target as HTMLElement).releasePointerCapture(e.pointerId) // <- Important! |
| 174 | +} |
| 175 | + |
| 176 | +export function pointerenter(e: PointerEvent) { |
| 177 | + // ignore the initial one we get on pointerdown on ourself |
| 178 | + if (!DDManager.dragElement) { |
| 179 | + // console.log('pointerenter ignored'); |
| 180 | + return; |
| 181 | + } |
| 182 | + // console.log('pointerenter'); |
| 183 | + simulatePointerMouseEvent(e, 'mouseenter'); |
| 184 | +} |
| 185 | + |
| 186 | +export function pointerleave(e: PointerEvent) { |
| 187 | + // ignore the leave on ourself we get before releasing the mouse over ourself |
| 188 | + // by delaying sending the event and having the up event cancel us |
| 189 | + if (!DDManager.dragElement) { |
| 190 | + // console.log('pointerleave ignored'); |
| 191 | + return; |
| 192 | + } |
| 193 | + DDTouch.pointerLeaveTimeout = window.setTimeout(() => { |
| 194 | + delete DDTouch.pointerLeaveTimeout; |
| 195 | + // console.log('pointerleave delayed'); |
| 196 | + simulatePointerMouseEvent(e, 'mouseleave'); |
| 197 | + }, 10); |
| 198 | +} |
| 199 | + |
0 commit comments