1
|
|
2
|
//github.com/fgnass/spin.js#v1.2.5
|
3
|
(function(window, document, undefined) {
|
4
|
|
5
|
/**
|
6
|
* Copyright (c) 2011 Felix Gnass [fgnass at neteye dot de]
|
7
|
* Licensed under the MIT license
|
8
|
*/
|
9
|
|
10
|
var prefixes = ['webkit', 'Moz', 'ms', 'O']; /* Vendor prefixes */
|
11
|
var animations = {}; /* Animation rules keyed by their name */
|
12
|
var useCssAnimations;
|
13
|
|
14
|
/**
|
15
|
* Utility function to create elements. If no tag name is given,
|
16
|
* a DIV is created. Optionally properties can be passed.
|
17
|
*/
|
18
|
function createEl(tag, prop) {
|
19
|
var el = document.createElement(tag || 'div');
|
20
|
var n;
|
21
|
|
22
|
for(n in prop) {
|
23
|
el[n] = prop[n];
|
24
|
}
|
25
|
return el;
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* Appends children and returns the parent.
|
30
|
*/
|
31
|
function ins(parent /* child1, child2, ...*/) {
|
32
|
for (var i=1, n=arguments.length; i<n; i++) {
|
33
|
parent.appendChild(arguments[i]);
|
34
|
}
|
35
|
return parent;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* Insert a new stylesheet to hold the @keyframe or VML rules.
|
40
|
*/
|
41
|
var sheet = function() {
|
42
|
var el = createEl('style');
|
43
|
ins(document.getElementsByTagName('head')[0], el);
|
44
|
return el.sheet || el.styleSheet;
|
45
|
}();
|
46
|
|
47
|
/**
|
48
|
* Creates an opacity keyframe animation rule and returns its name.
|
49
|
* Since most mobile Webkits have timing issues with animation-delay,
|
50
|
* we create separate rules for each line/segment.
|
51
|
*/
|
52
|
function addAnimation(alpha, trail, i, lines) {
|
53
|
var name = ['opacity', trail, ~~(alpha*100), i, lines].join('-');
|
54
|
var start = 0.01 + i/lines*100;
|
55
|
var z = Math.max(1-(1-alpha)/trail*(100-start) , alpha);
|
56
|
var prefix = useCssAnimations.substring(0, useCssAnimations.indexOf('Animation')).toLowerCase();
|
57
|
var pre = prefix && '-'+prefix+'-' || '';
|
58
|
|
59
|
if (!animations[name]) {
|
60
|
sheet.insertRule(
|
61
|
'@' + pre + 'keyframes ' + name + '{' +
|
62
|
'0%{opacity:'+z+'}' +
|
63
|
start + '%{opacity:'+ alpha + '}' +
|
64
|
(start+0.01) + '%{opacity:1}' +
|
65
|
(start+trail)%100 + '%{opacity:'+ alpha + '}' +
|
66
|
'100%{opacity:'+ z + '}' +
|
67
|
'}', 0);
|
68
|
animations[name] = 1;
|
69
|
}
|
70
|
return name;
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Tries various vendor prefixes and returns the first supported property.
|
75
|
**/
|
76
|
function vendor(el, prop) {
|
77
|
var s = el.style;
|
78
|
var pp;
|
79
|
var i;
|
80
|
|
81
|
if(s[prop] !== undefined) return prop;
|
82
|
prop = prop.charAt(0).toUpperCase() + prop.slice(1);
|
83
|
for(i=0; i<prefixes.length; i++) {
|
84
|
pp = prefixes[i]+prop;
|
85
|
if(s[pp] !== undefined) return pp;
|
86
|
}
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Sets multiple style properties at once.
|
91
|
*/
|
92
|
function css(el, prop) {
|
93
|
for (var n in prop) {
|
94
|
el.style[vendor(el, n)||n] = prop[n];
|
95
|
}
|
96
|
return el;
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Fills in default values.
|
101
|
*/
|
102
|
function merge(obj) {
|
103
|
for (var i=1; i < arguments.length; i++) {
|
104
|
var def = arguments[i];
|
105
|
for (var n in def) {
|
106
|
if (obj[n] === undefined) obj[n] = def[n];
|
107
|
}
|
108
|
}
|
109
|
return obj;
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Returns the absolute page-offset of the given element.
|
114
|
*/
|
115
|
function pos(el) {
|
116
|
var o = {x:el.offsetLeft, y:el.offsetTop};
|
117
|
while((el = el.offsetParent)) {
|
118
|
o.x+=el.offsetLeft;
|
119
|
o.y+=el.offsetTop;
|
120
|
}
|
121
|
return o;
|
122
|
}
|
123
|
|
124
|
var defaults = {
|
125
|
lines: 12, // The number of lines to draw
|
126
|
length: 7, // The length of each line
|
127
|
width: 5, // The line thickness
|
128
|
radius: 10, // The radius of the inner circle
|
129
|
rotate: 0, // rotation offset
|
130
|
color: '#000', // #rgb or #rrggbb
|
131
|
speed: 1, // Rounds per second
|
132
|
trail: 100, // Afterglow percentage
|
133
|
opacity: 1/4, // Opacity of the lines
|
134
|
fps: 20, // Frames per second when using setTimeout()
|
135
|
zIndex: 2e9, // Use a high z-index by default
|
136
|
className: 'spinner', // CSS class to assign to the element
|
137
|
top: 'auto', // center vertically
|
138
|
left: 'auto' // center horizontally
|
139
|
};
|
140
|
|
141
|
/** The constructor */
|
142
|
var Spinner = function Spinner(o) {
|
143
|
if (!this.spin) return new Spinner(o);
|
144
|
this.opts = merge(o || {}, Spinner.defaults, defaults);
|
145
|
};
|
146
|
|
147
|
Spinner.defaults = {};
|
148
|
merge(Spinner.prototype, {
|
149
|
spin: function(target) {
|
150
|
this.stop();
|
151
|
var self = this;
|
152
|
var o = self.opts;
|
153
|
var el = self.el = css(createEl(0, {className: o.className}), {position: 'relative', zIndex: o.zIndex});
|
154
|
var mid = o.radius+o.length+o.width;
|
155
|
var ep; // element position
|
156
|
var tp; // target position
|
157
|
|
158
|
if (target) {
|
159
|
target.insertBefore(el, target.firstChild||null);
|
160
|
tp = pos(target);
|
161
|
ep = pos(el);
|
162
|
css(el, {
|
163
|
left: (o.left == 'auto' ? tp.x-ep.x + (target.offsetWidth >> 1) : o.left+mid) + 'px',
|
164
|
top: (o.top == 'auto' ? tp.y-ep.y + (target.offsetHeight >> 1) : o.top+mid) + 'px'
|
165
|
});
|
166
|
}
|
167
|
|
168
|
el.setAttribute('aria-role', 'progressbar');
|
169
|
self.lines(el, self.opts);
|
170
|
|
171
|
if (!useCssAnimations) {
|
172
|
// No CSS animation support, use setTimeout() instead
|
173
|
var i = 0;
|
174
|
var fps = o.fps;
|
175
|
var f = fps/o.speed;
|
176
|
var ostep = (1-o.opacity)/(f*o.trail / 100);
|
177
|
var astep = f/o.lines;
|
178
|
|
179
|
!function anim() {
|
180
|
i++;
|
181
|
for (var s=o.lines; s; s--) {
|
182
|
var alpha = Math.max(1-(i+s*astep)%f * ostep, o.opacity);
|
183
|
self.opacity(el, o.lines-s, alpha, o);
|
184
|
}
|
185
|
self.timeout = self.el && setTimeout(anim, ~~(1000/fps));
|
186
|
}();
|
187
|
}
|
188
|
return self;
|
189
|
},
|
190
|
stop: function() {
|
191
|
var el = this.el;
|
192
|
if (el) {
|
193
|
clearTimeout(this.timeout);
|
194
|
if (el.parentNode) el.parentNode.removeChild(el);
|
195
|
this.el = undefined;
|
196
|
}
|
197
|
return this;
|
198
|
},
|
199
|
lines: function(el, o) {
|
200
|
var i = 0;
|
201
|
var seg;
|
202
|
|
203
|
function fill(color, shadow) {
|
204
|
return css(createEl(), {
|
205
|
position: 'absolute',
|
206
|
width: (o.length+o.width) + 'px',
|
207
|
height: o.width + 'px',
|
208
|
background: color,
|
209
|
boxShadow: shadow,
|
210
|
transformOrigin: 'left',
|
211
|
transform: 'rotate(' + ~~(360/o.lines*i+o.rotate) + 'deg) translate(' + o.radius+'px' +',0)',
|
212
|
borderRadius: (o.width>>1) + 'px'
|
213
|
});
|
214
|
}
|
215
|
for (; i < o.lines; i++) {
|
216
|
seg = css(createEl(), {
|
217
|
position: 'absolute',
|
218
|
top: 1+~(o.width/2) + 'px',
|
219
|
transform: o.hwaccel ? 'translate3d(0,0,0)' : '',
|
220
|
opacity: o.opacity,
|
221
|
animation: useCssAnimations && addAnimation(o.opacity, o.trail, i, o.lines) + ' ' + 1/o.speed + 's linear infinite'
|
222
|
});
|
223
|
if (o.shadow) ins(seg, css(fill('#000', '0 0 4px ' + '#000'), {top: 2+'px'}));
|
224
|
ins(el, ins(seg, fill(o.color, '0 0 1px rgba(0,0,0,.1)')));
|
225
|
}
|
226
|
return el;
|
227
|
},
|
228
|
opacity: function(el, i, val) {
|
229
|
if (i < el.childNodes.length) el.childNodes[i].style.opacity = val;
|
230
|
}
|
231
|
});
|
232
|
|
233
|
/////////////////////////////////////////////////////////////////////////
|
234
|
// VML rendering for IE
|
235
|
/////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
/**
|
238
|
* Check and init VML support
|
239
|
*/
|
240
|
!function() {
|
241
|
|
242
|
function vml(tag, attr) {
|
243
|
return createEl('<' + tag + ' xmlns="urn:schemas-microsoft.com:vml" class="spin-vml">', attr);
|
244
|
}
|
245
|
|
246
|
var s = css(createEl('group'), {behavior: 'url(#default#VML)'});
|
247
|
|
248
|
if (!vendor(s, 'transform') && s.adj) {
|
249
|
|
250
|
// VML support detected. Insert CSS rule ...
|
251
|
sheet.addRule('.spin-vml', 'behavior:url(#default#VML)');
|
252
|
|
253
|
Spinner.prototype.lines = function(el, o) {
|
254
|
var r = o.length+o.width;
|
255
|
var s = 2*r;
|
256
|
|
257
|
function grp() {
|
258
|
return css(vml('group', {coordsize: s +' '+s, coordorigin: -r +' '+-r}), {width: s, height: s});
|
259
|
}
|
260
|
|
261
|
var margin = -(o.width+o.length)*2+'px';
|
262
|
var g = css(grp(), {position: 'absolute', top: margin, left: margin});
|
263
|
|
264
|
var i;
|
265
|
|
266
|
function seg(i, dx, filter) {
|
267
|
ins(g,
|
268
|
ins(css(grp(), {rotation: 360 / o.lines * i + 'deg', left: ~~dx}),
|
269
|
ins(css(vml('roundrect', {arcsize: 1}), {
|
270
|
width: r,
|
271
|
height: o.width,
|
272
|
left: o.radius,
|
273
|
top: -o.width>>1,
|
274
|
filter: filter
|
275
|
}),
|
276
|
vml('fill', {color: o.color, opacity: o.opacity}),
|
277
|
vml('stroke', {opacity: 0}) // transparent stroke to fix color bleeding upon opacity change
|
278
|
)
|
279
|
)
|
280
|
);
|
281
|
}
|
282
|
|
283
|
if (o.shadow) {
|
284
|
for (i = 1; i <= o.lines; i++) {
|
285
|
seg(i, -2, 'progid:DXImageTransform.Microsoft.Blur(pixelradius=2,makeshadow=1,shadowopacity=.3)');
|
286
|
}
|
287
|
}
|
288
|
for (i = 1; i <= o.lines; i++) seg(i);
|
289
|
return ins(el, g);
|
290
|
};
|
291
|
Spinner.prototype.opacity = function(el, i, val, o) {
|
292
|
var c = el.firstChild;
|
293
|
o = o.shadow && o.lines || 0;
|
294
|
if (c && i+o < c.childNodes.length) {
|
295
|
c = c.childNodes[i+o]; c = c && c.firstChild; c = c && c.firstChild;
|
296
|
if (c) c.opacity = val;
|
297
|
}
|
298
|
};
|
299
|
}
|
300
|
else {
|
301
|
useCssAnimations = vendor(s, 'animation');
|
302
|
}
|
303
|
}();
|
304
|
|
305
|
window.Spinner = Spinner;
|
306
|
|
307
|
})(window, document);
|