jquery-ui/ui/ui.progressbar.js

185 lines
4.4 KiB
JavaScript
Raw Normal View History

/*
* jQuery UI ProgressBar
*
2008-08-09 23:06:18 +00:00
* Copyright (c) 2008 Eduardo Lundgren
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
2008-08-08 05:56:57 +00:00
*
* http://docs.jquery.com/UI/ProgressBar
*
* Depends:
* ui.core.js
*/
(function($) {
$.widget("ui.progressbar", {
init: function() {
2008-08-09 23:06:18 +00:00
$.extend(this.options, {
_interval: this.options.interval
});
2008-08-08 05:56:57 +00:00
2008-08-09 23:06:18 +00:00
var self = this,
options = this.options,
id = (new Date()).getTime()+Math.random(),
text = options.text || '0%';
2008-08-08 05:56:57 +00:00
2008-08-09 23:06:18 +00:00
this.element.addClass("ui-progressbar").width(options.width);
$.extend(this, {
active: false,
pixelState: 0,
percentState: 0,
identifier: id,
bar: $('<div class="ui-progressbar-bar ui-hidden"></div>').css({
width: '0px', overflow: 'hidden', zIndex: 100
}),
textElement: $('<div class="ui-progressbar-text"></div>').html(text).css({
width: '0px', overflow: 'hidden'
}),
textBg: $('<div class="ui-progressbar-text ui-progressbar-text-back"></div>').html(text).css({
2008-08-09 23:06:18 +00:00
width: this.element.width()
}),
wrapper: $('<div class="ui-progressbar-wrap"></div>')
});
2008-08-08 05:56:57 +00:00
this.wrapper
2008-08-09 23:06:18 +00:00
.append(this.bar.append(this.textElement.addClass(options.textClass)), this.textBg)
.appendTo(this.element);
2008-08-09 23:51:46 +00:00
jQuery.easing[this.identifier] = function (x, t, b, c, d) {
var inc = options.increment,
width = options.width,
step = ((inc > width ? width : inc)/width),
state = Math.round(x/step)*step;
return state > 1 ? 1 : state;
};
},
2008-08-08 05:56:57 +00:00
plugins: {},
ui: function(e) {
return {
instance: this,
2008-08-09 23:06:18 +00:00
identifier: this.identifier,
options: this.options,
2008-08-09 23:06:18 +00:00
element: this.bar,
textElement: this.textElement,
pixelState: this.pixelState,
percentState: this.percentState
};
},
propagate: function(n,e) {
$.ui.plugin.call(this, n, [e, this.ui()]);
this.element.triggerHandler(n == "progressbar" ? n : ["progressbar", n].join(""), [e, this.ui()], this.options[n]);
},
destroy: function() {
2008-08-09 23:51:46 +00:00
this.stop();
2008-08-08 05:56:57 +00:00
this.element
.removeClass("ui-progressbar ui-progressbar-disabled")
.removeData("progressbar").unbind(".progressbar")
.find('.ui-progressbar-wrap').remove();
2008-08-09 23:06:18 +00:00
delete jQuery.easing[this.identifier];
},
enable: function() {
this.element.removeClass("ui-progressbar-disabled");
this.disabled = false;
},
disable: function() {
this.element.addClass("ui-progressbar-disabled");
this.disabled = true;
},
start: function() {
2008-08-09 23:06:18 +00:00
var self = this, options = this.options;
2008-08-08 05:56:57 +00:00
2008-08-09 23:51:46 +00:00
if (this.disabled) {
return;
2008-08-09 23:06:18 +00:00
};
2008-08-09 23:51:46 +00:00
self.active = true;
setTimeout(
function() {
self.active = false;
},
options.duration
);
this.animate();
this.propagate('start', this.ui());
return false;
},
animate: function() {
var self = this,
options = this.options,
interval = options.interval;
2008-08-09 23:06:18 +00:00
this.bar.animate(
{
width: options.width
},
{
2008-08-09 23:51:46 +00:00
duration: interval,
2008-08-09 23:06:18 +00:00
easing: this.identifier,
step: function(step, b) {
self.progress((step/options.width)*100);
2008-08-09 23:51:46 +00:00
var elapsedTime = ((new Date().getTime()) - b.startTime);
options.interval = interval - elapsedTime;
2008-08-09 23:06:18 +00:00
},
complete: function() {
delete jQuery.easing[self.identifier];
2008-08-09 23:51:46 +00:00
self.pause();
if (self.active) {
/*TODO*/
self.stop();
self.animate();
}
}
}
2008-08-09 23:06:18 +00:00
);
},
2008-08-09 23:51:46 +00:00
pause: function() {
2008-08-09 23:06:18 +00:00
if (this.disabled) return;
this.bar.stop();
2008-08-09 23:51:46 +00:00
this.propagate('pause', this.ui());
},
2008-08-09 23:51:46 +00:00
stop: function() {
2008-08-09 23:06:18 +00:00
this.bar.stop();
this.bar.width(0);
this.textElement.width(0);
this.bar.addClass('ui-hidden');
2008-08-09 23:06:18 +00:00
this.options.interval = this.options._interval;
2008-08-09 23:51:46 +00:00
this.propagate('stop', this.ui());
},
2008-08-09 23:06:18 +00:00
progress: function(percentState) {
if (this.bar.is('.ui-hidden')) {
this.bar.removeClass('ui-hidden');
}
this.percentState = percentState > 100 ? 100 : percentState;
this.pixelState = (this.percentState/100)*this.options.width;
this.bar.width(this.pixelState);
this.textElement.width(this.pixelState);
if (this.options.range && !this.options.text) {
this.textElement.html(Math.round(this.percentState) + '%');
}
this.propagate('progress', this.ui());
}
});
$.ui.progressbar.defaults = {
2008-08-09 23:06:18 +00:00
width: 300,
duration: 3000,
2008-08-09 23:06:18 +00:00
interval: 200,
increment: 1,
range: true,
text: '',
2008-08-09 23:06:18 +00:00
addClass: '',
textClass: ''
};
2008-08-09 23:06:18 +00:00
})(jQuery);