forked from voronianski/ngprogress-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngprogress-lite.js
158 lines (130 loc) · 4.72 KB
/
ngprogress-lite.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
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* ngProgressLite - small && slim angular progressbars
* http://github.com/voronianski/ngprogress-lite
* Dmitri Voronianski http://pixelhunter.me
* (c) 2013 MIT License
*/
(function(window, angular, _) {
'use strict';
angular.module('ngProgressLite', []).provider('ngProgressLite', function() {
this.$get = ['$document',
function($document) {
return function(_container, _settings) {
var settings = {
minimum: _.random(0.01, 0.09),
speed: _.random(100, 300),
ease: 'ease',
trickleRate: _.random(0.03, 0.12),
trickleSpeed: _.random(100, 400),
// Temporarily disable shadow as we can no longer assume that the area above the progress bar will be clipped
// template: '<div class="ngProgressLite"><div class="ngProgressLiteBar"><div class="ngProgressLiteBarShadow"></div></div></div>'
template: '<div class="ngProgressLite"><div class="ngProgressLiteBar"></div></div>'
};
var container = _container || $document.find('body');
var $progressBarEl, status, cleanForElement;
var privateMethods = {
render: function() {
if (this.isRendered()) {
return $progressBarEl;
}
container.addClass('ngProgressLite-on');
$progressBarEl = angular.element(settings.template);
container.append($progressBarEl);
cleanForElement = false;
return $progressBarEl;
},
remove: function() {
container.removeClass('ngProgressLite-on');
$progressBarEl.remove();
cleanForElement = true;
},
isRendered: function() {
return $progressBarEl && $progressBarEl.children().length > 0 && !cleanForElement;
},
trickle: function() {
var remaining = 100 - (status * 100);
var inc = settings.trickleRate * Math.pow(1 - Math.sqrt(remaining), 2);
return publicMethods.inc(inc/100);
},
clamp: function(num, min, max) {
if (num < min) {
return min;
}
if (num > max) {
return max;
}
return num;
},
toBarPercents: function(num) {
return num * 100;
},
positioning: function(num, speed, ease) {
return {
'width': this.toBarPercents(num) + '%',
'transition': 'all ' + speed + 'ms ' + ease
};
}
};
var publicMethods = {
set: function(num) {
var $progress = privateMethods.render();
num = privateMethods.clamp(num, settings.minimum, 1);
status = (num === 1 ? null : num);
setTimeout(function() {
$progress.children().eq(0).css(privateMethods.positioning(num, settings.speed, settings.ease));
}, 100);
if (num === 1) {
setTimeout(function() {
$progress.css({
'transition': 'all ' + settings.speed + 'ms linear',
'opacity': 0
});
setTimeout(function() {
privateMethods.remove();
}, settings.speed);
}, settings.speed);
}
return publicMethods;
},
get: function() {
return status;
},
start: function() {
if (!status) {
publicMethods.set(0);
}
var worker = function() {
setTimeout(function() {
if (!status) {
return;
}
privateMethods.trickle();
worker();
}, settings.trickleSpeed);
};
worker();
return publicMethods;
},
inc: function(amount) {
var n = status;
if (!n) {
return publicMethods.start();
}
if (typeof amount !== 'number') {
amount = (1 - n) * privateMethods.clamp(Math.random() * n, 0.1, 0.95);
}
n = privateMethods.clamp(n + amount, 0, 0.994);
return publicMethods.set(n);
},
done: function() {
if (status) {
publicMethods.inc(0.3 + 0.5 * Math.random()).set(1);
}
}
};
return publicMethods;
};
}
];
});
})(window, window.angular, _);