forked from voronianski/ngprogress-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngprogress-lite.js
140 lines (111 loc) · 3.25 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
/*
* ngProgressLite - small && slim angular progressbars
* http://github.com/voronianski/ngprogress-lite
* Dmitri Voronianski http://pixelhunter.me
* (c) 2013 MIT License
*/
(function (window, angular, undefined) {
'use strict';
angular.module('ngProgressLite', []).provider('ngProgressLite', function () {
// global configs
var settings = this.settings = {
minimum: 0.08,
speed: 300,
ease: 'ease',
trickleRate: 0.02,
trickleSpeed: 500,
template: '<div class="ngProgressLite"><div class="ngProgressLiteBar"><div class="ngProgressLiteBarShadow"></div></div></div>'
};
this.$get = ['$document', function ($document) {
var $body = $document.find('body');
var $progressBarEl, status, cleanForElement;
var privateMethods = {
render: function () {
if (this.isRendered()) {
return $progressBarEl;
}
$body.addClass('ngProgressLite-on');
$progressBarEl = angular.element(settings.template);
$body.append($progressBarEl);
cleanForElement = false;
return $progressBarEl;
},
remove: function () {
$body.removeClass('ngProgressLite-on');
$progressBarEl.remove();
cleanForElement = true;
},
isRendered: function () {
return $progressBarEl && $progressBarEl.children().length > 0 && !cleanForElement;
},
trickle: function () {
return publicMethods.inc(Math.random() * settings.trickleRate);
},
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);