-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathToProgress.js
145 lines (127 loc) · 3.81 KB
/
ToProgress.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
/*
** ToProgress v0.1.1
** http://github.com/djyde/ToProgress
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory)
} else if (typeof exports === 'object') {
module.exports = factory()
} else {
root.ToProgress = factory()
}
})(this, function() {
// Animation Detection
function whichTransitionEvent(){
var t,
el = document.createElement("fakeelement");
var transitions = {
"transition" : "transitionend",
"OTransition" : "oTransitionEnd",
"MozTransition" : "transitionend",
"WebkitTransition": "webkitTransitionEnd"
}
for (t in transitions){
if (el.style[t] !== undefined){
return transitions[t];
}
}
}
var transitionEvent = whichTransitionEvent();
function ToProgress(opt, selector) {
// Attributes
this.progress = 0;
this.options = {
id: 'top-progress-bar',
color: '#F44336',
height: '2px',
duration: 0.2
};
if (opt && typeof opt === 'object') {
for (var key in opt) {
this.options[key] = opt[key];
}
}
this.options.opacityDuration = this.options.duration * 3;
this.progressBar = document.createElement('div');
// Initialization
this.progressBar.id = this.options.id;
this.progressBar.setCSS = function(style) {
for(var property in style){
this.style[property] = style[property];
}
}
this.progressBar.setCSS({
"position": selector ? "relative" : "fixed",
"top": "0",
"left": "0",
"right": "0",
"background-color": this.options.color,
"height": this.options.height,
"width": "0%",
"transition": "width " + this.options.duration + "s" + ", opacity " + this.options.opacityDuration + "s",
"-moz-transition": "width " + this.options.duration + "s" + ", opacity " + this.options.opacityDuration + "s",
"-webkit-transition": "width " + this.options.duration + "s" + ", opacity " + this.options.opacityDuration + "s"
});
// Create the Progress Bar
if (selector) {
var el = document.querySelector(selector);
if (el) {
if (el.hasChildNodes()) {
el.insertBefore(this.progressBar, el.firstChild);
} else {
el.appendChild(this.progressBar);
}
}
} else {
document.body.appendChild(this.progressBar);
}
}
ToProgress.prototype.transit = function() {
this.progressBar.style.width = this.progress + '%';
}
ToProgress.prototype.getProgress = function() {
return this.progress;
}
ToProgress.prototype.setProgress = function(progress, callback) {
this.show();
if (progress > 100) {
this.progress = 100;
} else if (progress < 0) {
this.progress = 0;
} else {
this.progress = progress;
}
this.transit();
callback && callback();
}
ToProgress.prototype.increase = function(toBeIncreasedProgress, callback) {
this.show();
this.setProgress(this.progress + toBeIncreasedProgress, callback);
}
ToProgress.prototype.decrease = function(toBeDecreasedProgress, callback) {
this.show();
this.setProgress(this.progress - toBeDecreasedProgress, callback);
}
ToProgress.prototype.finish = function(callback) {
var that = this;
this.setProgress(100, callback);
this.hide();
transitionEvent && this.progressBar.addEventListener(transitionEvent, function(e) {
that.reset();
that.progressBar.removeEventListener(e.type, arguments.callee);
});
}
ToProgress.prototype.reset = function(callback) {
this.progress = 0;
this.transit();
callback && callback();
}
ToProgress.prototype.hide = function() {
this.progressBar.style.opacity = '0';
}
ToProgress.prototype.show = function() {
this.progressBar.style.opacity = '1';
}
return ToProgress
})