| /* |
| The MIT License |
| |
| Copyright (c) 2010 Michael Brooks |
| |
| Permission is hereby granted, free of charge, to any person obtaining a copy |
| of this software and associated documentation files (the "Software"), to deal |
| in the Software without restriction, including without limitation the rights |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| copies of the Software, and to permit persons to whom the Software is |
| furnished to do so, subject to the following conditions: |
| |
| The above copyright notice and this permission notice shall be included in |
| all copies or substantial portions of the Software. |
| |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| THE SOFTWARE. |
| */ |
| |
| (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); |