UNPKG

detect_guester

Version:
183 lines (182 loc) 5.57 kB
module.exports.detect = (function() { var k = (positionY = newPositionX = newPositionY = 0), a = null, l = function(b) { b.off('pointerup'); b.off('pointermove'); }, e = function(b) { b.off('mouseup'); b.off('mousemove'); b.off('touchstart'); b.off('touchmove'); k = positionY = newPositionX = newPositionY = 0; a = null; }, f = function(b) { a || (a = b); if (a) if (a.hasOwnProperty('callback')) if ('function' === typeof a.callback) try { a.callback(); } catch (c) { throw Error(c.message); } else throw Error('param callback must be function'); else throw Error('not set callback function'); }; return { swipe: function(b) { a = b; if (b.hasOwnProperty('target')) if ('string' === typeof b.target) { var c = $(b.target); c.on('mousedown touchstart', function(a) { a.preventDefault(); k = a.clientX; positionY = a.clientY; c.on('mouseup touchend', function(b) { b.preventDefault(); e(c); }); c.on('mousemove touchmove', function(d) { d.preventDefault(); newPositionX = k - d.clientX; newPositionY = positionY - d.clientY; d = Math.abs(newPositionX); var g = Math.abs(newPositionY); if (b.hasOwnProperty('direction')) if ('string' === typeof b.direction) switch (b.direction) { case 'left': 0 < newPositionX && 100 <= d && (f(b), e(c)); break; case 'right': 0 > newPositionX && 100 <= d && (f(b), e(c)); break; case 'top': 0 < newPositionY && 100 <= g && (f(b), e(c)); break; case 'down': 0 > newPositionY && 100 <= g && (f(b), e(c)); } else throw Error('params direction must be string'); else throw Error('must be set direction params'); }); }); } else throw Error('param target element must be string'); else throw Error('no set target element'); }, press: function(b) { a = b; if (b.hasOwnProperty('target')) if ('string' === typeof b.target) { var c = $(b.target); c.on('mousedown touchstart', function(a) { a.preventDefault(); f(b); c.on('mouseup touchend', function(b) { b.preventDefault(); e(c); }); }); } else throw Error('param target must be string'); else throw Error('no set target element'); }, longPress: function(b) { a = b; if (b.hasOwnProperty('target')) if ('string' === typeof b.target) { var c = $(b.target), m = 1500; if (b.hasOwnProperty('wait_time')) if ('integer' === typeof b.wait_time) m = b.wait_time; else throw Error('param wait time must be integer'); c.on('mousedown touchstart', function(a) { a.preventDefault(); setTimeout(function() { f(b); }, m); c.on('mouseup touchend', function(b) { b.preventDefault(); e(c); }); }); } else throw Error('param target must be string'); else throw Error('no set target element'); }, rotate: function(b) { a = b; if (a.hasOwnProperty('target')) if ('string' === typeof a.target) { var c = $(a.target); c.on('mousedown touchstart', function(a) { a.preventDefault(); k = a.clientX; positionY = a.clientY; if (a.touches instanceof Array && 2 < a.touches.length) { var d = a.touches[0].clientY; k = a.touches[0].clientX; positionY = d; } c.on('mouseup touchend', function(b) { b.preventDefault(); e(c); }); c.on('mousemove touchmove', function(a) { a.preventDefault(); newPositionX = a.clientX; newPositionY = a.clientY; if (a.touches instanceof Array && 2 < a.touches.length) { var d = a.touches[0].clientY; newPositionX = a.touches[0].clientX; newPositionY = d; } a = 180 * Math.atan2(newPositionY - positionY, newPositionX - k) / Math.PI; 20 >= a && -20 <= a ? (f(b), e(c)) : 110 >= a && 70 <= a ? (f(b), e(c)) : 90 < a && 180 > a && (f(b), e(c)); }); }); } else throw Error('param target must be string'); else throw Error('no set target element'); }, zoom: function(b) { a = b; if (a.hasOwnProperty('target')) if ('string' === typeof a.target) { var c = $(a.target); if (a.hasOwnProperty('direction')) if ('string' === typeof a.direction) { var e = a.direction, d = [], g = -1; c.on('pointerdown', function(b) { d.push(b); 2 === d.length && (g = Math.abs(d[0].clientX - d[1].clientX)); }); c.on('pointermove', function(a) { for (var h = 0; h < d.length; h++) d[h].pointerId === a.pointerId && (d[h] = a); 2 === d.length && ((a = Math.abs(d[0].clientX - d[1].clientX)), (h = a - g), 0 < h && 'in' === e ? (f(b), l(c)) : 0 > h && 'out' === e && (f(b), l(c)), (g = a)); }); c.on('pointerup', function(a) { for (var b = 0; b < d.length; b++) if (d[b].pointerId == a.pointerId) { d.splice(b, 1); break; } 2 > d.length && (g = -1); 0 == d.length && l(c); }); } else throw Error('param direction must be string'); else throw Error('param direction must be set'); } else throw Error('param target element must be string'); else throw Error('no set target element'); } }; })();