jquery-ui/ui/ui.progressbar.js

182 lines
4.3 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", {
2008-08-16 14:45:28 +00:00
_init: function() {
this._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 {
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-23 02:38:28 +00:00
}
2008-08-09 23:51:46 +00:00
self.active = true;
setTimeout(
function() {
self.active = false;
},
options.duration
);
this._animate();
2008-08-09 23:51:46 +00:00
this._propagate('start', this.ui());
2008-08-09 23:51:46 +00:00
return false;
},
_animate: function() {
2008-08-09 23:51:46 +00:00
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:51:46 +00:00
}
}
}
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();
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');
this.options.interval = this._interval;
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);