fix(@vant/touch-emulator): add SSR support (#8767)

This commit is contained in:
Amumu 2021-05-26 11:56:59 +08:00 committed by chenjiahan
parent eeac061682
commit 35683a0d05

View File

@ -4,12 +4,16 @@
* Sourcehttps://github.com/hammerjs/touchemulator * Sourcehttps://github.com/hammerjs/touchemulator
*/ */
var eventTarget; (function () {
var supportTouch = 'ontouchstart' in window; if (typeof window === 'undefined') {
return;
}
var eventTarget;
var supportTouch = 'ontouchstart' in window;
// polyfills // polyfills
if (!document.createTouch) { if (!document.createTouch) {
document.createTouch = function( document.createTouch = function (
view, view,
target, target,
identifier, identifier,
@ -34,10 +38,10 @@ if (!document.createTouch) {
0 0
); );
}; };
} }
if (!document.createTouchList) { if (!document.createTouchList) {
document.createTouchList = function() { document.createTouchList = function () {
var touchList = TouchList(); var touchList = TouchList();
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
touchList[i] = arguments[i]; touchList[i] = arguments[i];
@ -45,9 +49,9 @@ if (!document.createTouchList) {
touchList.length = arguments.length; touchList.length = arguments.length;
return touchList; return touchList;
}; };
} }
/** /**
* create an touch point * create an touch point
* @constructor * @constructor
* @param target * @param target
@ -58,7 +62,7 @@ if (!document.createTouchList) {
* @returns {Object} touchPoint * @returns {Object} touchPoint
*/ */
var Touch = function Touch(target, identifier, pos, deltaX, deltaY) { var Touch = function Touch(target, identifier, pos, deltaX, deltaY) {
deltaX = deltaX || 0; deltaX = deltaX || 0;
deltaY = deltaY || 0; deltaY = deltaY || 0;
@ -70,37 +74,37 @@ var Touch = function Touch(target, identifier, pos, deltaX, deltaY) {
this.screenY = pos.screenY + deltaY; this.screenY = pos.screenY + deltaY;
this.pageX = pos.pageX + deltaX; this.pageX = pos.pageX + deltaX;
this.pageY = pos.pageY + deltaY; this.pageY = pos.pageY + deltaY;
}; };
/** /**
* create empty touchlist with the methods * create empty touchlist with the methods
* @constructor * @constructor
* @returns touchList * @returns touchList
*/ */
function TouchList() { function TouchList() {
var touchList = []; var touchList = [];
touchList['item'] = function(index) { touchList['item'] = function (index) {
return this[index] || null; return this[index] || null;
}; };
// specified by Mozilla // specified by Mozilla
touchList['identifiedTouch'] = function(id) { touchList['identifiedTouch'] = function (id) {
return this[id + 1] || null; return this[id + 1] || null;
}; };
return touchList; return touchList;
} }
/** /**
* only trigger touches when the left mousebutton has been pressed * only trigger touches when the left mousebutton has been pressed
* @param touchType * @param touchType
* @returns {Function} * @returns {Function}
*/ */
var initiated = false; var initiated = false;
function onMouse(touchType) { function onMouse(touchType) {
return function(ev) { return function (ev) {
// prevent mouse events // prevent mouse events
if (ev.type === 'mousedown') { if (ev.type === 'mousedown') {
@ -133,14 +137,14 @@ function onMouse(touchType) {
eventTarget = null; eventTarget = null;
} }
}; };
} }
/** /**
* trigger a touch event * trigger a touch event
* @param eventName * @param eventName
* @param mouseEv * @param mouseEv
*/ */
function triggerTouch(eventName, mouseEv) { function triggerTouch(eventName, mouseEv) {
var touchEvent = document.createEvent('Event'); var touchEvent = document.createEvent('Event');
touchEvent.initEvent(eventName, true, true); touchEvent.initEvent(eventName, true, true);
@ -154,44 +158,45 @@ function triggerTouch(eventName, mouseEv) {
touchEvent.changedTouches = createTouchList(mouseEv); touchEvent.changedTouches = createTouchList(mouseEv);
eventTarget.dispatchEvent(touchEvent); eventTarget.dispatchEvent(touchEvent);
} }
/** /**
* create a touchList based on the mouse event * create a touchList based on the mouse event
* @param mouseEv * @param mouseEv
* @returns {TouchList} * @returns {TouchList}
*/ */
function createTouchList(mouseEv) { function createTouchList(mouseEv) {
var touchList = TouchList(); var touchList = TouchList();
touchList.push(new Touch(eventTarget, 1, mouseEv, 0, 0)); touchList.push(new Touch(eventTarget, 1, mouseEv, 0, 0));
return touchList; return touchList;
} }
/** /**
* receive all active touches * receive all active touches
* @param mouseEv * @param mouseEv
* @returns {TouchList} * @returns {TouchList}
*/ */
function getActiveTouches(mouseEv) { function getActiveTouches(mouseEv) {
// empty list // empty list
if (mouseEv.type === 'mouseup') { if (mouseEv.type === 'mouseup') {
return TouchList(); return TouchList();
} }
return createTouchList(mouseEv); return createTouchList(mouseEv);
} }
/** /**
* TouchEmulator initializer * TouchEmulator initializer
*/ */
function TouchEmulator() { function TouchEmulator() {
window.addEventListener('mousedown', onMouse('touchstart'), true); window.addEventListener('mousedown', onMouse('touchstart'), true);
window.addEventListener('mousemove', onMouse('touchmove'), true); window.addEventListener('mousemove', onMouse('touchmove'), true);
window.addEventListener('mouseup', onMouse('touchend'), true); window.addEventListener('mouseup', onMouse('touchend'), true);
} }
// start distance when entering the multitouch mode // start distance when entering the multitouch mode
TouchEmulator['multiTouchOffset'] = 75; TouchEmulator['multiTouchOffset'] = 75;
if (!supportTouch) { if (!supportTouch) {
new TouchEmulator(); new TouchEmulator();
} }
})();