pull-to-refresh.js 6.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
function processDeltaY (evt, identifier, startY) {
  const touch = Array.prototype.slice.call(evt.changedTouches).filter(touch => touch.identifier === identifier)[0]
  if (!touch) {
    return false
  }
  evt.deltaY = touch.pageY - startY
  return true
}

// const ratio = 2.2

const PULLING = 'pulling'
const REACHED = 'reached'

const ABORTING = 'aborting'
const REFRESHING = 'refreshing'
const RESTORING = 'restoring'

export default {
  mounted () {
    if (this.enablePullDownRefresh) {
      this.refreshContainerElem = this.$refs.refresh.$el
      this.refreshControllerElem = this.refreshContainerElem.querySelector('.uni-page-refresh')
      this.refreshInnerElemStyle = this.refreshControllerElem.querySelector('.uni-page-refresh-inner').style

      UniServiceJSBridge.on(this.$route.params.__id__ + '.startPullDownRefresh', () => {
        if (!this.state) {
          this.state = REFRESHING
          this._addClass()
          setTimeout(() => {
            this._refreshing()
          }, 50)
        }
      })

      UniServiceJSBridge.on(this.$route.params.__id__ + '.stopPullDownRefresh', () => {
        if (this.state === REFRESHING) {
          this._removeClass()
          this.state = RESTORING
          this._addClass()

          this._restoring(() => {
            this._removeClass()
            this.state = this.distance = this.offset = null
          })
        }
      })
    }
  },
  methods: {
    _touchstart (evt) {
      const touch = evt.changedTouches[0]
      this.touchId = touch.identifier
      this.startY = touch.pageY
      if ([ABORTING, REFRESHING, RESTORING].indexOf(this.state) >= 0) {
        this.canRefresh = false
      } else {
        this.canRefresh = true
      }
    },
    _touchmove (evt) {
      if (!this.canRefresh) {
        return
      }
      if (!processDeltaY(evt, this.touchId, this.startY)) {
        return
      }

      let {
        deltaY
      } = evt

      if ((document.documentElement.scrollTop || document.body.scrollTop) !== 0) {
        this.touchId = null
        return
      }

      if (deltaY < 0 && !this.state) {
        return
      }

      evt.preventDefault()

      if (this.distance == null) {
        this.offset = deltaY
        this.state = PULLING
        this._addClass()
      }

      deltaY = deltaY - this.offset

      if (deltaY < 0) {
        deltaY = 0
      }

      this.distance = deltaY

      const reached = deltaY >= this.refreshOptions.range && this.state !== REACHED
      const pulling = deltaY < this.refreshOptions.range && this.state !== PULLING

      if (reached || pulling) {
        this._removeClass()
        this.state = this.state === REACHED ? PULLING : REACHED
        this._addClass()
      }

      this._pulling(deltaY)
    },
    _touchend (evt) {
      if (!processDeltaY(evt, this.touchId, this.startY)) {
        return
      }
      if (this.state === null) {
        return
      }
      if (this.state === PULLING) {
        this._removeClass()
        this.state = ABORTING
        this._addClass()
        this._aborting(() => {
          this._removeClass()
          this.state = this.distance = this.offset = null
        })
      } else if (this.state === REACHED) {
        this._removeClass()
        this.state = REFRESHING
        this._addClass()
        this._refreshing()
      }
    },
    _toggleClass (type) {
      if (!this.state) {
        return
      }
      const elem = this.refreshContainerElem
      if (elem) {
        elem.classList[type]('uni-page-refresh--' + this.state)
      }
    },
    _addClass () {
      this._toggleClass('add')
    },
    _removeClass () {
      this._toggleClass('remove')
    },
    _pulling (deltaY) {
      const elem = this.refreshControllerElem
      if (!elem) {
        return
      }

      const style = elem.style

      let rotate = deltaY / this.refreshOptions.range

      if (rotate > 1) {
        rotate = 1
      } else {
        rotate = rotate * rotate * rotate
      }

      const y = Math.round(deltaY / (this.refreshOptions.range / this.refreshOptions.height))

      const transform = y ? 'translate3d(-50%, ' + y + 'px, 0)' : 0

      style.webkitTransform = transform
      style.clip = 'rect(' + (45 - y) + 'px,45px,45px,-5px)'

      this.refreshInnerElemStyle.webkitTransform = 'rotate(' + 360 * rotate + 'deg)'
    },
    _aborting (callback) {
      const elem = this.refreshControllerElem
      if (!elem) {
        return
      }

      const style = elem.style

      if (style.webkitTransform) {
        style.webkitTransition = '-webkit-transform 0.3s'
        style.webkitTransform = 'translate3d(-50%, 0, 0)'
        const abortTransitionEnd = function () {
          timeout && clearTimeout(timeout)
          elem.removeEventListener('webkitTransitionEnd', abortTransitionEnd)
          style.webkitTransition = ''
          callback()
        }
        elem.addEventListener('webkitTransitionEnd', abortTransitionEnd)
        const timeout = setTimeout(abortTransitionEnd, 350) // 部分手机,部分情况webkitTransitionEnd不触发
      } else {
        callback()
      }
    },
    _refreshing () {
      const elem = this.refreshControllerElem
      if (!elem) {
        return
      }

      const style = elem.style
      style.webkitTransition = '-webkit-transform 0.2s'
      style.webkitTransform = 'translate3d(-50%, ' + this.refreshOptions.height + 'px, 0)'

      // Service 执行 refresh
      UniServiceJSBridge.emit('onPullDownRefresh', {}, this.$route.params.__id__)
    },
    _restoring (callback) {
      const elem = this.refreshControllerElem
      if (!elem) {
        return
      }

      const style = elem.style

      style.webkitTransition = '-webkit-transform 0.3s'
      style.webkitTransform += ' scale(0.01)'

      const restoreTransitionEnd = function () {
        timeout && clearTimeout(timeout)
        elem.removeEventListener('webkitTransitionEnd', restoreTransitionEnd)
        style.webkitTransition = ''
        style.webkitTransform = 'translate3d(-50%, 0, 0)'
        callback()
      }

      elem.addEventListener('webkitTransitionEnd', restoreTransitionEnd)
      const timeout = setTimeout(restoreTransitionEnd, 350) // 部分手机,部分情况webkitTransitionEnd不触发
    }
  }
}