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