Skip to content

Commit

Permalink
Add ability to stop and restart the emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
hashchange committed Mar 3, 2017
1 parent 0ec6e7b commit 2e8469f
Showing 1 changed file with 61 additions and 13 deletions.
74 changes: 61 additions & 13 deletions touch-emulator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(function(window, document, exportName, undefined) {
"use strict";

var isRunning = false;
var isMultiTouch = false;
var multiTouchStartPos;
var eventTarget;
var touchElements = {};
var fakedProps = [];
var handlers = [];

// polyfills
if(!document.createTouch) {
Expand Down Expand Up @@ -94,11 +97,23 @@
for(var p=0; p<props.length; p++) {
if(objs[o] && objs[o][props[p]] == undefined) {
objs[o][props[p]] = null;
fakedProps.push({prop: props[p], context: objs[o]});
}
}
}
}

/**
* revert to the state without fake touch support
*/
function removeFakeTouchSupport() {
for(var i=0; i<fakedProps.length; i++) {
delete fakedProps[i].context[fakedProps[i].prop];
}

fakedProps = [];
}

/**
* we don't have to emulate on a touch device
* @returns {boolean}
Expand Down Expand Up @@ -287,31 +302,64 @@
}
}

/**
* add an event handler and keep track of it
* @param {string} event
* @param {Function} handler
*/
function addHandler(event, handler) {
handlers.push({event: event, handler: handler});
window.addEventListener(event, handler, true);
}

/**
* remove all event handlers which have been added by the emulator
*/
function removeHandlers() {
for(var i=0; i<handlers.length; i++) {
window.removeEventListener(handlers[i].event, handlers[i].handler, true);
}

handlers = [];
}

/**
* TouchEmulator initializer
*/
function TouchEmulator() {
if (hasTouchSupport()) {
TouchEmulator.start();
}

TouchEmulator.start = function () {
if (isRunning || hasTouchSupport()) {
return;
}

fakeTouchSupport();

window.addEventListener("mousedown", onMouse('touchstart'), true);
window.addEventListener("mousemove", onMouse('touchmove'), true);
window.addEventListener("mouseup", onMouse('touchend'), true);
addHandler("mousedown", onMouse('touchstart'));
addHandler("mousemove", onMouse('touchmove'));
addHandler("mouseup", onMouse('touchend'));

window.addEventListener("mouseenter", preventMouseEvents, true);
window.addEventListener("mouseleave", preventMouseEvents, true);
window.addEventListener("mouseout", preventMouseEvents, true);
window.addEventListener("mouseover", preventMouseEvents, true);
addHandler("mouseenter", preventMouseEvents);
addHandler("mouseleave", preventMouseEvents);
addHandler("mouseout", preventMouseEvents);
addHandler("mouseover", preventMouseEvents);

// it uses itself!
window.addEventListener("touchstart", showTouches, true);
window.addEventListener("touchmove", showTouches, true);
window.addEventListener("touchend", showTouches, true);
window.addEventListener("touchcancel", showTouches, true);
}
addHandler("touchstart", showTouches);
addHandler("touchmove", showTouches);
addHandler("touchend", showTouches);
addHandler("touchcancel", showTouches);

isRunning = true;
};

TouchEmulator.stop = function () {
removeFakeTouchSupport();
removeHandlers();
isRunning = false;
};

// start distance when entering the multitouch mode
TouchEmulator.multiTouchOffset = 75;
Expand Down

0 comments on commit 2e8469f

Please sign in to comment.