mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
import {
  PRIMARY_COLOR
} from '../constants'
 
import {
  emitter
} from '../mixins'
 
const BACKGROUND_COLOR = '#EBEBEB'
const ANIMATE_INTERVAL = 30
const FONT_SIZE = 16
const STROKE_WIDTH = 6
 
function getProgress (weex) {
  return {
    name: 'Progress',
    mixins: [emitter],
    props: {
      percent: {
        type: [String, Number],
        default: 0
      },
      showInfo: {
        type: Boolean,
        default: false
      },
      borderRadius: {
        type: [String, Number],
        default: 0
      },
      fontSize: {
        type: [String, Number],
        default: FONT_SIZE
      },
      strokeWidth: {
        type: [String, Number],
        default: STROKE_WIDTH
      },
      active: {
        type: Boolean,
        default: false
      },
      activeColor: {
        type: String,
        default: PRIMARY_COLOR
      },
      activeMode: {
        type: String,
        default: 'backwards'
      },
      backgroundColor: {
        type: String,
        default: BACKGROUND_COLOR
      }
    },
    data () {
      return {
        width: 0,
        curPercent: 0
      }
    },
    computed: {
      barStyle () {
        return {
          height: this.strokeWidth,
          borderRadius: this.borderRadius,
          backgroundColor: this.backgroundColor
        }
      },
      innerBarStyle () {
        return {
          width: this.activeWidth,
          height: this.strokeWidth,
          backgroundColor: this.activeColor
        }
      },
      activeWidth () {
        return this.curPercent * this.width / 100
      },
      finalPercent () {
        let percent = parseFloat(this.percent, 10)
        percent > 100 && (percent = 100)
        percent < 0 && (percent = 0)
        return percent
      }
    },
    watch: {
      finalPercent (newVal, oldVal) {
        this._timerId && clearInterval(this._timerId)
        this._lastPercent = oldVal || 0
        this._animate()
      }
    },
    mounted () {
      setTimeout(() => {
        const dom = weex.requireModule('dom')
        dom.getComponentRect(this.$refs.progress, res => {
          this.width = res.size.width
          this._animate()
        })
      }, 50)
    },
    methods: {
      _animate () {
        const percent = this.finalPercent
        if (!this.active) {
          return (this.curPercent = percent)
        }
        this.curPercent = this.activeMode === 'forwards' ? this._lastPercent : 0
        this._timerId = setInterval(() => {
          if (percent <= this.curPercent + 1) {
            this.curPercent = percent
            clearInterval(this._timerId)
            this.$trigger('activeend', {
              curPercent: this.curPercent
            })
          }
          else {
            ++this.curPercent
          }
        }, ANIMATE_INTERVAL)
      }
    },
    render (createElement) {
      const _vm = this
      return createElement('div',
        _vm._g({
          ref: 'progress',
          staticClass: ['uni-progress']
        }, _vm.$listeners),
        [
          createElement('div', {
            staticClass: ['uni-progress-bar'],
            style: _vm.barStyle
          }, [
            createElement('div', {
              staticClass: ['uni-progress-inner-bar'],
              style: _vm.innerBarStyle
            })
          ]), _vm.showInfo ? [
            createElement('u-text', {
              staticClass: ['uni-progress-info'],
              style: {
                fontSize: _vm.fontSize
              }
            }, [_vm.curPercent + '%'])
          ] : _vm._e()
        ], 2)
    },
    style: {
      'uni-progress': {
        flexDirection: 'row',
        alignItems: 'center'
      },
      'uni-progress-bar': {
        flex: 1
      },
      'uni-progress-inner-bar': {
        position: 'absolute'
      },
      'uni-progress-info': {
        marginLeft: '15px'
      }
    }
  }
}
 
export default function init (Vue, weex) {
  Vue.component('progress', getProgress(weex))
}