-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadAnimation.js
119 lines (97 loc) · 2.67 KB
/
LoadAnimation.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
/**
* See https://github.com/mattjs/js-libraries for license
* Requires: https://github.com/fgnass/spin.js
*/
;(function(context, undefined) {
'use strict';
var loaders = [];
var start = function(size, element, absolute, color) {
var l = new Loader(size, element, absolute, color)
loaders.push(l);
return l.element();
}
var remove = function(element, cb) {
for(var i in loaders) {
if(loaders[i].parent() == element) {
loaders[i].remove(cb);
loaders.splice(i,1);
}
}
}
function Loader(size, parent, absolute, options) {
var inner_diameter = 0.4;
var element = document.createElement('div');
var settings = {
lines: 11, // The number of lines to draw
corners: 1, // Corner roundness (0..1)
color: '#000', // #rgb or #rrggbb
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: true, // Whether to use hardware acceleration
className: 'spinner', // The CSS class to assign to the spinner
top: 0, // Top position relative to parent in px
left: 0 // Left position relative to parent in px
}
if(options != undefined && 'color' in options) {
settings['color'] = '#'+options['color'];
}
var init = function() {
loader_settings();
configure_container();
var spinner = new Spinner(settings);
spinner.spin(element);
parent.appendChild(element);
}
var configure_container = function() {
var style = 'height:'+size+'px; width: '+size+'px;';
if(absolute) {
style += ' position: absolute;';
if(options != undefined && 'left' in options) {
style += ' left:'+options['left']+'px;';
} else {
style += ' left: 50%; margin-left: -'+(size/2)+'px;';
}
if(options != undefined && 'top' in options) {
style += ' top:'+options['top']+'px;';
} else {
style += ' top: 50%; margin-top: -'+(size/2)+'px;';
}
}
element.setAttribute('style', style);
}
var loader_settings = function() {
// Must be even
if(is_odd(size)) {
size = size - 1;
}
settings.radius = Math.floor((size*inner_diameter)/2);
settings.width = Math.floor(size/12);
settings.length = (size - settings.radius*2)/2 - settings.width;
}
var is_odd = function(value) {
return !!(value%2);
}
this.remove = function(cb) {
$(element).fadeOut({
complete: function() {
parent.removeChild(element);
if(typeof cb == 'function') {
cb();
}
}
})
}
this.parent = function() {
return parent;
}
this.element = function() {
return element;
}
init();
}
context.LoadAnimation = {
start: start,
remove: remove
};
window.LoadAnimation = LoadAnimation;
}(this));