swiftclick
Version:
Eliminates the 300ms click event delay on touch devices that support orientation change
1 lines • 3.16 kB
JavaScript
;function SwiftClick(a){function b(a){n(a)}function c(a){var b=a.target,c=b.nodeName.toLowerCase(),f=a.changedTouches[0];return void 0===l.options.elements[c]||(!!o||(l.options.useCssParser&&i(b)?(r=!1,!0):(a.stopPropagation(),o=!0,p.x=f.pageX,p.y=f.pageY,q=h(),b.removeEventListener("touchend",d,!1),b.addEventListener("touchend",d,!1),b.removeEventListener("touchcancel",e,!1),void b.addEventListener("touchcancel",e,!1))))}function d(a){var b=a.target,c=a.changedTouches[0];return b.removeEventListener("touchend",d,!1),o=!1,!!j(c)||(a.stopPropagation(),a.preventDefault(),r=!1,b.focus(),g(b,c),!1)}function e(a){a.target.removeEventListener("touchcancel",e,!1),o=!1}function f(a){var b=a.target,c=b.nodeName.toLowerCase();if(void 0!==l.options.elements[c]){if(r)return r=!1,a.stopPropagation(),a.preventDefault(),!1;r=!0}}function g(a,b){var c=document.createEvent("MouseEvents");c.initMouseEvent("click",!0,!0,window,1,b.screenX,b.screenY,b.clientX,b.clientY,!1,!1,!1,!1,0,null),a.dispatchEvent(c)}function h(){return{x:window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft||0,y:window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop||0}}function i(a){var b=a.parentNode,c=!1;if(k(a,"swiftclick-ignore"))return!0;if(k(a,"swiftclick-force"))return c;if(null===b)return c;for(;b;)k(b,"swiftclick-ignore")?(b=null,c=!0):b=b.parentNode;return c}function j(a){var b=l.options.maxTouchDrift,c=h();return Math.abs(a.pageX-p.x)>b||Math.abs(a.pageY-p.y)>b||Math.abs(c.x-q.x)>b||Math.abs(c.y-q.y)>b}function k(a,b){return void 0!==a.className&&(" "+a.className+" ").indexOf(" "+b+" ")>-1}if(void 0!==SwiftClick.swiftDictionary[a])return SwiftClick.swiftDictionary[a];SwiftClick.swiftDictionary[a]=this,this.options={elements:{a:"a",div:"div",span:"span",button:"button"},minTouchDrift:4,maxTouchDrift:16,useCssParser:!1};var l=this,m=a,n=m.onclick,o=!1,p={x:0,y:0},q={x:0,y:0},r=!1;"onorientationchange"in window&&"ontouchstart"in window&&function(){"function"==typeof n&&(m.addEventListener("click",b,!1),m.onclick=null),m.addEventListener("touchstart",c,!1),m.addEventListener("click",f,!0)}()}SwiftClick.swiftDictionary={},SwiftClick.prototype.setMaxTouchDrift=function(a){if("number"!=typeof a)throw new TypeError('expected "maxTouchDrift" to be of type "number"');a<this.options.minTouchDrift&&(a=this.options.minTouchDrift),this.options.maxTouchDrift=a},SwiftClick.prototype.addNodeNamesToTrack=function(a){var b,c=0,d=a.length;for(c;c<d;c++){if("string"!=typeof a[c])throw new TypeError('all values within the "nodeNames" array must be of type "string"');b=a[c].toLowerCase(),this.options.elements[b]=b}},SwiftClick.prototype.replaceNodeNamesToTrack=function(a){this.options.elements={},this.addNodeNamesToTrack(a)},SwiftClick.prototype.useCssParser=function(a){this.options.useCssParser=a},SwiftClick.attach=function(a){return void 0!==SwiftClick.swiftDictionary[a]?SwiftClick.swiftDictionary[a]:new SwiftClick(a)},"undefined"!=typeof define&&define.amd?define(function(){return SwiftClick}):"undefined"!=typeof module&&module.exports?module.exports=SwiftClick:window.SwiftClick=SwiftClick;