Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Try to distinguish touch from mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed May 19, 2014
1 parent 828e41b commit c074958
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions src/resources/javascript/main.js
Expand Up @@ -75,29 +75,44 @@
}

function once(fn) {
var generator = function() {
var hasRun = false,
runOnceCheck = function() {
if (!hasRun) {
hasRun = true;
fn.call(null);
}
};

return runOnceCheck;
};
var hasRun = false,
runOnceCheck = function() {
if (!hasRun) {
hasRun = true;
fn.call(null);
}
};

return generator();
return runOnceCheck;
}

function onMouseDetector(fn) {
var mouseDetectorCallback = function() {
document.removeEventListener("mouseover", mouseDetectorCallbackOnce);
fn.call(null);
// Experimental code to detect if a mouse pointing device is used.
// If a mouse is detected, call the supplied function once.
var onTouchMoveEventArgs = {
target: null,
},
mouseDetectorCallbackOnce = once(mouseDetectorCallback);
onTouchMove = function(e) {
onTouchMoveEventArgs.target = e.target;
},
onMouseMove = function(e) {
// If the target isn't the same, the assumption is that the touchmove event wasn't fired first - hence it's not a touch event.
// TODO: would be better to use the mouse event .x and .y, if matching ones exist in touchmove etcetera.
if (onTouchMoveEventArgs.target !== e.target) {
onDetect();
}

// Release pointer
onTouchMoveEventArgs.target = null;
},
onDetect = once(function() {
document.removeEventListener("touchmove", onTouchMove);
document.removeEventListener("mousemove", onMouseMove);
fn.call(null);
});

document.addEventListener("mouseover", mouseDetectorCallbackOnce);
document.addEventListener("touchmove", onTouchMove);
document.addEventListener("mousemove", onMouseMove);
}

function onResizeDetector(fn) {
Expand Down

0 comments on commit c074958

Please sign in to comment.