-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (56 loc) · 1.67 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
function create (tag, classname, children) {
var el = document.createElement(tag)
classname && el.classList.add(classname)
children && children.forEach(function (e) {
console.log('append', e)
el.appendChild(
'string' === typeof e ? document.createTextNode(e) : e
)
})
return el
}
module.exports = function (steps) {
var list = create('ul', 'hyperprogress__list')
var error = create('pre', 'hyperprogress__error')
var liquid = create('div', 'hyperprogress__liquid', ['.'])
var bar = create('div', 'hyperprogress__bar', [liquid])
liquid.style.width = '0%'
var n = 0
var prog = create('div', 'hyperprogress', [
steps ? bar : '',
list,
//only show bar if a number of steps is provided.
error
])
prog.complete = function () {
liquid.style.width = '100%'
prog.classList.add('hyperprogress--complete')
}
prog.next = function (name) {
n = Math.min(n+1, steps)
if(list.lastChild)
list.lastChild.classList.add('hyperprogress--okay')
if(name)
list.appendChild(create('li', 'hyperprogress__started', [name]))
liquid.style.width = Math.round((n/steps)*100)+'%'
if(n === steps)
prog.complete()
}
prog.fail = function (err) {
prog.classList.add('hyperprogress--failed')
list.lastChild.classList.add('hyperprogress--error')
if(err && err.stack)
error.textContent = err.stack
else if(err && err.name && err.message)
error.textContent = err.name + ': ' + err.message
else
error.textContent = JSON.stringify(err)
}
prog.reset = function () {
n = 0
error.innerHTML = list.innerHTML = ''
liquid.style.width = '0%'
return prog
}
return prog
}