/*
* completely stolen off mongrel_upload_progress and by somebody
* with little JavaScript interest/knowledge
*/
var UploadProgress = {
uploading: null,
monitor: function(upid) {
if(!this.periodicExecuter) {
this.periodicExecuter = new PeriodicalExecuter(function() {
if(!UploadProgress.uploading) return;
new Ajax.Request('/files/progress?upload_id=' + upid);
}, 3);
}
this.uploading = true;
this.StatusBar.create();
},
update: function(total, current) {
if(!this.uploading) return;
if(!total)
total = 2147483647;
var status = current / total;
var statusHTML = status.toPercentage();
$('results').innerHTML = statusHTML + "
" + current.toHumanSize() + ' of ' + total.toHumanSize() + " uploaded.";
this.StatusBar.update(status, statusHTML);
},
finish: function() {
this.uploading = false;
this.StatusBar.finish();
$('results').innerHTML = 'finished!';
},
cancel: function(msg) {
if(!this.uploading) return;
this.uploading = false;
if(this.StatusBar.statusText) this.StatusBar.statusText.innerHTML = msg || 'canceled';
},
StatusBar: {
statusBar: null,
statusText: null,
statusBarWidth: 500,
create: function() {
this.statusBar = this._createStatus('status-bar');
this.statusText = this._createStatus('status-text');
this.statusText.innerHTML = '0%';
this.statusBar.style.width = '0';
},
update: function(status, statusHTML) {
this.statusText.innerHTML = statusHTML;
this.statusBar.style.width = Math.floor(this.statusBarWidth * status);
},
finish: function() {
this.statusText.innerHTML = '100%';
this.statusBar.style.width = '100%';
},
_createStatus: function(id) {
el = $(id);
if(!el) {
el = document.createElement('span');
el.setAttribute('id', id);
$('progress-bar').appendChild(el);
}
return el;
}
},
FileField: {
add: function() {
new Insertion.Bottom('file-fields', '