all files / src/utils/ drag.js

1.49% Statements 1/67
7.41% Branches 2/27
0% Functions 0/19
1.52% Lines 1/66
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 1501×                                                                                                                                                                                                                                                                                                          
const IS_TOUCH = window !== undefined && (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch)
export default class Drag {
  constructor (element) {
    this.el = element
    this.startPos = {}
    this.endPos = {}
    this.starts = []
    this.drags = []
    this.ends = []
    if (IS_TOUCH) {
      this.el.addEventListener('touchstart', this, false)
    } else {
      this.el.addEventListener('mousedown', this, false)
    }
  }
  handleEvent (event) {
    switch (event.type) {
      case 'touchstart':
        this.touchStart(event)
        break
      case 'touchmove':
        this.touchMove(event)
        break
      case 'touchcancel':
      case 'touchend':
        this.touchEnd(event)
        break
      case 'mousedown':
        this.mouseStart(event)
        break
      case 'mousemove':
        this.mouseMove(event)
        break
      case 'mouseleave':
      case 'mouseup':
        this.mouseEnd(event)
        break
    }
  }
 
  touchStart (event) {
    let touch = event.touches[0]
    this.startPos = { // 取第一个touch的坐标值
      x: touch.pageX,
      y: touch.pageY,
      time: new Date().getTime()
    }
    this.endPos = {}
    this.el.addEventListener('touchmove', this, false)
    this.el.addEventListener('touchend', this, false)
    this.starts.map((func) => {
      func.call(this, this.startPos, event)
    })
  }
 
  touchMove (event) {
    if (event.touches.length > 1 || event.scale && event.scale !== 1) return
    let touch = event.touches[0]
    this.endPos = {
      x: touch.pageX - this.startPos.x,
      y: touch.pageY - this.startPos.y,
      time: new Date().getTime() - this.startPos.time
    }
    this.drags.map((func) => {
      func.call(this, this.endPos, event)
    })
  }
 
  touchEnd (event) {
    this.endPos.time = new Date().getTime() - this.startPos.time
 
    this.el.removeEventListener('touchmove', this, false)
    this.el.removeEventListener('touchend', this, false)
    this.ends.map((func) => {
      func.call(this, this.endPos, event)
    })
  }
 
  mouseStart (event) {
    this.startPos = {
      x: event.clientX,
      y: event.clientY,
      time: new Date().getTime()
    }
    this.endPos = {}
    this.el.addEventListener('mousemove', this, false)
    this.el.addEventListener('mouseup', this, false)
    this.starts.map((func) => {
      func.call(this, this.startPos, event)
    })
  }
 
  mouseMove (event) {
    this.endPos = {
      x: event.clientX - this.startPos.x,
      y: event.clientY - this.startPos.y
    }
 
    this.drags.map((func) => {
      func.call(this, this.endPos, event)
    })
  }
 
  mouseEnd (event) {
    this.el.removeEventListener('mousemove', this, false)
    this.el.removeEventListener('mouseup', this, false)
 
    this.endPos.time = new Date().getTime() - this.startPos.time
 
    this.ends.map((func) => {
      func.call(this, this.endPos, event)
    })
  }
 
  start (fun) {
    this.starts.push(fun)
    return this
  }
 
  end (fun) {
    this.ends.push(fun)
    return this
  }
 
  drag (fun) {
    this.drags.push(fun)
    return this
  }
 
  reset (event) {
    let touch = event.touches ? event.touches[0] : {}
    this.startPos = { // 取第一个touch的坐标值
      x: touch.pageX || event.clientX,
      y: touch.pageY || event.clientY,
      time: new Date().getTime()
    }
    this.endPos = {
      x: 0,
      y: 0
    }
  }
  destory () {
    if (IS_TOUCH) {
      this.el.removeEventListener('touchstart', this, false)
    } else {
      this.el.removeEventListener('mousedown', this, false)
    }
  }
}