oxjs/demos/progress/js/progress.js

77 lines
2.5 KiB
JavaScript
Raw Normal View History

2012-01-02 08:25:34 +00:00
'use strict';
2011-09-01 21:38:57 +00:00
2012-01-02 08:25:34 +00:00
Ox.load('UI', {debug: true}, function() {
var cancelled = false,
paused = false,
2011-09-01 13:13:47 +00:00
width = 384,
$progressbar = Ox.Progressbar({
showCancelButton: true,
showPauseButton: true,
showPercent: true,
2012-01-02 08:25:34 +00:00
showRestartButton: true,
2011-09-01 13:13:47 +00:00
showTime: true,
width: width
})
2011-09-01 21:38:57 +00:00
.css({margin: '16px'})
.appendTo(Ox.UI.$body)
2011-09-01 13:13:47 +00:00
.bindEvent({
2012-01-02 08:25:34 +00:00
cancel: function() {
cancelled = true;
setPercent();
},
complete: function() {
clearInterval(interval);
},
2011-09-01 13:13:47 +00:00
pause: function() {
paused = true;
2012-01-02 08:25:34 +00:00
setPercent();
2011-09-01 13:13:47 +00:00
},
2012-01-02 08:25:34 +00:00
restart: function() {
cancelled = false;
2011-09-01 13:13:47 +00:00
paused = false;
2012-01-02 08:25:34 +00:00
progress = 0;
2011-09-01 13:13:47 +00:00
},
2012-01-02 08:25:34 +00:00
resume: function() {
paused = false;
2011-09-01 13:13:47 +00:00
}
}),
2011-09-01 21:38:57 +00:00
$status = Ox.Label({
2011-09-01 13:13:47 +00:00
width: width
2011-09-01 21:38:57 +00:00
})
.css({marginLeft: '16px'})
2012-01-02 08:25:34 +00:00
.appendTo(Ox.UI.$body),
2011-09-01 21:38:57 +00:00
$percent = $('<div>')
2011-09-01 13:13:47 +00:00
.css({float: 'left', width: '64px', fontWeight: 'bold'})
2012-01-02 08:25:34 +00:00
.appendTo($status),
2011-09-01 21:38:57 +00:00
$remaining = $('<div>')
2011-09-01 13:13:47 +00:00
.css({float: 'left', width: width - 64 - 16 + 'px', textAlign: 'right'})
2012-01-02 08:25:34 +00:00
.appendTo($status),
2011-09-01 21:38:57 +00:00
progress = 0,
i = 0,
interval = setInterval(function() {
2012-01-02 08:25:34 +00:00
if (!cancelled && !paused) {
2011-09-01 13:13:47 +00:00
if (Math.random() < 0.25) {
progress += 0.01;
$progressbar.options({progress: progress});
}
if (i % 10 == 0 || progress >= 1) {
var status = $progressbar.status();
2012-01-02 08:25:34 +00:00
setPercent();
$remaining.html('Remaining: ' + (
status.remaining == 0 ? 'done'
: status.remaining == Infinity ? 'unknown'
: Ox.formatDuration(status.remaining, 'long')
));
2011-09-01 13:13:47 +00:00
}
if (progress >= 1) {
clearInterval(interval);
}
i++;
2011-09-01 21:38:57 +00:00
}
}, 25);
2012-01-02 08:25:34 +00:00
function setPercent() {
$percent.html(
Math.round($progressbar.options('progress') * 100) + '%'
);
}
2011-09-01 21:38:57 +00:00
});