1 // http://rubyforge.org/pipermail/mongrel-users/2007-July/003747.html
2 function appendDebug(text) {
3 li = document.createElement('li');
5 $('debug').appendChild(li);
11 begin: function(upload_id) {
12 //$('upload-console').src = '/files/upload_progress?upload_id=' + upid;
14 new Ajax.Pull('/progress?long&upload_id=' + upload_id, {
16 // poll_url is not yet implemented in Ajax.Pull. this is needed for
17 // older versions of safari due to a bug.
19 //poll_url: '/files/upload_progress?single&upload_id=' + upload_id,
20 //pullDebugger: appendDebug,
21 handler: this.update.bind(this)
24 this.uploading = true;
25 this.StatusBar.create();
28 update: function(json) {
30 if(json["state"] == 'starting') {
31 $('results').update('staring upload...');
33 } else if(json["state"] == 'done') {
34 this.uploading = false;
35 this.StatusBar.finish();
36 $('results').update('finished!');
38 } else if(json["state"] == 'error') {
39 this.error(json['message']);
41 } else if(json["state"] == 'uploading') {
42 var status = json["received"] / json["size"];
43 var statusHTML = status.toPercentage();
45 statusHTML + "<br /><small>" + json["received"].toHumanSize() +
46 ' of ' + json["size"].toHumanSize() + " uploaded.</small>"
48 this.StatusBar.update(status, statusHTML);
51 this.error('Unknown upload progress state received: ' + json['state']);
55 error: function(msg) {
56 if(!this.uploading) return;
57 this.uploading = false;
58 if(this.StatusBar.statusText) this.StatusBar.statusText.innerHTML = msg || 'Error Uploading File';
67 this.statusBar = this._createStatus('status-bar');
68 this.statusText = this._createStatus('status-text');
69 this.statusText.innerHTML = '0%';
70 this.statusBar.style.width = '0';
73 update: function(status, statusHTML) {
74 this.statusText.innerHTML = statusHTML;
75 this.statusBar.style.width = Math.floor(this.statusBarWidth * status);
79 this.statusText.innerHTML = '100%';
80 this.statusBar.style.width = '100%';
83 _createStatus: function(id) {
86 el = document.createElement('span');
87 el.setAttribute('id', id);
88 $('progress-bar').appendChild(el);
96 new Insertion.Bottom('file-fields', '<p style="display:none"><input id="data" name="data" type="file" /> <a href="#" onclick="UploadProgress.FileField.remove(this);return false;">x</a></p>')
97 $$('#file-fields p').last().visualEffect('blind_down', {duration:0.3});
100 remove: function(anchor) {
101 anchor.parentNode.visualEffect('drop_out', {duration:0.25});
106 Number.prototype.bytes = function() { return this; };
107 Number.prototype.kilobytes = function() { return this * 1024; };
108 Number.prototype.megabytes = function() { return this * (1024).kilobytes(); };
109 Number.prototype.gigabytes = function() { return this * (1024).megabytes(); };
110 Number.prototype.terabytes = function() { return this * (1024).gigabytes(); };
111 Number.prototype.petabytes = function() { return this * (1024).terabytes(); };
112 Number.prototype.exabytes = function() { return this * (1024).petabytes(); };
113 ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte', 'petabyte', 'exabyte'].each(function(meth) {
114 Number.prototype[meth] = Number.prototype[meth+'s'];
117 Number.prototype.toPrecision = function() {
118 var precision = arguments[0] || 2;
119 var s = Math.round(this * Math.pow(10, precision)).toString();
120 var pos = s.length - precision;
121 var last = s.substr(pos, precision);
122 return s.substr(0, pos) + (last.match("^0{" + precision + "}$") ? '' : '.' + last);
125 // (1/10).toPercentage()
127 Number.prototype.toPercentage = function() {
128 return (this * 100).toPrecision() + '%';
131 Number.prototype.toHumanSize = function() {
132 if(this < (1).kilobyte()) return this + " Bytes";
133 if(this < (1).megabyte()) return (this / (1).kilobyte()).toPrecision() + ' KB';
134 if(this < (1).gigabytes()) return (this / (1).megabyte()).toPrecision() + ' MB';
135 if(this < (1).terabytes()) return (this / (1).gigabytes()).toPrecision() + ' GB';
136 if(this < (1).petabytes()) return (this / (1).terabytes()).toPrecision() + ' TB';
137 if(this < (1).exabytes()) return (this / (1).petabytes()).toPrecision() + ' PB';
138 return (this / (1).exabytes()).toPrecision() + ' EB';