Skip to content

Instantly share code, notes, and snippets.

@Splurov
Last active January 1, 2016 05:59
Show Gist options
  • Select an option

  • Save Splurov/8102237 to your computer and use it in GitHub Desktop.

Select an option

Save Splurov/8102237 to your computer and use it in GitHub Desktop.
var $RegisterUniversalClick = function(target, listener, isMiddleClickTriggers) {
var touchSupported = ('ontouchstart' in window);
var lastEventSource;
if (touchSupported) {
var tapping;
target.addEventListener('touchstart', function(e) {
if (lastEventSource === 'mouse') {
lastEventSource = null;
tapping = false;
} else {
lastEventSource = 'touch';
tapping = true;
}
}, false);
target.addEventListener('touchmove', function() {
tapping = false;
}, false);
target.addEventListener('touchcancel', function() {
tapping = false;
}, false);
target.addEventListener('touchend', function(e) {
if (tapping) {
listener(e);
console.log('TOUCH');
}
}, false);
}
if (!window.mkIsMobile || !touchSupported) {
if (isMiddleClickTriggers) {
var clicking;
target.addEventListener('mousedown', function() {
if (lastEventSource === 'touch') {
lastEventSource = null;
clicking = false;
} else {
lastEventSource = 'mouse';
clicking = true;
}
}, false);
target.addEventListener('mousemove', function() {
clicking = false;
}, false);
target.addEventListener('mouseup', function(e) {
if (clicking && e.which !== 3) {
listener(e);
console.log('MOUSE MIDDLE OR CLICK: ' + e.which);
}
}, false);
target.addEventListener('keypress', function(e) {
var code = e.keyCode || e.which;
if ([13, 32].indexOf(code) !== -1) {
listener(e);
console.log('KEYBOARD');
}
}, false);
} else {
target.addEventListener('click', function(e) {
if (lastEventSource === 'touch') {
lastEventSource = null;
} else {
lastEventSource = 'mouse';
listener(e);
console.log('CLICK');
}
}, false);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment