Apache-UploadMeter
view release on metacpan or search on metacpan
lib/Apache/UploadMeter/Resources/JavaScript.pm view on Meta::CPAN
// but it makes for uglier code :-(
// Constructor - Parameters are:
// el: Outer div to bind widget to
// meter: uploadmeter id
// url: uploadmeter ajax url
// options: additional callbacks, etc
initialize: function(el, meter, url, options) {
this.url = url;
this.meter = meter;
this.main = $(el);
Object.extend(this.options, options || {});
this.desc = Builder.node( "div", {className:'metercontent'});
this.main.appendChild(this.desc);
this.desc = $(this.desc);
Position.absolutize(this.desc);
Position.clone(this.main, this.desc);
this.ul = Builder.node( "div", {className:'meterunderlay'});
this.main.appendChild(this.ul);
this.ul = $(this.ul);
Position.absolutize(this.ul);
Position.clone(this.main, this.ul);
this.ul.setStyle({width: "0px"});
},
start: function() {
// Restart paused meter
if (this.ajax !== undefined) {
this.ajax.start();
return;
}
// Call onCreate hook
try {
(this.options.onCreate || Prototype.emptyFunction)();
UploadMeter.Responders.dispatch('onCreate', this);
} catch (e) {
this.dispatchException(e);
}
var errorFunc = function (transport) {
Element.setStyle(this.main, {'border-color': 'yellow'});
Element.update(this.desc, "Unknown");
Element.setStyle(this.ul, {'background-color': 'yellow'}).hide();
Position.clone(this.main, this.ul);
Element.show(this.ul);
}.bind(this);
// New meter
var params = "format=json&meter_id=" + this.meter;
new Ajax.Request(this.url, {
parameters: params,
onSuccess: function(transport) {
// Handle response
this.__onSuccess(transport);
// Start periodic
params = params + "&returned=1";
this.ajax = new PeriodicalExecuter(function() {
new Ajax.Request(this.url, {
parameters: params,
onSuccess: this.__onSuccess.bind(this)
});}.bind(this), this.options.delay);
}.bind(this),
on404: errorFunc,
on400: errorFunc
}); /* first request */
},
stop: function() {
try {this.ajax.stop();} catch (e) {}
},
__slide: function(element) {
element = $(element);
var elementDimensions = element.getDimensions();
return new Effect.Scale(element, 100, Object.extend({
scaleContent: false,
scaleY: false,
scaleFrom: 0,
scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
restoreAfterFinish: true,
afterSetup: function(effect) {
effect.element.makeClipping().setStyle({width: '0px'}).show();
},
afterFinishInternal: function(effect) {
effect.element.undoClipping();
},
afterFinish: function(effect) {
this.ul.setStyle({width: parseInt(this.ul.getStyle('width')) + parseInt(Element.getStyle(effect.element, 'width')) + "px"});
effect.element.remove();
}.bind(this)
}, arguments[1] || {}));
},
__onSuccess: function(transport) {
var json = eval('(' + transport.responseText + ')');
var last = this.status;
if (last===undefined) {
last = json;
try {
(this.options.onInitialize || Prototype.emptyFunction)(json, last);
UploadMeter.Responders.dispatch('onInitialize', this, json, last);
} catch (e) {
this.dispatchException(e);
}
}
var total = json.status.total;
var seen = json.status.received;
var progress = seen / total * 100;
var currentrate = Try.these(
function(){return (json.status.received - last.status.received) / (json.status.timestamp - last.status.timestamp);}
) || 0;
var elapsed = json.status.timestamp - json.status.start;
var remaining = Try.these(
function() {return (json.finished ? -1 : Math.round((json.status.total - json.status.received) / currentrate));}
) || "Unknown";
// Set to -1 so it doesn't trip the "Unknown"
if (remaining == -1) {remaining = 0;}
Object.extend(json, {
'total': total,
'seen': seen,
( run in 1.614 second using v1.01-cache-2.11-cpan-39bf76dae61 )