Files: 84e8acd6b25c6a0ee91b1246f2cc39444d9ccad1 / index.js
1559 bytesRaw
1 | |
2 | function create (tag, classname, children) { |
3 | var el = document.createElement(tag) |
4 | classname && el.classList.add(classname) |
5 | children && children.forEach(function (e) { |
6 | console.log('append', e) |
7 | el.appendChild( |
8 | 'string' === typeof e ? document.createTextNode(e) : e |
9 | ) |
10 | }) |
11 | return el |
12 | } |
13 | |
14 | module.exports = function (steps) { |
15 | var list = create('ul', 'hyperprogress__list') |
16 | var error = create('pre', 'hyperprogress__error') |
17 | var liquid = create('div', 'hyperprogress__liquid', ['.']) |
18 | var bar = create('div', 'hyperprogress__bar', [liquid]) |
19 | liquid.style.width = '0%' |
20 | |
21 | var n = 0 |
22 | |
23 | var prog = create('div', 'hyperprogress', [ |
24 | steps ? bar : '', |
25 | list, |
26 | //only show bar if a number of steps is provided. |
27 | error |
28 | ]) |
29 | |
30 | prog.complete = function () { |
31 | liquid.style.width = '100%' |
32 | prog.classList.add('hyperprogress--complete') |
33 | } |
34 | |
35 | prog.next = function (name) { |
36 | n = Math.min(n+1, steps) |
37 | if(list.lastChild) |
38 | list.lastChild.classList.add('hyperprogress--okay') |
39 | |
40 | if(name) |
41 | list.appendChild(create('li', 'hyperprogress__started', [name])) |
42 | |
43 | liquid.style.width = Math.round((n/steps)*100)+'%' |
44 | |
45 | if(n === steps) |
46 | prog.complete() |
47 | } |
48 | |
49 | prog.fail = function (err) { |
50 | prog.classList.add('hyperprogress--failed') |
51 | list.lastChild.classList.add('hyperprogress--error') |
52 | if(err && err.stack) |
53 | error.textContent = err.stack |
54 | } |
55 | |
56 | prog.reset = function () { |
57 | n = 0 |
58 | error.innerHTML = list.innerHTML = '' |
59 | liquid.style.width = '0%' |
60 | return prog |
61 | } |
62 | |
63 | return prog |
64 | } |
65 | |
66 | |
67 | |
68 |
Built with git-ssb-web