Files: 2ba82fd391fcb12d40a521ed9a2c156d9426de3a / index.js
1405 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 | |
20 | var n = 0 |
21 | |
22 | var prog = create('div', 'hyperprogress', [ |
23 | steps ? bar : '', |
24 | list, |
25 | //only show bar if a number of steps is provided. |
26 | error |
27 | ]) |
28 | |
29 | prog.complete = function () { |
30 | liquid.style.width = '100%' |
31 | progress.classList.add('hyperprogress--complete') |
32 | } |
33 | |
34 | prog.next = function (name) { |
35 | n = Math.min(n+1, steps) |
36 | if(list.lastChild) |
37 | list.lastChild.classList.add('hyperprogress--okay') |
38 | |
39 | if(name) |
40 | list.appendChild(create('li', 'hyperprogress__started', [name])) |
41 | |
42 | liquid.style.width = Math.round((n/steps)*100)+'%' |
43 | |
44 | if(n === steps) |
45 | prog.complete() |
46 | } |
47 | |
48 | prog.fail = function (err) { |
49 | progress.classList.add('hyperprogress--failed') |
50 | list.lastChild.classList.add('hyperprogress--error') |
51 | if(err && err.stack) |
52 | error.textContent = err.stack |
53 | } |
54 | |
55 | return prog |
56 | } |
57 | |
58 |
Built with git-ssb-web