UNPKG

swipe-dispatcher

Version:
2 lines (1 loc) 4.36 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;require("custom-event-polyfill");function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}var SwipeDispatcher=/*#__PURE__*/function(){function SwipeDispatcher(){var _ref=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{},_ref$root=_ref.root,root=_ref$root===void 0?document.documentElement:_ref$root,_ref$maxTime=_ref.maxTime,maxTime=_ref$maxTime===void 0?333:_ref$maxTime,_ref$minDistance=_ref.minDistance,minDistance=_ref$minDistance===void 0?100:_ref$minDistance,_ref$variance=_ref.variance,variance=_ref$variance===void 0?100:_ref$variance,_ref$preventMove=_ref.preventMove,preventMove=_ref$preventMove===void 0?true:_ref$preventMove;_classCallCheck(this,SwipeDispatcher);_defineProperty(this,"events",{start:['touchstart','mousedown'],move:['touchmove','mousemove'],end:['touchend','mouseup']});this.root=root;this.maxTime=maxTime;this.minDistance=minDistance;this.variance=variance;this.preventMove=!!preventMove;this.reset();}_createClass(SwipeDispatcher,[{key:"setEventListeners",value:function setEventListeners(){var _this=this;var handlers=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{start:true,move:true,end:true};Object.getOwnPropertyNames(handlers).forEach(function(name){if(_this.events[name]){_this.events[name].forEach(function(event){if(handlers[name]){_this.root.addEventListener(event,_this,false);}else{_this.root.removeEventListener(event,_this,false);}});}});}},{key:"handleStart",value:function handleStart(e){this.recordData('start',e);this.setEventListeners({move:true,end:true});}},{key:"handleMove",value:function handleMove(e){if(this.preventMove){e.preventDefault();}if(performance.now()-this.data.start.time>this.maxTime){this.reset();}}},{key:"handleEnd",value:function handleEnd(e){this.recordData('end',e);this.maybeTriggerSwipe();this.reset();}},{key:"handleEvent",value:function handleEvent(e){switch(e.type){case'mousedown':case'touchstart':this.handleStart(e);break;case'mousemove':case'touchmove':this.handleMove(e);break;case'mouseup':case'touchend':this.handleEnd(e);break;}}},{key:"recordData",value:function recordData(key,e){var target=e.target;var touch=e.changedTouches&&e.changedTouches.length===1?e.changedTouches[0]:false;var time=performance.now();var x=touch?touch.pageX:e.pageX;var y=touch?touch.pageY:e.pageY;this.data[key]={target:target,time:time,x:x,y:y};}},{key:"isSwipe",value:function isSwipe(start,end,xy){var isWithinSwipeTime=end.time-start.time<=this.maxTime;var isWithinVariance=Math.abs(end[xy?'y':'x']-start[xy?'y':'x'])<=this.variance;var hasMinSwipeDistance=Math.abs(end[xy?'x':'y']-start[xy?'x':'y'])>=this.minDistance;return isWithinSwipeTime&&isWithinVariance&&hasMinSwipeDistance;}},{key:"maybeTriggerSwipe",value:function maybeTriggerSwipe(){var _this$data=this.data,start=_this$data.start,end=_this$data.end;if(!start||!end){return;}var isHorizontalSwipe=this.isSwipe(start,end,true);var isVerticalSwipe=this.isSwipe(start,end,false);if(isHorizontalSwipe||isVerticalSwipe){start.target.dispatchEvent(new CustomEvent('swipe',{detail:{left:isHorizontalSwipe&&end.x-start.x<0,right:isHorizontalSwipe&&end.x-start.x>0,up:isVerticalSwipe&&end.y-start.y<0,down:isVerticalSwipe&&end.y-start.y>0},bubbles:true,cancelable:true}));}}},{key:"reset",value:function reset(){this.data={start:null,end:null};this.setEventListeners({start:true,move:false,end:false});}},{key:"stop",value:function stop(){this.setEventListeners({start:false,move:false,end:false});}}]);return SwipeDispatcher;}();exports.default=SwipeDispatcher;