forked from jairajs89/Touchy.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchy.min.js
14 lines (13 loc) · 3.71 KB
/
touchy.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
Touchy.js
Socket-style finger management for touch events
Jairaj Sethi
*/
(function(){function o(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent&&(a=a.attachEvent("on"+b,c),h[a]={name:b,callback:c})}function n(a){this.id=a;this.points=[];this.callbacks={start:[],move:[],end:[]}}function j(a){this.fingers=!a?[]:p(a,function(a){return new n(a)});this.callbacks={start:[],move:[],end:[]}}function k(a,b){return p(a,function(a){return{id:a.identifier,x:a.pageX,y:a.pageY,time:b}})}function l(a,b){function c(a){var c=[];d(a,function(a){var b=new n(a.id);b.points.push(a);
c.push([b,a]);i.add(b)});d(c,function(a){b.any&&b.any(i,a[0]);a[0].trigger("start",a[1])});i.trigger("start",a)}function e(a){var b=[];d(a,function(a){var c=i.get(a.id);c.points.push(a);b.push([c,a])});d(b,function(a){a[0].trigger("move",a[1])});i.trigger("move",a)}function r(a){var b=[];d(a,function(a){var c=i.get(a.id);c.points.push(a);b.push([c,a]);i.remove(c)});d(b,function(a){a[0].trigger("end",a[1])});i.trigger("end",a)}function f(a){var b=[];d(a,function(a){var c=g.get(a.id);c.points.push(a);
b.push([c,a])});d(b,function(a){a[0].trigger("move",a[1])});g.trigger("move",a)}function h(a,b){m();var c=s(b,function(b){var c=!0;d(a,function(a){a.id==b.id&&(c=!1)});return c});l(c)}function l(a){if(0!=a.length){g=new j;var c=[];d(a,function(a){var b=new n(a.id);b.points.push(a);c.push([b,a]);g.add(b)});var e=b[{1:"one",2:"two",3:"three",4:"four",5:"five"}[g.fingers.length]];e&&e.apply(window,[g].concat(g.fingers));d(c,function(a){a[0].trigger("start",a[1])});g.trigger("start",a)}}function m(){if(g){var a=
[];d(g.fingers,function(b){var c=b.points[b.points.length-1];b.points.push(c);a.push(c);b.trigger("end",b.point)});g.trigger("end",a);g=null}}"function"==typeof b&&(b={any:b});var i=new j,g;o(a,"touchstart",function(a){var b=k(a.touches,a.timeStamp),a=k(a.changedTouches,a.timeStamp);c(a);m();l(b)});o(a,"touchmove",function(a){var b=k(a.touches,a.timeStamp),a=k(a.changedTouches,a.timeStamp);e(a);f(a,b)});o(a,"touchend",function(a){var b=k(a.touches,a.timeStamp),a=k(a.changedTouches,a.timeStamp);r(a);
h(a,b)})}var d=function(){return Array.prototype.forEach?function(a,b,c){Array.prototype.forEach.call(a,b,c)}:function(a,b,c){for(var e=0,d=a.length;e<d;e++)e in a&&b.call(c,a[e],e,a)}}(),q=function(){return Array.prototype.indexOf?function(a,b,c){return Array.prototype.indexOf.call(a,b,c)}:function(a,b,c){for(var c=c||0,e=a.length;c<e;c++)if(c in a&&a[c]===b)return c;return-1}}(),p=function(){return Array.prototype.map?function(a,b,c){return Array.prototype.map.call(a,b,c)}:function(a,b,c){for(var e=
a.length,d=Array(e),f=0;f<e;f++)f in a&&(d[f]=b.call(c,a[f],f,a));return d}}(),s=function(){return Array.prototype.filter?function(a,b,c){return Array.prototype.filter.call(a,b,c)}:function(a,b,c){for(var e=[],d,f=0,h=a.length;f<h;f++)d=a[f],f in a&&b.call(c,d,f,a)&&e.push(d);return e}}(),h={};n.prototype.on=function(a,b){this.callbacks[a].push(b)};n.prototype.trigger=function(a,b){var c=this;d(this.callbacks[a],function(a){a.call(c,b)})};j.prototype.add=function(a){-1==q(this.fingers,a)&&this.fingers.push(a)};
j.prototype.remove=function(a){a=q(this.fingers,a);-1!=a&&this.fingers.splice(a,1)};j.prototype.get=function(a){var b;d(this.fingers,function(c){c.id==a&&(b=c)});return b};j.prototype.on=function(a,b){this.callbacks[a].push(b)};j.prototype.trigger=function(a,b){var c=this;d(this.callbacks[a],function(a){a.call(c,b)})};var m=function(a){a.preventDefault()};l.stopWindowBounce=function(){o(window,"touchmove",m)};l.startWindowBounce=function(){var a=window;if(a.removeEventListener)a.removeEventListener("touchmove",
m,!1);else if(a.detachEvent)for(var b in h)"touchmove"===h[b].name&&h[b].callback===m&&(a.detachEvent(b),delete h[b])};window.Touchy=l})();