1let emulatorInitialized = false; 2export function getScrollDistToTop() { 3 const scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0; 4 return Math.round(scrollTop); 5} 6 7export function getScrollDistToBottom() { 8 const dist = document.documentElement.scrollHeight - 9 document.documentElement.scrollTop - 10 document.documentElement.clientHeight; 11 return Math.round(dist); 12} 13 14export function initTouchEmulator() { 15 loadTouchEmulator(); 16 17} 18 19// Based on https://github.com/youzan/vant/blob/main/packages/vant-touch-emulator/src/index.js 20window.loadTouchEmulator = function () { 21 if (typeof window === 'undefined' || emulatorInitialized) { 22 return; 23 } 24 var eventTarget; 25 var supportTouch = 'ontouchstart' in window; 26 27 // polyfills 28 if (!document.createTouch) { 29 document.createTouch = function ( 30 view, 31 target, 32 identifier, 33 pageX, 34 pageY, 35 screenX, 36 screenY 37 ) { 38 // auto set 39 return new Touch( 40 target, 41 identifier, 42 { 43 pageX: pageX, 44 pageY: pageY, 45 screenX: screenX, 46 screenY: screenY, 47 clientX: pageX - window.scrollX, 48 clientY: pageY - window.scrollY, 49 }, 50 0, 51 0 52 ); 53 }; 54 } 55 56 if (!document.createTouchList) { 57 document.createTouchList = function () { 58 var touchList = TouchList(); 59 for (var i = 0; i < arguments.length; i++) { 60 touchList[i] = arguments[i]; 61 } 62 touchList.length = arguments.length; 63 return touchList; 64 }; 65 } 66 67 if (!Element.prototype.matches) { 68 Element.prototype.matches = 69 Element.prototype.msMatchesSelector || 70 Element.prototype.matches; 71 } 72 73 if (!Element.prototype.closest) { 74 Element.prototype.closest = function (s) { 75 var el = this; 76 77 do { 78 if (el.matches(s)) return el; 79 el = el.parentElement || el.parentNode; 80 } while (el !== null && el.nodeType === 1); 81 82 return null; 83 }; 84 } 85 86 /** 87 * create an touch point 88 * @constructor 89 * @param target 90 * @param identifier 91 * @param pos 92 * @param deltaX 93 * @param deltaY 94 * @returns {Object} touchPoint 95 */ 96 97 var Touch = function Touch(target, identifier, pos, deltaX, deltaY) { 98 deltaX = deltaX || 0; 99 deltaY = deltaY || 0; 100 101 this.identifier = identifier; 102 this.target = target; 103 this.clientX = pos.clientX + deltaX; 104 this.clientY = pos.clientY + deltaY; 105 this.screenX = pos.screenX + deltaX; 106 this.screenY = pos.screenY + deltaY; 107 this.pageX = pos.pageX + deltaX; 108 this.pageY = pos.pageY + deltaY; 109 }; 110 111 /** 112 * create empty touchlist with the methods 113 * @constructor 114 * @returns touchList 115 */ 116 function TouchList() { 117 var touchList = []; 118 119 touchList['item'] = function (index) { 120 return this[index] || null; 121 }; 122 123 // specified by Mozilla 124 touchList['identifiedTouch'] = function (id) { 125 return this[id + 1] || null; 126 }; 127 128 return touchList; 129 } 130 131 /** 132 * only trigger touches when the left mousebutton has been pressed 133 * @param touchType 134 * @returns {Function} 135 */ 136 137 var initiated = false; 138 function onMouse(touchType) { 139 return function (ev) { 140 // prevent mouse events 141 142 if (ev.type === 'mousedown') { 143 initiated = true; 144 } 145 146 if (ev.type === 'mouseup') { 147 initiated = false; 148 } 149 150 if (ev.type === 'mousemove' && !initiated) { 151 return; 152 } 153 154 // The EventTarget on which the touch point started when it was first placed on the surface, 155 // even if the touch point has since moved outside the interactive area of that element. 156 // also, when the target doesnt exist anymore, we update it 157 if ( 158 ev.type === 'mousedown' || 159 !eventTarget || 160 (eventTarget && !eventTarget.dispatchEvent) 161 ) { 162 eventTarget = ev.target; 163 } 164 165 if (eventTarget.closest('[data-no-touch-simulate]') == null) { 166 triggerTouch(touchType, ev); 167 } 168 169 // reset 170 if (ev.type === 'mouseup') { 171 eventTarget = null; 172 } 173 }; 174 } 175 176 /** 177 * trigger a touch event 178 * @param eventName 179 * @param mouseEv 180 */ 181 function triggerTouch(eventName, mouseEv) { 182 var touchEvent = new Event(eventName, { bubbles: true, cancelable: true });
183 //document.createEvent('Event'); 184 //touchEvent.initEvent(eventName, true, true); 185 186 touchEvent.altKey = mouseEv.altKey; 187 touchEvent.ctrlKey = mouseEv.ctrlKey; 188 touchEvent.metaKey = mouseEv.metaKey; 189 touchEvent.shiftKey = mouseEv.shiftKey; 190 191 touchEvent.touches = getActiveTouches(mouseEv); 192 touchEvent.targetTouches = getActiveTouches(mouseEv); 193 touchEvent.changedTouches = createTouchList(mouseEv); 194 195 eventTarget.dispatchEvent(touchEvent); 196 } 197 198 /** 199 * create a touchList based on the mouse event 200 * @param mouseEv 201 * @returns {TouchList} 202 */ 203 function createTouchList(mouseEv) { 204 var touchList = TouchList(); 205 touchList.push(new Touch(eventTarget, 1, mouseEv, 0, 0)); 206 return touchList; 207 } 208 209 /** 210 * receive all active touches 211 * @param mouseEv 212 * @returns {TouchList} 213 */ 214 function getActiveTouches(mouseEv) { 215 // empty list 216 if (mouseEv.type === 'mouseup') { 217 return TouchList(); 218 } 219 return createTouchList(mouseEv); 220 } 221 222 /** 223 * TouchEmulator initializer 224 */ 225 function TouchEmulator() { 226 window.addEventListener('mousedown', onMouse('touchstart'), true); 227 window.addEventListener('mousemove', onMouse('touchmove'), true); 228 window.addEventListener('mouseup', onMouse('touchend'), true); 229 } 230 231 // start distance when entering the multitouch mode 232 TouchEmulator['multiTouchOffset'] = 75; 233 234 if (!supportTouch) { 235 new TouchEmulator(); 236 } 237 238 emulatorInitialized = true; 239};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.