forked from Jack-Lo/ez-progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
204 lines (159 loc) · 3.53 KB
/
index.js
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
var Event = require('on-fire')
var win = window
var doc = document
var sTO = win.setTimeout
var sIV = win.setInterval
var cTO = win.clearTimeout
var cIV = win.clearInterval
function random (n) {
if (typeof n === 'object') {
var times = n[1] - n[0]
var offset = n[0]
return Math.random() * times + offset
} else {
return n
}
}
function main (opt) {
var onFire = new Event()
var _t = this
_t.on = onFire.on
_t.fire = onFire.fire
_t.handler = onFire.handler
var cfg = _t.config = {
from: opt.from ? opt.from : 0,
to: opt.to ? opt.to : 100,
speed: opt.speed ? opt.speed : 1,
delay: opt.delay ? opt.delay : (1000 / 11)
}
var data = _t.data = {
progress: cfg.from,
next: {
dist: cfg.from,
callback: null,
speed: cfg.speed,
delay: cfg.delay,
status: 1
},
status: 0
}
_t.timer = 0
}
main.prototype = {
from (prg) {
var _t = this
var _prg = random(prg)
_t.config.from = _prg
},
progress (prg, cb, spd, dly) {
var _t = this
var cfg = _t.config
var data = _t.data
var _spd = random(spd)
var _dly = random(dly)
var from = data.progress
_t.timer = sTO(() => {
if (from + _spd >= prg) {
data.progress = prg
_t.fire('progress', data)
cTO(_t.timer)
data.next.status = 1
if (prg === cfg.to) {
_t.fire('completed', data)
}
cb && cb(data)
} else {
data.progress += _spd
_t.fire('progress', data)
_t.progress(prg, cb, spd, dly)
}
}, _dly)
},
add (prg, cb, spd, dly) {
var _t = this
var cfg = _t.config
var data = _t.data
var next = data.next
var _prg = random(prg)
if (_t.data.status === 1) { // ended
return _t
}
cTO(_t.timer)
if (next.status === 0) {
_t.fire('passed', data)
next.status = 1
}
if (next.dist + _prg > cfg.to) { // 对超出部分裁剪对齐
next.dist = cfg.to
} else {
next.dist += _prg
}
next.callback = cb
next.speed = spd ? spd : cfg.speed
next.delay = dly ? dly : cfg.delay
next.status = 0
_t.progress(next.dist, next.callback, next.speed, next.delay)
if (_t.data.status === 2) { // complete
_t.data.status = 1 // 完成后,锁住add方法
}
return _t
},
go (prg, cb, spd, dly) {
var _t = this
var data = _t.data
var _prg = random(prg)
_t.add(_prg - data.progress, cb, spd, dly)
return _t
},
end (cb) {
var _t = this
_t.data.status = 1
cTO(_t.timer)
_t.fire('ended', _t.data)
cb && cb(_t.data)
return _t
},
complete (cb, spd, dly) {
var _t = this
_t.data.status = 2
cTO(_t.timer)
_t.go(_t.config.to, cb, spd, dly)
return _t
},
reset (cb) {
var _t = this
var cfg = _t.config
cTO(_t.timer)
_t.handler = {}
_t.data = {
progress: cfg.from,
next: {
dist: cfg.from,
callback: null,
speed: cfg.speed,
delay: cfg.delay,
status: 1
},
status: 0
}
_t.timer = 0
_t.fire('reset', _t.data)
// _t.fire('progress', _t.data) // 触发一次progress?
cb && cb(_t.data)
return _t
},
destroy (cb) {
var _t = this
cTO(_t.timer)
cb && cb(_t.data)
_t.fire('destroy', _t.data) // 这里实际上是先回调,再destroy...
_t.config = null
_t.data = null
_t.timer = 0
_t.on = null
_t.fire = null
_t.handler = null
return _t
}
}
module.exports = main