Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
(function(window) { | ||
|
||
/** | ||
* Do not use thumbs.js on touch-enabled devices | ||
* | ||
* Thanks to Jesse MacFadyen (purplecabbage): | ||
* https://gist.github.com/850593#gistcomment-22484 | ||
*/ | ||
try { | ||
document.createEvent('TouchEvent'); | ||
return; | ||
} | ||
catch(e) { | ||
} | ||
|
||
/** | ||
* Map touch events to mouse events | ||
*/ | ||
var eventMap = { | ||
'mousedown': 'touchstart', | ||
'mouseup': 'touchend', | ||
'mousemove': 'touchmove' | ||
}; | ||
|
||
/** | ||
* Fire touch events | ||
* | ||
* Monitor mouse events and fire a touch event on the | ||
* object broadcasting the mouse event. This approach | ||
* likely has poorer performance than hijacking addEventListener | ||
* but it is a little more browser friendly. | ||
*/ | ||
window.addEventListener('load', function() { | ||
for (var key in eventMap) { | ||
document.body.addEventListener(key, function(e) { | ||
// Supports: | ||
// - addEventListener | ||
// - setAttribute | ||
var event = createTouchEvent(eventMap[e.type], e); | ||
e.target.dispatchEvent(event); | ||
|
||
// Supports: | ||
// - element.ontouchstart | ||
var fn = e.target['on' + eventMap[e.type]]; | ||
if (typeof fn === 'function') fn(e); | ||
}, false); | ||
} | ||
}, false); | ||
|
||
/** | ||
* Utility function to create a touch event. | ||
* | ||
* @param name {String} of the event | ||
* @return event {Object} | ||
*/ | ||
var createTouchEvent = function(name, e) { | ||
var event = document.createEvent('MouseEvents'); | ||
|
||
event.initMouseEvent( | ||
name, | ||
e.bubbles, | ||
e.cancelable, | ||
e.view, | ||
e.detail, | ||
e.screenX, | ||
e.screenY, | ||
e.clientX, | ||
e.clientY, | ||
e.ctrlKey, | ||
e.altKey, | ||
e.shiftKey, | ||
e.metaKey, | ||
e.button, | ||
e.relatedTarget | ||
); | ||
|
||
return event; | ||
}; | ||
|
||
})(window); |