2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-09-01 09:35:45 +00:00
|
|
|
Ox.Progressbar = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({}, self)
|
|
|
|
.defaults({
|
2011-09-01 13:13:47 +00:00
|
|
|
paused: false,
|
2011-09-01 09:35:45 +00:00
|
|
|
progress: 0,
|
2011-09-01 13:13:47 +00:00
|
|
|
showCancelButton: false,
|
|
|
|
showPauseButton: false,
|
|
|
|
showPercent: false,
|
|
|
|
showTime: false,
|
2011-09-01 09:35:45 +00:00
|
|
|
width: 256
|
|
|
|
})
|
|
|
|
.options(options || {})
|
|
|
|
.addClass('OxProgressbar')
|
2011-09-01 21:38:57 +00:00
|
|
|
.css({width: self.options.width - 2 + 'px'});
|
2011-09-01 09:35:45 +00:00
|
|
|
|
2011-09-01 13:13:47 +00:00
|
|
|
self.trackWidth = self.options.width
|
|
|
|
- self.options.showPercent * 36
|
|
|
|
- self.options.showTime * 60
|
|
|
|
- self.options.showPauseButton * 16
|
|
|
|
- self.options.showCancelButton * 16;
|
2011-09-01 09:35:45 +00:00
|
|
|
|
2011-09-01 13:13:47 +00:00
|
|
|
self.$track = $('<div>')
|
|
|
|
.addClass('OxTrack')
|
|
|
|
.css({
|
|
|
|
width: self.trackWidth - 2 + 'px'
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
2011-09-01 09:35:45 +00:00
|
|
|
|
2011-09-01 13:13:47 +00:00
|
|
|
self.$progress = $('<div>')
|
|
|
|
.addClass('OxProgress')
|
|
|
|
.appendTo(self.$track);
|
|
|
|
|
|
|
|
if (self.options.showPercent) {
|
|
|
|
self.$percent = $('<div>')
|
|
|
|
.addClass('OxText')
|
|
|
|
.css({width: '36px'})
|
|
|
|
.appendTo(that);
|
2011-09-01 09:35:45 +00:00
|
|
|
}
|
|
|
|
|
2011-09-01 13:13:47 +00:00
|
|
|
if (self.options.showTime) {
|
|
|
|
self.$time = $('<div>')
|
|
|
|
.addClass('OxText')
|
|
|
|
.css({width: '60px'})
|
|
|
|
.appendTo(that);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.options.showPauseButton) {
|
|
|
|
self.$pauseButton = Ox.Button({
|
|
|
|
style: 'symbol',
|
|
|
|
title: [
|
|
|
|
{id: 'pause', title: 'pause', selected: !self.options.paused},
|
|
|
|
{id: 'resume', title: 'redo', selected: self.options.paused}
|
|
|
|
],
|
|
|
|
tooltip: ['Pause', 'Resume'],
|
|
|
|
type: 'image'
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
click: togglePaused
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
|
|
|
}
|
2011-09-01 21:38:57 +00:00
|
|
|
|
2011-09-01 13:13:47 +00:00
|
|
|
if (self.options.showCancelButton) {
|
|
|
|
self.$cancelButton = Ox.Button({
|
|
|
|
style: 'symbol',
|
|
|
|
title: 'close',
|
|
|
|
tooltip: 'Cancel',
|
|
|
|
type: 'image'
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
click: cancel
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
|
|
|
}
|
|
|
|
|
|
|
|
!self.options.paused && resume();
|
|
|
|
|
|
|
|
function cancel() {
|
|
|
|
self.cancelled = true;
|
2011-09-02 00:32:23 +00:00
|
|
|
stop();
|
2011-09-01 13:13:47 +00:00
|
|
|
that.triggerEvent('cancel');
|
|
|
|
}
|
|
|
|
|
|
|
|
function pause() {
|
|
|
|
self.pauseTime = +new Date();
|
|
|
|
self.$progress.removeClass('OxAnimate');
|
2011-09-03 15:25:52 +00:00
|
|
|
($.browser.mozilla || $.browser.opera) && clearInterval(self.interval);
|
2011-09-01 13:13:47 +00:00
|
|
|
self.$time && self.$time
|
|
|
|
.addClass('OxSmall')
|
|
|
|
.html(self.cancelled ? 'Cancelled' : 'Paused');
|
|
|
|
}
|
|
|
|
|
|
|
|
function resume() {
|
|
|
|
self.startTime = !self.startTime
|
|
|
|
? +new Date()
|
|
|
|
: self.startTime + +new Date() - self.pauseTime;
|
2011-09-01 21:38:57 +00:00
|
|
|
self.$progress.addClass('OxAnimate');
|
2011-09-03 15:25:52 +00:00
|
|
|
if ($.browser.mozilla || $.browser.opera) {
|
2011-09-01 21:38:57 +00:00
|
|
|
self.offset = 0;
|
|
|
|
self.interval = setInterval(function() {
|
|
|
|
self.$progress.css({backgroundPosition: --self.offset + 'px 0, 0 0'})
|
|
|
|
}, 1000 / 32);
|
|
|
|
}
|
2011-09-01 13:13:47 +00:00
|
|
|
self.$time && self.$time
|
|
|
|
.removeClass('OxSmall')
|
|
|
|
.html(self.options.progress ? Ox.formatDuration(that.status().remaining) : 'unknown');
|
|
|
|
}
|
|
|
|
|
|
|
|
function setProgress() {
|
|
|
|
self.$percent && self.$percent.html(
|
|
|
|
Math.floor(self.options.progress * 100) + '%'
|
|
|
|
);
|
|
|
|
self.$time && self.$time.html(
|
|
|
|
self.options.progress ? Ox.formatDuration(that.status().remaining) : 'unknown'
|
|
|
|
);
|
|
|
|
self.$progress.stop().animate({
|
|
|
|
width: Math.round(14 + self.options.progress * (self.trackWidth - 16)) + 'px'
|
|
|
|
}, 250, function() {
|
2011-09-02 00:32:23 +00:00
|
|
|
self.options.progress == 1 && stop();
|
2011-09-01 13:13:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-09-02 00:32:23 +00:00
|
|
|
function stop() {
|
|
|
|
pause();
|
|
|
|
self.$time && self.$time
|
|
|
|
.addClass('OxSmall')
|
|
|
|
.html(self.cancelled ? 'Cancelled' : 'Complete');
|
|
|
|
self.$pauseButton.options({disabled: true});
|
|
|
|
self.$cancelButton.options({disabled: true});
|
|
|
|
}
|
|
|
|
|
2011-09-01 13:13:47 +00:00
|
|
|
function togglePaused() {
|
|
|
|
self.options.paused = !self.options.paused;
|
|
|
|
if (self.options.paused) {
|
|
|
|
pause()
|
|
|
|
} else {
|
|
|
|
resume();
|
|
|
|
}
|
|
|
|
that.triggerEvent(self.options.paused ? 'pause' : 'resume');
|
|
|
|
}
|
|
|
|
|
|
|
|
self.setOption = function(key, value) {
|
|
|
|
if (key == 'paused') {
|
|
|
|
togglePaused();
|
|
|
|
} if (key == 'progress') {
|
|
|
|
self.options.progress = Ox.limit(self.options.progress, 0, 1);
|
|
|
|
!self.options.paused && !self.options.cancelled && setProgress();
|
|
|
|
}
|
2011-09-01 21:38:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
that.status = function() {
|
2011-09-01 13:13:47 +00:00
|
|
|
var elapsed = +new Date() - self.startTime,
|
|
|
|
remaining = elapsed / self.options.progress * (1 - self.options.progress);
|
2011-09-01 21:38:57 +00:00
|
|
|
return {
|
2011-09-01 13:13:47 +00:00
|
|
|
elapsed: Math.floor(elapsed / 1000),
|
|
|
|
remaining: self.options.progress
|
|
|
|
? Math.ceil(remaining / 1000)
|
|
|
|
: Infinity
|
2011-09-01 21:38:57 +00:00
|
|
|
};
|
|
|
|
};
|
2011-09-01 09:35:45 +00:00
|
|
|
|
|
|
|
return that;
|
2011-11-03 15:42:41 +00:00
|
|
|
|
2011-09-01 09:35:45 +00:00
|
|
|
};
|