all files / src/refreshControl/ refreshControl.vue

2.3% Statements 2/87
0% Branches 0/54
0% Functions 0/3
2.56% Lines 2/78
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                            1× 1×                                                                                                                                                                                                                                                                                                                                                                                                                
<template>
  <div class="mu-refresh-control" :style="refreshStyle" :class="refreshClass">
    <svg v-show="!refreshing && draging" viewBox="0 0 24 24" class="mu-refresh-svg-icon" :style="circularStyle">
      <path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"/>
    </svg>
    <circular v-show="refreshing" :size="24" :border-width="2"></circular>
  </div>
</template>
 
<script>
import Drag from '../utils/drag'
import circular from '../internal/circular'
import * as domUtil from '../utils/domUtil'
 
const LENGTH = 130 // 下拉最大长度
const INITY = -68  // 初始化Y轴位置
export default {
  name: 'mu-refresh-control',
  props: {
    refreshing: {
      type: Boolean,
      default: false
    },
    trigger: {
      type: Element
    }
  },
  data () {
    return {
      y: 0,
      draging: false,
      state: 'pending'
    }
  },
  computed: {
    refreshStyle () {
      let style = {}
      if (!this.refreshing && this.draging) {
        let translate3d = 'translate3d(0, ' + (this.y + INITY) + 'px, 0) '
        style['-webkit-transform'] = style['transform'] = translate3d
      }
      return style
    },
    circularStyle () {
      let style = {}
      if (!this.refreshing && this.draging) {
        let percentage = this.y / LENGTH
        let rotate = 'rotate(' + (360 * percentage) + 'deg)'
        let opacity = this.y / Math.abs(INITY)
        style['-webkit-transform'] = style['transform'] = rotate
        style['opacity'] = opacity
      }
      return style
    },
    refreshClass () {
      let classNames = []
      switch (this.state) {
        case 'pending':
          break
        case 'ready':
          classNames.push('mu-refresh-control-noshow')
          break
        case 'dragStart':
          classNames.push('mu-refresh-control-hide')
          break
        case 'dragAnimate':
          classNames.push('mu-refresh-control-animate')
          classNames.push('mu-refresh-control-hide')
          break
        case 'refreshAnimate':
          classNames.push('mu-refresh-control-animate')
          classNames.push('mu-refresh-control-noshow')
          break
      }
      if (this.refreshing) classNames.push('mu-refresh-control-refreshing')
      return classNames
    }
  },
  mounted () {
    this.bindDrag()
  },
  beforeDestory () {
    this.unbindDrag()
  },
  methods: {
    clearState () {
      this.state = 'ready'
      this.draging = false
      this.y = 0
    },
    bindDrag () {
      if (!this.trigger) return
      const drager = this.drager = new Drag(this.trigger)
      const initTop = domUtil.getOffset(this.$el).top + INITY  // 初始化位置
      this.state = 'ready'
      drager.start(() => {
        if (this.refreshing) return
        this.state = 'dragStart'
        let top = domUtil.getOffset(this.$el).top
        if (top === initTop) this.draging = true
      }).drag((pos, event) => {
        if (pos.y < 5) return // 消除误差
        let top = domUtil.getOffset(this.$el).top
        if (this.refreshing || !initTop || top < initTop) {
          this.draging = false
          return
        }
 
        if (top === initTop && !this.draging) {
          this.draging = true
          drager.reset(event)
        }
 
        if (this.draging && pos.y > 0) {
          event.preventDefault()
          event.stopPropagation()
        }
 
        this.y = pos.y
        if (this.y < 0) this.y = 1
        if (this.y > LENGTH) this.y = LENGTH
      }).end((pos, event) => {
        if (!pos.y || pos.y < 5) {
          this.clearState()
          return // 消除误差
        }
        let canRefresh = pos.y + INITY > 0 && this.draging
        this.state = 'dragAnimate'
        if (canRefresh) {
          this.draging = false
          this.$emit('refresh')
        } else {
          this.y = 0
          domUtil.transitionEnd(this.$el, this.clearState.bind(this))
        }
      })
    },
    unbindDrag () {
      if (!this.drager) return
      this.drager.destory()
      this.drager = null
    }
  },
  watch: {
    refreshing (val) {
      if (!val) {
        domUtil.transitionEnd(this.$el, this.clearState.bind(this))
      } else {
        this.state = 'refreshAnimate'
      }
    },
    trigger (trigger, oldTrigger) {
      if (trigger === oldTrigger) return
      this.unbindDrag()
      this.bindDrag()
    }
  },
  components: {
    circular
  }
}
</script>
 
<style lang="less">
@import "../styles/import.less";
.mu-refresh-control{
  display: flex;
  margin: 0 auto;
  width: 40px;
  height: 40px;
  color: @primaryColor;
  align-items: center;
  justify-content: center;
  background-color: #FFF;
  border-radius: 50%;
  .depth(1);
  position: absolute;
  left: 50%;
  margin-left: -18px;
  margin-top: 24px;
  z-index: 90;
  .mu-icon {
    display: inline-block;
    vertical-align: middle;
  }
}
 
.mu-refresh-svg-icon {
  display: inline-block;
  width: 28px;
  height: 28px;
  fill: currentColor;
  user-select: none;
}
 
.mu-refresh-control-animate{
   transition: all 0.45s ease;
}
 
.mu-refresh-control-hide{
  opacity: 1;
  transform: translate3d(0, -68px, 0);
}
 
.mu-refresh-control-noshow{
  opacity: 0;
  transform: scale(0.01);
}
 
.mu-refresh-control-refreshing {
  transform: scale(1);
  opacity: 1;
}
 
</style>