oxjs/source/UI/js/Core/LoadingIcon.js

103 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2012-05-21 10:38:18 +00:00
2011-05-16 08:24:46 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.LoadingIcon <f> Loading Icon Element
2011-05-16 08:24:46 +00:00
options <o> Options object
size <n|s|16> size of icon
2011-05-16 08:24:46 +00:00
self <o> Shared private variable
([options[, self]]) -> <o:Ox.Element> Loading Icon Element
2011-05-16 08:24:46 +00:00
@*/
2011-04-22 22:03:10 +00:00
Ox.LoadingIcon = function(options, self) {
2012-01-27 14:29:11 +00:00
self = self || {};
var that = Ox.Element('<img>', self)
2012-01-17 17:34:33 +00:00
.defaults({
size: 16,
video: false
2012-01-17 17:34:33 +00:00
})
.options(options || {})
.addClass('OxLoadingIcon')
.attr({
2014-09-26 12:31:20 +00:00
src: Ox.UI.getImageURL(
'symbolLoading',
2014-02-04 09:22:55 +00:00
self.options.video ? 'videoIcon' : null
)
});
Ox.isNumber(self.options.size)
? that.css({width: self.options.size, height: self.options.size})
: that.addClass('Ox' + Ox.toTitleCase(self.options.size));
2012-01-27 14:29:11 +00:00
2011-05-16 08:24:46 +00:00
/*@
start <f> Start loading animation
() -> <f> Loading Icon Element
@*/
that.start = function(callback) {
2012-05-24 13:43:46 +00:00
var css, deg = 0, previousTime = +new Date();
function step() {
var currentTime = +new Date(),
delta = (currentTime - previousTime) / 1000,
next = Math.round((deg + delta * 360) % 360 / 30) * 30;
if (deg != next) {
2012-05-24 13:43:46 +00:00
previousTime = currentTime;
deg = next;
2012-05-24 13:43:46 +00:00
css = 'rotate(' + deg + 'deg)';
that.css({
2012-05-24 13:43:46 +00:00
MozTransform: css,
2012-05-25 16:28:05 +00:00
MsTransform: css,
2012-05-24 17:12:02 +00:00
OTransform: css,
WebkitTransform: css,
transform: css
});
}
if (!self.stopping && !Ox.isUndefined(window.requestAnimationFrame)) {
self.loadingInterval = window.requestAnimationFrame(step);
}
}
if (!self.loadingInterval) {
if (Ox.isUndefined(window.requestAnimationFrame)) {
self.loadingInterval = setInterval(step, 83);
} else {
self.loadingInterval = window.requestAnimationFrame(step);
}
that.animate({opacity: 1}, 250, function() {
callback && callback();
});
}
2011-04-22 22:03:10 +00:00
return that;
};
2012-01-27 14:29:11 +00:00
2011-05-16 08:24:46 +00:00
/*@
stop <f> Stop loading animation
() -> <f> Loading Icon Element
@*/
that.stop = function(callback) {
2013-07-29 10:49:48 +00:00
if (self.loadingInterval && !self.stopping) {
self.stopping = true;
that.animate({opacity: 0}, 250, function() {
var css = 'rotate(0deg)';
if (Ox.isUndefined(window.cancelAnimationFrame)) {
clearInterval(self.loadingInterval);
} else {
window.cancelAnimationFrame(self.loadingInterval);
}
2013-07-29 10:49:48 +00:00
self.loadingInterval = null;
self.stopping = false;
that.css({
MozTransform: css,
MsTransform: css,
OTransform: css,
WebkitTransform: css,
transform: css
});
callback && callback();
2012-01-17 17:34:33 +00:00
});
}
2011-04-22 22:03:10 +00:00
return that;
};
2012-01-27 14:29:11 +00:00
2011-04-22 22:03:10 +00:00
return that;
2012-01-27 14:29:11 +00:00
};