Stackedit/public/res/extensions/workingIndicator.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
2013-05-29 19:55:23 +00:00
"jquery",
2013-06-22 23:48:57 +00:00
"underscore",
2014-03-19 22:10:59 +00:00
"crel",
2013-11-17 13:32:53 +00:00
"classes/Extension"
2014-03-19 22:10:59 +00:00
], function ($, _, crel, Extension) {
2013-05-29 19:55:23 +00:00
2013-06-22 23:48:57 +00:00
var workingIndicator = new Extension("workingIndicator", "Working Indicator");
2013-05-29 19:55:23 +00:00
2014-03-19 22:10:59 +00:00
var keyframeTemlate = [
'@<%= prefix %>keyframes <%= name %> {',
' 0% { opacity:<%= z %>; }',
' <%= start %>.01% { opacity:<%= alpha %>; }',
' <%= start %>.02% { opacity:1; }',
' <%= ((start + trail) % 100) %>.01% { opacity:<%= alpha %>; }',
' 100% { opacity:<%= z %>; }',
'}'
].join('\n');
2013-11-07 23:10:38 +00:00
var $bodyElt;
var $workingIndicatorElt;
2013-11-16 18:09:33 +00:00
workingIndicator.onAsyncRunning = function (isRunning) {
2013-08-22 00:19:59 +00:00
$bodyElt.toggleClass("working", isRunning);
2013-12-03 00:29:57 +00:00
$workingIndicatorElt.toggleClass("hide", !isRunning);
2013-08-22 00:19:59 +00:00
};
2014-03-19 22:10:59 +00:00
2013-11-16 18:09:33 +00:00
workingIndicator.onReady = function () {
2014-03-19 22:10:59 +00:00
var styleContent = '';
function addKeyframe(params) {
params.z = Math.max(1 - (1-params.alpha) / params.trail * (100-params.start), params.alpha);
styleContent += _.template(keyframeTemlate, _.extend({
prefix: ''
}, params));
styleContent += _.template(keyframeTemlate, _.extend({
prefix: '-webkit-'
}, params));
}
2013-08-22 00:19:59 +00:00
$bodyElt = $(document.body);
2013-11-23 15:41:02 +00:00
$workingIndicatorElt = $('<div class="hide">');
$('.working-indicator').append($workingIndicatorElt);
2013-12-03 00:20:16 +00:00
for (var i = 0; i < 3; i++) {
2014-03-19 22:10:59 +00:00
var name = 'working-indicator-bar' + i;
addKeyframe({
name: name,
alpha: 0.25,
start: 20 * i,
trail: 50
});
var animation = name + ' 0.7s linear infinite';
2013-11-17 13:32:53 +00:00
$workingIndicatorElt.append($('<div class="bar">').css({
2014-03-19 22:10:59 +00:00
'animation': animation,
'-webkit-animation': animation,
2013-11-17 13:32:53 +00:00
}));
2013-11-16 18:09:33 +00:00
}
2014-03-19 22:10:59 +00:00
var styleElt = crel('style', {
type : 'text/css'
});
document.head.appendChild(styleElt);
styleElt.innerHTML = styleContent;
2013-05-29 19:55:23 +00:00
};
return workingIndicator;
2014-03-19 22:10:59 +00:00
});