All files swipeJsListener.js

30.77% Statements 12/39
11.54% Branches 3/26
33.33% Functions 2/6
35.29% Lines 12/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78        2x 2x 2x 2x                                                                           2x 2x 2x 2x 2x 2x                                 1x 1x                  
class SwipeJsListener {
 
    constructor() {
  
      this.xDown = null;
      this.yDown = null;
      this.callbackFuncObj = {};
      this.ctx = null;
  
    }
  
    _onTouchStart = (evt) => {
  
      this.xDown = evt.touches[0].clientX;
      this.yDown = evt.touches[0].clientY;
    
    };
    
    _onTouchMove = (evt) => {
    
      if (!this.xDown || !this.yDown) return;
    
      const xDiff = this.xDown - evt.touches[0].clientX;
      const yDiff = this.yDown - evt.touches[0].clientY;
 
      if (Math.abs(xDiff) > Math.abs(yDiff)) {
    
        if (xDiff > 0 && Object.prototype.hasOwnProperty.call(this.callbackFuncObj, 'callbackToSwipeLeft')) this.callbackFuncObj.callbackToSwipeLeft.call(this.ctx);
        else if (Object.prototype.hasOwnProperty.call(this.callbackFuncObj, 'callbackToSwipeRight')) this.callbackFuncObj.callbackToSwipeRight.call(this.ctx);
      
      } else {
    
        if (yDiff > 0 && Object.prototype.hasOwnProperty.call(this.callbackFuncObj, 'callbackToSwipeUp')) this.callbackFuncObj.callbackToSwipeUp.call(this.ctx);
        else if (Object.prototype.hasOwnProperty.call(this.callbackFuncObj, 'callbackToSwipeDown')) this.callbackFuncObj.callbackToSwipeDown.call(this.ctx);
    
      }
      
      this.xDown = null;
      this.yDown = null;
    
    };
    
    
    addSwipeListener = (ctx, callbackFuncObj) => {
  
      this.xDown = null;
      this.yDown = null;
      this.callbackFuncObj = callbackFuncObj;
      this.ctx = ctx;
      window.addEventListener('touchstart', this._onTouchStart);
      window.addEventListener('touchmove', this._onTouchMove);
    
    };
    
    removeSwipeListener = () => {
    
      this.xDown = null;
      this.yDown = null;
      this.callbackFuncObj = {};
      this.ctx = null;
      window.removeEventListener('touchstart', this._onTouchStart);
      window.removeEventListener('touchmove', this._onTouchMove);
    
    };
  
  }
  
  Eif (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
    module.exports = SwipeJsListener;
  } else {
    if (typeof define === 'function' && define.amd) {
      define([], function() {
        return SwipeJsListener;
      });
    } else {
      window.SwipeJsListener = SwipeJsListener;
    }
  }