-
Notifications
You must be signed in to change notification settings - Fork 8
/
progress.js
90 lines (79 loc) · 3.01 KB
/
progress.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
var Progress = {};
Progress.bar = (function(config) {
if ("undefined" === typeof config) {
console.log('Please give config object, like:\nnew Progress.bar({ id: "progress1", autoRemove: true, removeTimeout: 2000 , backgroundSpeed: 50, type: "discharge", showPercentage: true });');
return;
}
// defaults
config.type = config.type ? config.type : 'charge';
config.id = config.id ? config.id : 'progress' + Math.floor(Math.random()*9999);
var outerDiv = createOuterDiv();
var percentageSpan = createPercentageSpan();
var innerDiv = createInnerDiv();
var intervals = [];
function createOuterDiv() {
var outerDiv = document.createElement('div');
outerDiv.setAttribute('class', 'outerDiv');
outerDiv.setAttribute('id', config.id);
return outerDiv;
}
function createPercentageSpan() {
var percentageSpan = document.createElement('span');
percentageSpan.innerHTML = config.type === 'charge' ? '0%' : '100%';
return percentageSpan;
}
function createInnerDiv() {
var innerDiv = document.createElement('div');
innerDiv.setAttribute('class', 'innerDiv');
innerDiv.style.width = config.type === 'charge' ? '0' : '100%';
return innerDiv;
}
function update(percent) {
percent = percent > 100 ? 100 : percent < 0 ? 0 : percent;
innerDiv.style.width = percent + '%';
if (config.showPercentage) {
percentageSpan.innerHTML = percent + '%';
}
checkForAutoRemoval(percent);
}
function renderTo(element) {
if (config.showPercentage) {
outerDiv.appendChild(percentageSpan);
}
outerDiv.appendChild(innerDiv);
element.appendChild(outerDiv);
animateBackground();
}
function animateBackground() {
if (!config.backgroundSpeed) {
return;
}
var position = 0;
intervals['backgroundAnimation'] = window.setInterval(function() {
if (config.backgroundSpeed < 0) {
innerDiv.style.backgroundPosition = ++position + 'px';
} else {
innerDiv.style.backgroundPosition = --position + 'px';
}
}, config.backgroundSpeed);
}
function checkForAutoRemoval(percent) {
if ('discharge' === config.type && (0 !== percent || true !== config.autoRemove)) {
return;
}
if ('charge' === config.type && (100 !== percent || true !== config.autoRemove)) {
return;
}
!config.removeTimeout ? remove() : window.setTimeout(remove, config.removeTimeout);
}
function remove(callback) {
window.clearInterval(intervals['backgroundAnimation']);
var renderedProgressBar = document.getElementById(config.id);
renderedProgressBar.parentNode.removeChild(renderedProgressBar);
}
return {
update: update,
renderTo: renderTo,
remove: remove
};
});